When a dog.barks()…


Back to the future! When Eclispe WTP 2.0 meets Maven 2…
August 13, 2007, 6:58 am
Filed under: Java

I have been using Eclipse3.2 with WTP 1.5 plus Maven 2 for product development. My history of using WTP can actually dated back to its early beta version 0.7, to the days I have to manually edit the projects files to change the project’s web content folder to places other than WebContent/. It’s buggy all along the way but have been constantly improving. When 2.0 is out, I installed at once and start setting it up with my Maven 2 environment.

Maven project first? Or Eclipse project first?

Recalls me the old humor “Which came 1st chicken or egg?”. Someone have even setup a whole site for it (http://www.whichcame1st.com/). Um? Cough! Cough … let’s back to our topic. The first decision to make on setting up the maven enabled project, is to decide where to start first.

Maven first – the official way … boom!

Maven comes with a plug-in that helps generate a eclipse WTP project that fits the standard maven project structure and dependency management practices. In theory all you need to do is executing a mvn command with desired WTP version specified. In practices it never works for me, from WTP 0.7 to 1.5. The Eclipse will either told you that there something wrong with the project’s WTP meta or the classpath. Okay, sorry guys I know I am sort of moron don’t know how to fix a cook. But even if you managed to get it loaded into Eclipse without any error, you might not like the way maven is brought into your eclipse either. It brings the necessary dependencies into your project by listing all into the classpath statistically. Here’s a grep:

<classpathentry kind="var" path="M2_REPO/ognl/ognl/2.6.9/ognl-2.6.9.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/2.0.1/spring-core-2.0.1.jar"/>
<classpathentry kind="var" path="M2_REPO/commons-fileupload/commons-fileupload/1.1.1/commons-fileupload-1.1.1.jar"/>

Not to mention the variable M2_REPO. This approach probably means you have to re-generate the classpath again every time you updates the dependencies of the project. I haven’t figured out if the plug-in is smart enough to merge changes if it founds a .classpath file on its way. But if it didn’t, god bless you my friend.

Most important of all, the plug-in does not support 2.0 by the day of writing. So boom!

How about if I generate the maven project first, not with the wtp plugin of course, then import into Eclipse and make it a WTP project? Looks feasible. As with Eclipse 3.3 you can add facets to the project through the user interface. If a project is not initially a WTP project, you can turn it into by adding the WTP facet. Our team has once used this technique to save a WTP project which loses its WTP nature after passing through the CVS as the component settings are by default not carried along.

So I do it. For unknown reason my Eclipse don’t like it, there’s no facet configuration option show up in the project properties dialog at all that I found no way of adding the facet. Again, boom! Time to go the other way.

Eclipse first – the alternative official way? … boom! boom!

This is my thought: create a WTP project with structure conform to maven project structure then make it maven enabled with the M2Eclipse plug-in. It should work. At least this is the way I do the setup previously with Eclipse WTP 1.5, which runs on a 3.2 core.

Improvements on products, you know, often comes with more validations to prevent users from making serious mistakes that could turn the product into error reporting service. This is obviously true with the new 3.3, it no longer accepts “/” or “\” in resource name. That means you can specify the web content directory as src/main/webapp and Java source directory as src/main/java according to maven conventions.

wtp1.jpg

I don’t know why they do so. Perhaps the folks at Eclipse don’t like things not placed on the first level. I give up this approach anyway.

Craft by Hand. Dirty. But it works!

Yes it is a hack. It’s not smart at all. I did it with WTP 0.7. Now I do it again with WTP 2.0. Well well, it works. Here’s the approach I take:

  1. Open a eclipse, create a reference “Dynamic Web Application” project.
  2. Generate a standard maven project with default archetype
  3. Copy all necessary Eclipse settings files to the maven project and modify these files to make them fits the project.

If you plan to follow the instructions in below to setup a Maven 2 WTP project, make sure the following are done to save your effort later on:

  1. Eclipse 3.3 with WTP 2.0 installed.
  2. M2Eclipse plug-in installed (http://m2eclipse.codehaus.org/)
  3. A servlet container runtime, could be Tomcat or anything supported, is configured.

Ready? Let’s do it.

Step 1: Create a reference WTP Project

Simple huh?

  1. Open eclipse.
  2. New project > Project … > Web > Dynamic Web Project.
  3. Name project. Select target runtime. Click Finish.
  4. Close eclipse.

Step 2: Create a maven project

  1. Do it with maven archetype.
    mvn archetype:create -DgroupId=com.mycompany -DartifactId=my-war
  2. Add the following folders:
    1. src/main/webapp
  3. You might want to add the following folders as well:
    1. src/main/resources
    2. src/test/resources

Step 3: Copy Eclipse files

Copy the following files from the reference eclipse project to maven project.

  1. .project (file)
  2. .classpath (file)
  3. .settings (folder)

Step 4: Edit project setting

  1. Edit .project.
  2. Find <name>${project-name}</name> under <projectDescription>
  3. Change ${project-name} you would like to have with your project. It is recommended that you name your project in only characters, digits, hyper and underscore only, although not strictly required.

Step 5: Edit classpath

  1. Edit file .classpath.
  2. Find <classpathentry kind="src" path="src"/>
  3. Change to <classpathentry kind="src" path="src/main/java"/>

Step 6: Edit WTP component setting

  1. Edit file .settings/org.eclipse.wst.common.component. It should looks like:
    <?xml version="1.0" encoding="UTF-8"?>
    <project-modules id="moduleCoreId" project-version="1.5.0">
    <wb-module deploy-name="${deploy-name}">
    <wb-resource deploy-path="/" source-path="${webcontent-path}"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="${source-path}"/>
    <property name="context-root" value="${context-root}"/>
    <property name="java-output-path" value="build/classes"/>
    </wb-module>
    </project-modules>
  2. Set ${deploy-name} to your desired deploy-name. This name have no effect on the app’s accessing URL It usually only serves as a meaningful label in servlet container’s administration console.
  3. Set ${webcontent-path} to /src/main/webapp
  4. Set ${source-path} to /src/main/java
  5. Set ${context-root} to the path you want the application to be accessed. Say, “my-app”. Then you app will be accessed through http://localhost:8080/my-app. It is usually good to follow the convention that you can name it with the project name, as long as your project name fits URL naming limitations.

Step 7: Import project into Eclipse

  1. Open eclipse.
  2. File > Import > General > Existing projects into eclipse.
  3. Browse the directory where your maven project is located. Click Finish.

Step 8: Post configuration

Nearly done. Just a few more steps to make maven works smoothly with WTP. Make sure that you have M2Eclipse installed before you go.

  1. Enable Maven support
    On the project context menu, select Maven 2 > Enable.
  2. Include Maven Dependency to Web Library
    Project properties > J2EE Module Dependencies> Tick “Maven2 Dependencies”.
  3. Add additional maven source paths.
    By default the project only have src/main/java in the source folder list. Maven directory structure, however, comes with multiple useful source folders. Add them to the list as well:

    1. src/main/resources
    2. src/test/java
    3. src/test/resources
  4. Set order for libraries
    Project properties > Java Build Path > Order and Export, set order as: src/main/java, Maven 2 Dependencies, Web App Libraries, blahalala…
  5. Set order for builders
    Project properties > Java Build Path > Builders, set order as: Maven 2 Project Builder, blahalala…

Step 9: Ready! Set! Go!

Clean your project. Let it build automatically. You might need a eclipse restart or a few more rounds of clean if not all the things work out immediately.

This is how I get my first Struts 2 tutorial running with Eclipse WTP 2.0.

wtp2.jpg

The Rebel

It is heard that Intellj IDEA 7 comes with out-of-the-box support to Maven. I will try it out soon to see if it really works as advertised. Before that, if you are interested you might take a look at Stefan Reuter’s post Using Maven with IntelliJ Idea 7.0.

If you know better anyway to do the integration, please let me know. I seriously looking for better methods 🙂


7 Comments so far
Leave a comment

Libcopy is no longer required for WTP 2.0 + Eclipse 3.3
You can export the M2Eclipse classpath to the server runtime by exporting the J2EE dependencies(Project properties->J2EE module dependencies-> tick Maven2 Dependencies)

no more libcopy builder enable/disable after refresh the POM~

Comment by Not a Blogger

Thanks buddy! It is nice to know I can live without LibCopy. Just great.

Comment by Livingash

Procedure updated. LibCopy parts dropped.

Comment by Livingash

Thanks a lot, great help!!

Comment by yale

Wow, thanks a lot!

After one day of trouble… you save me a lot of time. Tryed it in the same order, first with an archetype than try to mate a wtp project with maven wich was succeesful in some way. But to really combine both wasn’t in my scope until i read your workaround.

And the “J2EE dependencies(Project properties->J2EE module dependencies-> tick Maven2 Dependencies)” is really great!!!

regards qpid

Comment by qpid

Thanks for your post.
I used it to build my own maven eclipse jetty integration process, I described here : http://www.jroller.com/francoisledroff/entry/quickly_create_run_and_test

Comment by Francois

Great post
Thanks you a lot
your procedure looks almost simple, why is there no maven archetype doing such a project structure ?

Comment by marouk




Leave a comment