Felipe Gaúcho works as senior software engineer at Netcetera AG in Switzerland. He is a well known Brazilian JUG leader and open-source evangelist. Felipe works with Java since its early versions and has plans to keep that Java tradition as it is. When he is not coding, he prefers to listen reggae and travel around with his lovely wife Alena and his son Rodrigo. Felipe is a DZone MVB and is not an employee of DZone and has posted 29 posts at DZone. View Full User Profile

Webapps with Eclipse and Maven (maven-war-plugin)

12.28.2009
| 32800 views |
  • submit to reddit

Web applications created by the Eclipse IDE contains an annoying folder WebContent to host the web resources and deployment descriptors. While 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. 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 I: John J. Franey proved the usability of the Sonatype plugin to get better results than the instructions in my blog. Please check this screencast and the comments at the end of this page.

 

ERRATA II: 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.

  1. Create a Web Project in Eclipse: right-click on the Project Explorer > New > Project > Web \ Dynamic Web Project

  2. 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 <a target="_blank" href="http://maven.apache.org/maven-v4_0_0.xsd%22%3E" title="http://maven.apache.org/maven-v4_0_0.xsd">">http://maven.apache.org/maven-v4_0_0.xsd"></a>
    <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>
    </project>
  3. 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

From http://weblogs.java.net/blog/felipegaucho

Merry Christmas and Happy New Year!

Published at DZone with permission of Felipe Gaúcho, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Nicolas Frankel replied on Tue, 2009/12/29 - 5:20am

I'm much dismayed by this article.

The truth is, when Eclipse create Web project, just tell him that the web source directory is src/main/webapp, et voila. Just remember that Maven prefers convention over configuration and don't adapt your project's structure to the IDE but the other way around.

The other solution is just to hack a hidden configuration file (in .settings if I remember well).

I use Eclipse since way back and I would like people to refrain from telling the world that:

Eclipse if far behind the other IDEs regarding Maven support.
That smells like an holy IDE war to me...

The proof, a working example: uncompress and import in Eclipse. It works like a breeze.

John J. Franey replied on Tue, 2009/12/29 - 9:34am

The 'very good M2eclipse' plugin runs maven archetypes. So, the easiest way of creating a new web app project is 'New->Project->Maven->Maven Project'. Use this wizard to select maven-archetype-webapp. It appears that after the archetype is run, M2eclipse reads the pom's 'package' tag and modifies the eclipse project configuration files accordingly. In this case, it adds the eclipse wst nature to the project for you.

The configuration you have (aside from setting the web content path) appear to reflect the policy of your development environment as they are not needed even outside of eclipse. The maven provided webapp archetype does not generate any of what you have in the 'plugin' tag for maven-war-plugin. An alternative to cut-n-paste a developer specific pom template is to provide your own archetype customized for your development environment. M2eclipse can be taught to find custom archetypes: Window->Preferences->Maven->Archetypes.

On install, out of the box, M2Eclipse is aware of many archetypes from many opensource projects, mostly apache projects, but also spring, jboss and many others.

Use of M2Eclipse, not 'mvn eclipse:eclipse', will free you from 'daily hacks' (in your own words).

Zombi G replied on Tue, 2009/12/29 - 8:10pm

I would rather suggest, that  "don't adapt your project's structure to a build tool, but the other way around" ;)

Felipe Gaúcho replied on Wed, 2009/12/30 - 6:08am

Hi guys,

since I wrote this blog entry I am disturbed by the need ot modifying my project structure... and then I found the holy grail of the integration between Eclipse and Maven: the WP Support. Yes, it is there, supported by default by Maven!

 I updated the entry with the new plugin "mvn -Dwtpversion=2.0"

 @nfrankel: no, I am not raising my voice against the too I use daily from the last ten years, I continue to be an Eclipse user and I like Eclipse a lot. My point was just: IDEA and Netbeans just uses the pom.xml as project configuration, out of the box, no tricks, no mess. Eclipse imposes some conventions and in case of Maven project there is a clear and unecessary conflict. I am not the flamewar guy.

 @John J. Franey: yes, m2Eclipse is very good, but to be forced to depend on a tool plugin for properly configure a project is not much elegant. Your suggestion is valid anyway, thanks.

 

with the WTP support by Maven I believe we won't need to worry about the integration problems in the near future.. let's see. Thank you all for the tips and to not allow me to adopt the first lazy tought :) Blogging is about mind sharing and your feedback pushes my epistemic boudanries in a good direction.

John J. Franey replied on Wed, 2009/12/30 - 9:34am

Felipe,

In your comment to nfrankel, you say: "Eclipse imposes some conventions and in case of Maven project there is a clear and unecessary conflict." m2eclipse overcomes this conflict and it is 'very good' (your words, too).

I find a contradiction between your acceptance of the addon to maven but not the addon to eclipse. How is the former acceptable, and the latter not? Maven and Eclipse are both extensible because they are both plugin architectures. 'mvn eclipse:eclipse' represents a dependency on a plugin in the maven architecture. The 'eclipse:eclipse' is as much an addon to the maven runtime, as m2eclipse is to the eclipse runtime. What distinction are you making between these such that m2eclipse is not elegant?

Regards,

John

P.S. I think that m2eclipse is closer to eclipse than maven's eclipse:eclipse plugin even though both are open source and can be modified as necessary. This is just a hunch. I think the m2eclipse plugin can better utilize the eclipse runtime than the maven eclipse:eclipse plugin which can only modifiy the external project configuration files. In that way, I'd say the m2eclipse plugin has advantage in elegance.

Nicolas Frankel replied on Thu, 2009/12/31 - 5:36am in response to: John J. Franey

Thanks John, you took the words out of my mouth.

Felipe Gaúcho replied on Fri, 2010/01/01 - 11:00am in response to: John J. Franey

Hi John,

you are right.. maven and eclipse uses two distinct conventions, and there is no better-or-worse concept appliable here.

* IntelliJ IDEA and Netbeans just uses the pom.xml file to import the project, so there is no need of running eclipse:eclipse or special project structure adaptations. I don't know why Eclipse can't have a feature like that. My comments were a bit biased due to the lack of such feature in Eclipse, but I should admit conventions has no quality precedence within each other.

I also figured out two thing about the magic -Dwtpversion=2.0 flag:

  1. It applies the WTP support for any project and not olny the ones packaged as webapps (<packaging>war</packaging>)
  2. As soon you run eclipse:eclipse without the flag,  the WTP support will removed.

So, I guess m2eclipse remains as the best choice for the Eclipse users. -Dwtpversion is still a good workaround but soon or later will cause problems.

John J. Franey replied on Fri, 2010/01/01 - 1:56pm in response to: Felipe Gaúcho

Felipe,

IntelliJ IDEA and Netbeans just uses the pom.xml file to import the project, so there is no need of running eclipse:eclipse or special project structure adaptations. I don't know why Eclipse can't have a feature like that.

Eclipse does indeed have a feature just as you describe. This is the capability that comes in m2eclipse plugin. I refer you to the chapter "Import Maven Projects" in the m2eclipse documentation: http://www.sonatype.com/products/m2eclipse/documentation

The m2eclipse plugin to eclipse frees you from using 'mvn eclipse:eclipse'. m2eclipse imports the pom.xml and configures the eclipse builders for building the project as described in the pom. The dependencies are pulled into eclipse classpath. The build output directory is set to target/classes. It adds the wtp nature for maven 'war' package. It does all this. 'mvn eclipse:eclipse' can be dropped.

Regards,

John

Felipe Gaúcho replied on Sat, 2010/01/02 - 4:06am

ok, I will try again.. the steps I tried so far:

  1. Create a web app project with maven: mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
  2. import in Eclipse
  3. Run in Server in eclipse

If you know how to do that, please blog about it.. otherwise if I suceed in my trial I will publish a second blog retificating my oppinion :)

I asked in mailing lists and there is no responses so far, so I believe that's a common misunderstanding about how to use m2eclipse properly ... but first, show me the example :)

 

* if you wanna try my pet project: 

hg clone https://hg.kenai.com/hg/puj~arena

and then you have all code.. the webapps are:

  • arena-dwr
  • arena-vaadin
  • arena-jsf20
thanks tfor the patience to keep discussing.. perhaps I was just missing a feature from the plugin.. let's see.

Murali Mohan Rath replied on Sat, 2010/01/02 - 6:03am

Guys I agree with Felipe. I have used both NetBeans and eclipse. But when it comes to maven support NetBeans is much ahead of eclipse. You don't need to run any commands or tricks ;-) with NetBeans.

Note: I like eclipse more than NetBeans

John J. Franey replied on Sun, 2010/01/03 - 8:42am in response to: Felipe Gaúcho

Please have a look at this (silent) movie.

I show:

  • import and run my-webapp generated by maven web application archetype
  • import arena-jsf20

Felipe Gaúcho replied on Mon, 2010/01/04 - 1:54pm

Mr. Franey,

impressive :) Thanks for proving your point....  you are absolutelly right and my blog was unfair with Eclipse.

Watching your movie I started to figure out the problem: my Eclipse doesn't have such Sonatype Maven WTP support plugin :(

I have no idea why, since I installed a brand new Eclipse and added the Sonatype maven plugin...

I will try to reinstall everything paying more attention for the details.... also, I will check if it works when importing the root folder (instead of a webapp project).

 

thanks a lot.. 

Felipe Gaúcho replied on Mon, 2010/01/04 - 2:26pm in response to: John J. Franey

more feedback:

 

yes, I reinstalled everything and now the WTP support is enabled to Maven projects. Well done, but: 

it only works if you import the webapp separatelly. If you try to compile the whole project  and import all sub-modules pointing to the root directory of the Java EE project, the submodules that are web applications are not loaded as WTP projects.

* It is already an advance to know I can import simple projects as WTP nature...

* Combinning the -Dwtpversion=2.0 with the WTP plugin it works..

* after using -Dwtpversion=2.0, if you run eclipse:eclipse again in the root folderm, goodbye WTP.

I would say now that Eclipse is not yet stable and reliable for Maven web development, but my oppinion is much better about Eclipse after I wrote the blog and took the discussions.  :)

John J. Franey replied on Tue, 2010/01/05 - 11:42am in response to: Felipe Gaúcho

I would say now that Eclipse is not yet stable and reliable for Maven web development

Of course, now that you have been able to use the import maven feature, your opinion can be more valued. I certainly honor your opinion. However, I don't agree that you have spent enough time using m2eclipse at this stage to evaluate its stability and reliability. You report a functional problem in a specific use case. Hardly a show-stopper. This problem and specific stability and reliability issues are fairly handled by reporting them directly to the m2eclipse bug report system, no?

You can workaround this problem by deleting the webapp child projects and re-importing them individually, or by importing them individually to begin with. I'd suggest the workaround only because the 'mvn eclipse:eclipse' approach is prone to human error.

Anyway, I wonder if anyone can post a movie of netbeans in the same use case? I have very little knowledge of netbeans and will probably lead myself astray. I'm interested in seeing a comparison to see for myself whether this claim can be sustained (made by mmrath):

But when it comes to maven support NetBeans is much ahead of eclipse.
.

Felipe Gaúcho replied on Wed, 2010/01/06 - 4:37pm in response to: John J. Franey

I will leave this point open.. I changed my mind set along our discussion, and I am very thankful for that ... while still not 100% happy with Eclipse, I am able to work with that now much better than before.. let's see what I can learn next.

Markos Charatzas replied on Mon, 2010/07/05 - 10:50am

Trying the same thing as the vidcast with the Eclipse Helios release and the m2eclipse
0.10.2.20100623-1649 release doesn't look like it picks up "src/main/webapp" as the 
WebResource.

Also the mvn eclipse plugin for WTP seems to be out of date.

Also, not sure if it's an m2eclipse bug, the Eclipse Helios release uses a "Deployment Assembly"
instead of a "J2EE Module Dependencies" under "Properties" which doesn't look like it's updating
the maven dependencies correctly. 
Eclipse Forums

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.