Friday 13 February 2009

Eclipse Dynamic Web Project + Maven2 = initial pain

Eclipse 3.4 ganymede gave me a VERY frustrated day yet again :(
I spent most of the day convincing it to to install the m2eclipse plugin.
It kept on telling me:
No repository found containing: org.maven.ide.components.dependency_tree/osgi.bundle
So after updating all my eclipse plugins and also using the Maven Integration for Eclipse Dev Update Site and restarting Eclipse, it finally decided it will install.
(You almost want to swear at it and call it M$ Eclipse. Ok it wasn't that bad.)

Next I came to a painful catch 22: I want a Dynamic Web Project so that my code can be automatically deployed on my Glassfish server. But I also want it to be a Maven project for all the maven goodness, without changing the standard maven code layout.

I googled for hours but I only found my worst fear to be true: The only way to do it is to make it a web project, changing its structure to be in the maven layout and then activate it as a maven project. I improved on this site's methods a bit by doing some configs up front and thus avoiding to mess with them later. Here are my steps as good as I remembered:
  1. (I created a reference maven web project in eclipse, but it turned out that I didn't actually need it)
  2. Create a new Eclipse Dynamic Web Project. Don't click finish, click [next].
  3. Set the 'Content Directory' to 'src/main/webapp' and the 'Java Source Directory' to 'src/main/java'. Now you can click [finish].
  4. Right click on the project -> Maven -> Enable Dependancy Management.
  5. Set the packaging to war, [finish].
  6. You can delete the build folder now since maven already started to use the target folder.
  7. If you need the other standard maven source and target folers, you can add them manually or directly in .classpath:
    <classpathentry kind="src" path="src/main/resources">
    <classpathentry kind="src" output="target/test-classes"
    path="src/test/java">
    <classpathentry kind="src" output="target/test-classes"
    path="src/test/resources">



  8. From there I just had to set maven to use Java6 in pom.xml:
    <build>
    <pluginmanagement>
    <plugins>
    <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <configuration>
    <source>1.6</source>
    <target>1.6</target>
    </configuration>
    </plugin>
    </plugins>
    </pluginmanagement>
    </build>

  9. Right click on the project -> Maven -> Update Dependancies.
  10. I removed the following from .settings/org.eclipse.wst.common.component:
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>
  11. Then I could run maven tasks and also deploy it to my glassfish server (already set up to be managed by eclipse the normal JavaEE way (run as -> run on server))
sweetness! I love Eclipse+JavaEE+maven again.
I think the WTP or whatever does the new 'Dynamic Web Project' wizard would do well to have a checkbox which can setup the web project in the maven sturcture for you.
tomorrow (aka later today) I have to do the real work of actually putting an application into this empty shell.

goodnight

3 comments:

  1. Great post. Thanks for the tip. It helped me a lot. God bless you!

    ReplyDelete
  2. I know it`s an old tip, but this is not working for me. The server do the deploy but the dependency libs from Maven is not present on the server, causing a lot of ClassNotFoundException. If I do an Export in Eclipse (right click, Export, WAR) the libs are present, but on the server... FAIL!
    PS: Sorry about my english! I'm brazilian, :lol:

    ReplyDelete
  3. @Allemand: You probably need to set the maven dependencies to get exported by the project in its classpath settings, see: http://amanica.blogspot.com/2010/10/eclipse-tomcat-maven-pain-again.html

    ReplyDelete