Thursday, July 17, 2008

Eclipse and Maven integration

If you are looking to use Maven on your next Project and currently use Eclipse as your IDE, then I would suggest you also take a look at MyEclipse 6.5. This new version of MyEclipse has some nifty features implemented that aren't just a whole bunch of open source plugins bundled together (like prior versions of Myeclipse) - especially the integration built in for Maven.


Myeclipse 6.5 is built on Eclipse 3.3 and the Web Tools Project(WTP). The Maven support I am referring to isn't available in the regular Maven IDE plugin. What has been provided with MyEclipse is the ability to create EJB and WEB projects structured around Maven. These EJB/WEB projects are enabled to be deployable to your application server of choice from within the IDE. While this makes life easy for a developer, one still needs to understand a little bit more about how to go about structuring these EJB and WEB projects to create the necessary enterprise artifact or EAR file.
Using the MyEclipse project options, I went ahead and created two projects - FrmkConsoleServices, an EJB project and FrmkWebConsole, a WEB project. Due to the lact of support for creating an Enterprise project (essentially one that creates the EAR file), I created the project - FrmkConsole, that brings together the other two projects or creates the EAR file containing a JAR and WAR. The POM for this EAR project looks like this, which I had to hand-code since MyEclipse does not have a capability to create such an Enterprise project (a feature they need to have in their next version asap ):

<dependencies>
<dependency>
<groupId>com.mycompany.myappfrmk.console</groupId>
<artifactId>console-svcs</artifactId>
<type>ejb</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.mycompany.myappfrmk.console</groupId>
<artifactId>web-console</artifactId>
<type>war</type>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<generateApplicationXml>
true
</generateApplicationXml>
<generatedDescriptorLocation>
${basedir}/target
</generatedDescriptorLocation>
<finalName>FrmkConsole</finalName>
</configuration>
</plugin>

You will also need to control the creation of the various project artifacts, or get the WAR,JAR built before the EAR, and that's the reason for creating FrmkConsoleRoot. It essentially specifies the modules that make up the project and the POM really only contains the following, besides declaring the packaging for it to be of type "pom":

<modules>
<module>../FrmkConsoleServices</module>
<module>../FrmkWebConsole</module>
<module>../FrmkConsole</module>
</modules>

Overall though, I was not particularly happy that I had to create 4 top-level projects for me to create an EAR file. This approach may work for groups of developers working separately on the WEB and Service layer, however the ideal structure here should be one top-level project that has multiple modules under it - in this case 3 (the ear, the war, the jar). If you don't want to use MyEclipse, then I would go with the m2eclipse plugin. This is pretty good and allows you to create projects based on archetypes available. For example an archetype that helps in building a EAR file is "maven-archetype-j2ee-simple". Using m2eclipse, you can select this archetype by building a new Maven project and selecting the "internal" catalog that has these predefined template projects. You essentially want your project structured according to this archetype.At command line you can retrieve it using the following command:
mvn archetype:create -DgroupId=com.mycompany.myapp -DartifactId=my-webapp 
-DarchetypeArtifactId=maven-archetype-j2ee-simple -DarchetypeVersion=1.0

Well, one thing I can say after all that - its not easy to find this information, but at-least its getting there. Once you have your first project setup, subsequent ones should be easy with Maven. Those of you who are used to ant may find all this a little cumbersome initially, but Maven's archetype is exactly what convention over configuration is all about. Give Maven a try, its not all bad....:).

No comments: