How to set an eclipse/webapp maven? - java

What's the correct form to set up a maven webapp project?
It should be able to:
Run with the eclipse embedded tomcat (available in the servers tab).
This way I can run/debug the application like a regular webapp.
Run with the maven plugin tomcat7. So far I can only run the run-war
goal, couldnt make the "run" goal work =/
I have followed some tutorials, but couldnt make all these things to work properly.
When I follow the MkYong suggestion, I can run with the embedded tomcat, but the project dependencies are changed to classpath variables. This is not a good consequence at all, since the project loses the ability to dynamically set the dependencies.
I use Eclipse Juno (I'd rather wait Kepler for a couple of months to check it is really stable), m2e and m2e-wtp plugins, and have 2 kinds of projects: one is based in JSF (the front-end) and another in Apache CXF (the back-end). I hope these two can be hot deployed (when a resource changes, embedded tomcat automatically publishes it) in development environment to improve productivity.

Source taken from
Web applications created by the Eclipse IDE contains an annoying folder WebContent to host the web resources and deployment descriptors. Quite natural for Eclipse users, this feature ignores the Maven convention and force the developers to hack the pom files in order to get the project up and running in Eclipse. In this aspect, Eclipse if far behind the other IDEs regarding Maven support, even if you consider the very good M2Eclipse plugin. So, for you lazy Christmas hackers, here it is a solution for the Maven integration problems in Eclipse based on the maven-war-plugin.
ERRATA: please use the WTP plugin instead of changing the project structure. I figured out the wtp plugin after writting this blog, so I suggest you to check just the 1st and the 3th steps of the below instructions. I kept the original blog information in case you really need or want to change the project structure.
Create a Web Project in Eclipse: right-click on the Project Explorer
New > Project > Web \ Dynamic Web Project
Create a pom.xml file in the project root folder, with the following
content:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.cejug</groupId>
<artifactId>webapp-test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>webapp-test Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
<build>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/WebContent</directory>
</resource>
</webResources>
<warSourceDirectory>WebContent</warSourceDirectory>
<warSourceExcludes>WebContent/WEB-INF/lib/*.jar</warSourceExcludes>
<archiveClasses>false</archiveClasses>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix />
</manifest>
<manifestEntries>
<url>${pom.url}</url>
<Implementation-Build>${buildNumber}</Implementation-Build>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Vendor>CEJUG</Implementation-Vendor>
<Implementation-Version>${project.version}</Implementation-Version>
<Built-By>${user.name}</Built-By>
<Built-OS>${os.name}</Built-OS>
<Build-Date>${timestamp}</Build-Date>
<SCM>${buildNumber}</SCM>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Compile and prepare the project for eclipse:
mvn -Dwtpversion=2.0 compile eclipse:eclipse
Notice the usage of the plugin -Dwtpversion to enable Maven to add Eclipse WTP Support to the project. That simple flag do the trick :). Actually just using that flag will work, but not all plugins of Eclipse will work out of the box without the WebContent folder - it is up to you to decide if it is worthy to modify your project structure or just go straight ahead with the plain Maven folder.
Done, now you can refresh the project in Eclipse and continue to work. Remember that jjust the resources folder is hard coded in Eclipse, the src/main/java continues as Maven expects. Perhaps some day we can have the confluence between the conventions of Maven and Eclipse and then we will finally becomes free of this daily basis hacks.

We are using Kepler and we find it quite stable. You can create a normal Dynamic Web Project and then right click and make the project Maven enable. Then you have hot deploy and maven management POM.
Also you can have all Maven goals there.

Related

How to make a plugin-like architecture runnable?

We have a couple of legacy Java projects, which we converted to Maven projects / modules. Previously, all projects were NetBeans projects and had no real dependency management. External dependencies existed on the companies network drive and were directly included as JARs in the NetBeans projects of each module. For the internal dependencies, simple project references were used. It was a pain to build everything because the programmer had to build everything in the right order.
Now, we are in the position that we can open all the Maven modules in IntelliJ IDEA and NetBeans. However, I am having trouble figuring out the best way to combine the different modules and external dependencies in a specific way, which conforms to in-house plugin-like structure. Especially with NetBeans (developing with both IDEs must be possible).
Here is how the git repositories / project structure roughly looks like. The folder structure of the modules is the default Maven structure for each module. The list feature of this site was too clumsy, so I included it as screenshot...
We have an internal maven repository for the stuff and building with maven etc. is working. For Intellij IDEA i can run and debug the end product for customer1 via a custom run configuration, which copies the needed files in the needed structure:
With IntelliJ IDEA, I can debug the software, but I think that the approach (custom IntelliJ run config I created, pointing to all needed JARs and files directly) is rather ugly, and for NetBeans I could not find a similar "run configuration" mechanism.
So I tried to achieve this build process by creating a new "Customer1Runnable" Maven project as a sort of build description, which points to all needed Maven modules. Based on this, I believed I could achieve and automatism to create the needed software structure. Ergo copy all modules into a plugin folder and all dependencies of the modules into a lib folder inside the Customer1Runnable project, using the maven-assembly-plugin.
First off, is my assumption correct that this is a possible use case for the maven-assembly-plugin?
The project itself does not have any source files, it is only a pom.xml and the assembly-config.xml descriptor. I attached the assembly-plugin to the package phase. When running the mvn package command all connected modules are built, but for the execution of the assembly-plugin I get the following output:
For starters, I only tried to include one module in the assembly descriptor. This is the XML (opicom-assembly.xml) for it:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>opicom-assembly</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>my.company.reporting:module1</include>
</includes>
</moduleSet>
</moduleSets>
</assembly>
pom.xml of Customer1Runnable project
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<version>1.6</version>
<groupId>my.company.customer1</groupId>
<artifactId>OpicomRunnable</artifactId>
<packaging>pom</packaging>
<name>OpicomRunnable</name>
<repositories>
<repository>
<id>Company-Maven-Repo</id>
<url>file:\\\\MyCompany\TFSDrop\MavenRepo</url>
</repository>
</repositories>
<modules>
<module>../my.company.customer1.module1</module>
<module>../my.company.customer1.module2</module>
.
.
.
<module>../../MyCompany_Common/Report/my.company.reporting.module1</module>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<inherited>true</inherited>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>opicom-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
The pom of a module looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>my.company</groupId>
<artifactId>reporting</artifactId>
<version>1.3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>module1</artifactId>
<version>1.3</version>
<packaging>jar</packaging>
<dependencies>
<!-- external dependencies -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<finalName>my-company-${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>../build</outputDirectory>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Thanks for any input on what I am doing wrong here / how to achieve this with Maven.
EDIT:
As requested, here an example project as ZIP-File.
https://drive.google.com/drive/folders/1ilJeTrOPgYgUTdOP0J4BQcBnPT5fls0k?usp=sharing
The parent directories ModuleGroupCustomer and ModuleGroupCommon do represent git repositories in the real scenario. The relative module path is caused, because the maven project which should be my "run config" points to maven projects in both repositories.
Maybe I am misunderstanding Maven in general? I thought of it in terms of use cases for dependency management similar to .Net nuget packages, but also as "project configuration" like ordinary NetBeans/Intellij projects.
Is it better to simply stick to the existing NetBeans projects for day to day development?
After a long and tedious process of trial and error, I have found a solution which is working for me. So I decided to share the solution online, in case someone else runs into a similar problem. Here is a link to the final zip archive containing working example projects => File CustomerRunnable_RunningAssemblyPluginStackoverflowExample.zip https://drive.google.com/drive/u/0/folders/1ilJeTrOPgYgUTdOP0J4BQcBnPT5fls0k
My error was that I misunderstood how the assembly-plugin works. The approach that I executed the plugin inside my aggregator pom (CustommerRunnable) is wrong, as this maven project only exists as parent pom.
The CustommerRunnable pom.xml references all customer plugins as modules. Those modules have not the CustommerRunnable as parent, but a different pom. Then I created a separate maven project "distribution". The pom.xml of the distribution defines all the plugins (needed customer maven modules) as dependencies. It also has the CustommerRunnable pom.xml as parent. Hence when I run the project in NetBeans, all connected modules are also build(if necessary).
It also configures the assembly plugin. The assembly plugin is attached to the maven package-phase and thus executed with it. It also uses a custom assembly descriptor, which copies all the previously defined plugins into the right folders. This is done by using dependencySets with include and exclude patterns.
See https://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html for details on this.
So one dependencySet copies all jar files of all plugins to a /plugin folder by using an include pattern. Then this approach is inversed to copy the jar files of all external dependencies to a /lib folder.
The descriptor also defines some extra files to copy to a particular location. exec-maven-plugin, so I can comfortably start the customer software out of NetBeans. I didn't yet manage to configure the execute plugin correctly regarding the needed classpath arguments.
Endresult looks like this:
It is also worth noting that the configurations of the "Build project", "Run project" and "Debug project" inside NetBeans need a tiny bit of modification. (Right Click Module "distribution" -> "Properties" -> point "Actions"

Deploy dynamically project on Tomcat from eclipse

I am wondering if there is a way to dynamically deploy a maven project on eclipse and have the same result as if it was packaged in a WAR.
I noticed that when I right-click the project and choose "run as" and then choose tomcat the project deployed does not work properly but when I generate the war and place it manually it works fine.
That means everytime I change something in the code I have to generate a war and deploy it manually on the server.
Is there a tomcat config that I can use to have an output when I run from eclipse similar to when i generate a war?
I'm using tomcat 6.0.26 and eclipse Neon 3.
Thanks.
Yes, you can ...
In general the steps are:
1) Turn your existing project into a maven project by adding a pom.xml like this into the project root folder:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany</groupId>
<artifactId>yourApp</artifactId>
<packaging>war</packaging>
<version>2.3</version>
<name>Your Web Application</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Add your dependencies as needed. -->
</dependencies>
</project>
2) Rightclick the project and Configure -> Convert to Maven Project
3) In Eclipse Servers view add an Apache Tomcat.
4) Rightclick the Tomcat server and Add and Remove ... -> Add your project.
5) Launch the Tomcat in debug mode.
Now eclipse will deploy the project in exploded .WAR format, replacing and therefore hot deploying resources on every change and compiling Java classes on change.
If you do not alter Java method or class signature, changes in Java source code and resources take effect instantly without the requirement to restart Tomcat or the web application.
The problem was that I didn't configure the maven profiles when I ran the project from eclipse which explains why I had different outputs when deploying the project from a war and directly from eclipse. The answer here Maven Profiles and Tomcat in Eclipse explains how to configure maven profiles when you run on server from eclipse.

Continue development of Plugin

There is an Eclipse Plugin managed by Maven containing this configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wonttellya</groupId>
<artifactId>wonttellya</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
...
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
<configuration>
<pde>true</pde>
</configuration>
</plugin>
</plugins>
</build>
</project>
In console I run
C:\Users\user\git\wonttellya\mvn
eclipse:eclipse -Declipse.workspace=C:\Users\user\workspace2
...
Using Eclipse Workspace: C:\Users\user\workspace2
...
BUILD SUCCESS
If I open Eclipse in the workspace there is no project.
First of all, you have to understand that the purpose of the maven-eclipse-plugin is, quoting its documentation:
to generate Eclipse IDE files (*.classpath, *.project, *.wtpmodules and the .settings folder) for use with a project.
Its goal is not to create an entire project but the building Eclipse blocks from an existing project.
This is also true for PDE support. Quoting its documentation:
Note that the scope of the maven-eclipse-plugin is to synchronise the Eclipse .project and .classpath files with the configuration found in the pom file. Once you have finished configuring the Eclipse plugin as below, and once you have run the eclipse:eclipse goal, you will be in a position to build your plugin code with the Eclipse IDE, or the Eclipse headless PDE build. The Eclipse headless PDE build can be triggered from within Maven using the pde-maven-plugin.
As such, the configuration you have simply enables the creation of correct .project and .classpath files for an existing project, nothing more. Once this configuration has been made and eclipse:eclipse goal was run, you will need to follow these steps:
Open Eclipse and import the existing project, by going to "File > Import... > Existing Projects into Workspace".
Right-click the new project and select "Configure > Convert to Plugins Projects...". Confirm this choice.
You will then be able to build your Eclipse plugin directly in the IDE.
Note that I do not recommend using this solution and I would suggest you use Tycho instead, this might be an improvement you could make to this plugin (refer to this question).
Make sure you have update your project before you run the maven install
Try to click on your project with the right mouse button and go to maven-->update project
For a different clause
You can use export and import with archive (.zip) that you can manage Plugin and simply transfert your project in different workspaces

How to create a Maven project in Eclipse

I want to create a web application project in Eclipse with Maven. Everytime I try to create the project I get an error as "Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories".
I have checked for solutions presented in other questions tried them, but none of them solved the issue. I have also changed my settings.xml file to point it to proxy even that didn't help. I also tried deleting the repositries folder in .m2.
Please suggest some solutions for this
Open Window > Preferences
Open Maven > Archetypes
Click 'Add Remote Catalog' and add the following:
Catalog File: http://repo1.maven.org/maven2/archetype-catalog.xml
Description: maven catalog
That is quite a weird issue ... however m2eclipse gave me my fair share of problems when I tried to create my projects. In fact, I ended up creating the archetypes myself!
Let me share my maven 3 POM file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- add your dependencies here -->
</dependencies>
<build>
<finalName>example-project</finalName>
<pluginManagement>
<plugins>
<!-- v. useful! m2eclipse sometimes fails to see it as a
dynamic web app project in Eclipse. Declaring this plugin
would help eclipse recognize its nature (i.e. a Java
project requesting at least JDK1.7+ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Running mvn eclipse:eclipse on this project should work. You can safely import to eclipse as a maven project as well (File > Import > Existing Maven Projects) If it doesn't, then you should consider re-installing a fresh copy of maven.
Let me know if you manage to get it up and running. :)
I was recently struggling to create a new Maven project in Eclipse. Trying to create a Dynamic Web Project first and then converting it into a Maven project did not work (none of the Java integration was working and my code wouldn't even compile)!
The following article describes how to create a Maven project in Eclipse.
The tutorial recommends skipping the archetype part, which might solve the issue described in the original question:
https://www.tech-recipes.com/rx/39279/create-a-new-maven-project-in-eclipse/
If that doesn't work for you, perhaps doing a clean install with the latest version of Eclipse, in a new directory, may help. What you described might just be a bug in Eclipse or one of its installed plugins.

m2eclipse maven build parameters

I am developing a spring mvc app using eclipse kepler with the m2eclipse plugin. How do I create a war file from within eclipse using the m2e plugin?
Do I use run as... maven build...? If so, how do I configure it? I right clicked on the root folder for the app in the eclipse workspace and chose run as... maven build..., which resulted in the following dialog box:
But I do not know what parameters to enter into the dialog box. Can anyone guide me through how to create the war file?
Put Goals as clean compile package
There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's site documentation.
Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle.
Read more about Build Lifecycle Basics
Here is the complete description with snapshots Using the M2Eclipse Maven Plugin in Eclipse
Please have a look at Tutorial - Maven Eclispe IDE Integration
First of all you need war plugin configured in your pom.xml
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- In version 2.1-alpha-1, this was incorrectly named warSourceExcludes -->
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
which is associated with one of the phase (generally package) then you need to put clean package in goals

Categories

Resources