Integrando Maven2 y Eclipse WTP / WTP and Maven2 integration

Sunday November 25th 2007, 2:07 pm
Filed under: Java, Tecnologí­a

english spanish 

I’ve been using Maven2 and Eclipse with WTP for a long time. To me they make a good match, because they offer very attractive options:

Maven2:

  • Complete automation of the building process, and ad-hoc customization.
  • Handles project dependencies in a rather efficient way.
  • Easily integrated with Ant, so it has its flexibility and countless options.
  • Eases the continuous integration process, by executing tests, coverage reports, programming styles, etc.

Eclipse WTP:

  • Allows execution of a container or appserver inside Eclipse, so it’s possible to debug applications (even JSP’s) , and hot deploying, so there’s no need to restart the server every time a class, a JSP or other file changes.
  • It has a good autocomplete JSP/HTML/XML editor. Version 2.0 even has a graphical HTML editor (although it has some problems with css).

The problem is they don’t integrate easily. WTP needs a specific directory structure which is different than Maven’s. Even though Maven downloads dependencies automatically, it stores them in its own local repository which has nothing to do with WTP, and therefore .jar files need to be manually copied where WTP will be able to find them to do its own internal deploys.
After a couple of days on trial and error (since documentation is non-existent), I’ve found the way to integrate them 100%, with help from Eclipse plugin for Maven. These are the steps to follow, I have tested it and it works great. I assume you have a basic knowledge of Maven and pom.xml structure, if not, I would recommend this brief tutorial. The versions I used were Maven 2.0.7 and WTP 2.0., it might work with previous 2.0x branch of Maven.

  1. Let’s start with a project with a directory structure almost as proposed by Maven:
    .
     |-- pom.xml
     |-- WebContent
     |   |-- WEB-INF
     |   |   `-- web.xml
     |   |-- index.jsp
     |   `-- jsp
     |       `-- websource.jsp
     `-- src
          `-- main
              |-- java
              |   `-- com
              |       `-- example
              |           `-- projects
              |               `-- SampleAction.java
              |-- test
              |   `-- com
              |       `-- example
              |           `-- projects
              |               `-- SampleActionTest.java
              `-- resources
                  |-- log4j.properties
                  `-- hibernate.properties

    If the project is already going with a different structure, well, you will need to adapt it, or find another way to make the integration work.
    The difference with standard Maven is we placed all the web files in WebContent instead of src/main/webapp.
    Unfortunately, Maven for Eclipse plugin (which automatically builds an Eclipse project based on pom.xml) currently doesn’t support WTP 2.0, so I couldn’t test it and I don’t know how integration would be. Hopefully they will support WTP 2.0 soon.

  2. This project can be migrated to Eclipse, (if it’s not yet) doing File -> New -> Other -> Web -> Dynamic Web Project, unchecking Use default and entering the directory where the project is. For some reason the paths cannot be changed to suits us, since strangely the field won’t accept values including “/”, so we’ll leave it as it is, and we’ll fix it later. Once the project is opened in Eclipse, if we want to run it in a container o make a build with maven, it won’t work because of the following reasons:
    • Packages names are wrong, they start with main.java
    • The web.xml at WebContent/WEB-INF/web.xml is one created automatically by WTP, not our project’s, and Maven can’t find none of the web files (.html, .jsp, .js, .jpg, etc.), since they’re supposed to be found at src/main/webapp.
    • WTP can’t find any of the resource files usually found in the classpath (i.e. .properties, .xml, etc)
    • Finally, WTP can’t find any external .jar, since these are in the Maven repository in ~/.m2/repo and WTP wants them on WebContent/WEB-INF/lib

    Veamos entonces como solucionar cada uno de estos problemas.

  3. To fix packages, go to Java Build Path -> Source and remove the only directory on the list. Then select Add Folder, and choose the directories containing the .java files, namely src/main/java y src/test/java. This way, packages are fixed.
  4. To make the build work in Eclipse, let it know where to find the we files. Add the following lines to the pom.xml, in the plugins section:
            maven-war-plugin                  WebContent        

    Now the Maven work should be working, we could try it by doing mvn clean install freom the project home directory.

  5. To fix the resources, let’s do a small trick. Go to Java Build Path -> Source and select Link Source… At Linked folder location enter the path to our resources directory src/main/resources. The Folder name ma be any. I’d suggest creating a variable PROJECT_HOME or something, pointing to our project root, and then extending the link folders from there, so it’ll be portable, if we later move this project to other location or PC, or give it to someone else, it’ll be as easy as changing this variable for the project to work from another location.
    This will cause the resources directory will be included in the classpath, and the internal deploy will put it in WEB-INF/classes.
  6. To make WTP find the .jar, we’ll use Eclipse for Maven plugin, which will also allow us to run Maven from isnide Eclipse, and make it easy to add dependencies.
    Once installed, the pom.xml file will appear decorated with a small m at the top left corner of its icon. Right-click on the project root, and choose Maven2 -> Enable from the menu. This will add a new library named Maven 2 Dependencies to our project, containing all the dependencies detailed in the pom.xml. This is enough for the whole lot of dependencies to be in the classpath , so the project should compile inside Eclipse with no problems now. However, WTP still needs the dependencies to be at WebContent/WEB-INF/lib to make the internal deploy.
    For this last detail, go to project properties, and at J2EE Module Dependencies, select the library Maven 2 Dependencies. This way, we’re letting WTP know to include Maven dependencies at WEB-INF/lib in its internal depoly.

Once all these steps are done, should be possible to satart a container inside Tomcat (I used Tomcat 5.5 or 6) and run our project as a webapp. We should be able to make builds with Maven from inside Eclipse as well, right-clicking on the project root and choosing the appropriate Run as… Finally, to add a new dependency. right-click on pom.xml and choose Maven2 -> Add Dependency, and in the query field we write part of the name of the dependency. Almost immediately, a tree with the found results will appear, we simply move until we find the desired package and choose it. The dependency will be added to the pom, the .jar will be downloaded into our local repository, and added to the Maven dependency library, therefore to the WTP internal deploy.

Note: don’t choose the project option Maven2 -> Update Source Folders, I don’t know exactly what its purpose is, but the immediate effect is to make the link created to the resource folder no longer a link, so WTP will not include it in its internal deploy.
Note 2: sometimes the resources in the link don’t deploy automatically when modified. To force a deploy, it’s as easy as refresh (F5) the linked folder.
Conclusions

To have Maven2 integrated into Eclipse WTP eases the whole development cycle of a webapp.
Starting with this, more resource folders, different builds with changes or different structures, different builds based on the same basecode, etc., can be added later. Maven2 documentation was never that great, but with a bit of patience the desired objectives can be achieved.

Powered by ScribeFire.


| show comments »