Your Maven Java WEB project in Eclipse WTP

In our company, all Java projects are setup with Maven configuration so that after a “mvn eclipse:eclipse” any developer is generally good to go. One of these projects was a web project but would not transform into a WTP project. By running “mvn eclispe:eclipse” it became a Java project, but could not be added to a Server in Eclipse. It was not a WTP project.

I learned that the author of the project tried but never got the WTP plugin to work properly. Using the Google, I found more people who are having the same problem converting their existing Maven Java Web projects in Eclipse into a WTP project. There are even a few desperate articles describing how to edit your .project and .classpath files. Oh dear. This calls for an article on http://www.rolfje.com.

It all came down to the maven-eclipse-plugin which would just not configure my project properly. I soon learned that in their infinite wisdom, the development team decided that the maven-eclipse-plugin no longer uses a default value for the <wtpversion> setting. Even if you have the <wtpapplicationxml>true</wtpapplicationxml> element in your pom.xml, without the <wtpversion> tag the maven-eclipse-plugin will ignore all wtp project settings silently. So the solution was rather simple:

Short answer
The short answer to this fun evening full of Googling and reading is: Add the <wtpversion> tag to your maven-eclipse-plugin configuration, run “mvn clean eclipse:eclipse” and refresh your project. That’s it.

The long version
For people who like to copy-paste (like me): Change your pom.xml file in the root of your project to include the <wtpapplicationxml> and <wtpversion> tags as shown at the bottom of this example pom file:

<project>
  <packaging>war</packaging>

  <dependencies>
    [...]
  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.xsd</include>
          <include>**/*.wsdl</include>
        </includes>
      </resource>
    </resources>

    <testResources>
      <testResource>
        <directory>src/test/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </testResource>
    </testResources>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <wtpapplicationxml>true</wtpapplicationxml>
          <wtpversion>2.0</wtpversion>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Open a command prompt at the root of your project (where the pom.xml file is) and have maven re-fresh the project, like this:

mvn clean eclipse:eclipse

Open eclipse, right click on the root of your project and select “refresh” (or press F5).

After you’ve done this, you should have “Web Project Settings” in the properties of the eclipse project. If you right-click on your defined servers in Eclipse, the project should show up in the “Add and Remove” dialog.

Have fun!

12 Responses to Your Maven Java WEB project in Eclipse WTP

  1. harkje says:

    Or you try Netbeans or IntelliJ; then Maven integration “just works”. As an added bonus you do not have to insert IDE specific configuration in your IDE independent Maven pom.🙂

    Incredible how bad Maven support is the most popular/used IDE….

  2. Tim van der Leeuw says:

    Yes, absolutely incredible! It’s what made me freak out with Eclipse in the first place several years ago (along with other issues, but this was among the top problems) and eventually settle on NetBeans…

  3. […] Here is a good tutorial show you about Maven WEB project in Eclipse WTP: In our company, all Java projects are setup with Maven configuration so that after a “mvn eclipse:eclipse” any developer is generally good to go. One of these projects was a web project but would not transform into a WTP project. By running “mvn eclispe:eclipse” it became a Java project, but could not be added to a Server in Eclipse. It was not a WTP project. Posted in Maven | Tagged eclipse, eclipse wtp, maven, maven eclipse, Maven WEB project […]

  4. Anilkumar GT says:

    I agree it can be frustrating to get this integration working…. later on i found out this command and note no need for any plugin related configuration changes …. just run the following command:

    mvn -Dwtpversion=2.0 eclipse:eclipse

    instead of just mvn eclipse:eclipse

    • rolfje says:

      Yes I found that too, before writing this post. It’s effectively doing the same thing, but now everybody in your team must remember to type that each time and without errors. The whole point of Maven is to have a painless, quick and easy setup.

      Doing it this way will enable you to say “just check out and run mvn eclipse, and you’re done”. Your team will thank you for effort spent on getting this out of their way.

  5. Douglas says:

    Thanks for this tutorial.

  6. Nacho says:

    Thanks a lot, worked like a charm🙂

  7. Erik Kerkhoven says:

    Rolfje it is written clearly on the Maven WTP support page:

    “Note that in the 2.0 version of the plugin the default was R7 and WTP configuration was always generated. Now that different incompatible versions are out the default is to not to generate WTP configuration if the user doesn’t specify the desired version.”

    See: http://maven.apache.org/plugins/maven-eclipse-plugin/wtp.html

    What’s the problem? Just joking!

    I think the difference with for example IDEA is that Eclipse is a flexible plugin platform. Plugins come and go, and it all has to work together.

    Thanks for the blog! very usefull because this setup stuff I am doing only once in a while and forget easily.

  8. Erik Kerkhoven says:

    I think that I do not agree with the opinion that Maven must be IDE independent. Maven offers the possibilty to configure plugins. Several tools can be plugged in, ranging from code compliance to JAXB. All those tools are part of the development environment of an engineer, The Maven plugins are by defintion not (I)DE independent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s