M2Eclipse, Maven, Eclipse and Managing Java Project with dependencies

Using Version Control System (henceforth VCS) has now become an essential part of the development life-cycle. One cannot visualize life when it comes to managing project/product releases without having well established VCS. As per my experience (and since got chance to work with only VSS and SVN, I feel SVN is the better choice – simple, easy and most important free!!!).

Well but there is one problem with both the listed VCS, both of them work pretty well on Text based files and are not meant for storing non-textual files. In day to day development environment where developers write thousands lines of code, they also make use of already written libraries to reduce the development effort and use what is already tested & proven. (In Java world the libraries are available in JAR file format). While libraries are in compiled format, using VCS for storing them doesn’t sound to be good idea. In fact when it comes to following mavenised structure of branches, tags and trunks, the decision to store JAR files becomes critical and SVN seems to be not a right choice to store them. Unnecessarily copying JAR files in tags and branches would simply end up increasing repository size. Hence using special repositories like Apache Archiva for JAR files along with Maven POM scripts seems to be right choice.

While one can handcode the POM script using any text-based editor, it would be good to use User Interface to manage the Scripts and locate the Maven artifacts like plugins, dependencies etc. Here comes the rescue from M2Eclipse (sonatype), which provides Eclipse plugins to manage POM scripts. Well if you think, it just provides a form based interface to manage scripts and does nothing, then you must have a look at the features provided by M2Eclipse plugins.

Most of the time, I see developers running here and there to find out the required libraries for the project to get compile and run. The reasons could be the libraries are not stored in VCS or they are not aware of which version should be used. The chaos and time spend in resolving such issues is pretty high and unnecessary. Maven addresses these issues with the help of POM scripts which can be versioned as per release and one can be assured of versions to be used in project. But Maven alone can’t help you to manage libraries when it comes to using POM scripts while developing application in IDE like Eclipse. It cannot add libraries to project on its own and define classpath. Surely you need a plug-in which can manage the project dependencies using Maven Scripts and also free you from configuring them in project. Fortunately there are plugins which can help you to accomplish the same. Add following URL to Eclipse update manager and install the Maven Plug-in http://m2eclipse.sonatype.org/sites/m2e/

It won’t be possible to explain all the features provided by this plug-in in this article, there are so many sections and configurations available that one can write a book for their explanation. Lets understand the important sections required to use this plug-in in Eclipse. Once the plug-in is installed, you should see “Maven” as one of the item in Eclipse > Window > Preferences window.

Eclipse Preferences for Maven

Eclipse Preferences for Maven

The next step is to configure the Maven installation in Plug-in and it can be done in Preferences window using Maven > Installations and click on Add button. Point to the directory location where you have unzipped/installed Maven (I am using Maven 3 along with Apache Archiva).

The last step is to configure the Maven settings file in Eclipse Plug-in, so that Plug-in can use the settings that you have customized in settings.xml rather than using predefined settings. This can be done using Maven > User Settings and then select the settings.xml file. (In my case the file is available in Maven installation folder > conf. It is recommended to use local repository server like Apache Archiva as after the Eclipse maven plug-in is configured, it starts to index the repository to tune the search operation. If one uses default settings, the plug-in will query repo1.maven.org everytime it starts and that will take too much of time).

Once above configuration is done, you should be able to use Maven Eclipse plugin for your project. Create a Project using Eclipse > File > New > Project. Note that this project should not be associated with any builder and hence I have selected “Project” instead of “Java Project”, Maven plug-in will automatically configure the required nature and builders for this project. Next step is to right click on created project and select New > Other option. Search for “Maven”, you should see following options.

Maven options

Maven options

Select “Maven POM File” and click “Next”. The dialog will ask to configure POM script with options like artifactId, groupId etc. Enter the required values and choose “Jar” as packaging option. A POM.xml file will be created in your project. Double click on the POM.xml file and Eclipse will now open “Maven POM Editor”. You will find many tabs below editor screen to configure various POM Script configurations. Go to “Plugins” section and in “Plugins” panel, click Add button. You should now see a dialog box. Enter “maven-compiler” and the dialog must list matching Maven Plugins to the entered text.

Maven Plug-ins

Maven Plug-ins

Select “2.3.2 – maven-compiler-plugin…” from given options and click OK. Now go to pom.xml tab to open the script in XML text editor mode and add following configuration to maven-compiler-plugin.


	1.6
	1.6

This is required to indicate which Java Compiler you want to use, Maven will then configure the require JRE for your project. Now create following folders one below the other “src > main > java” and then right click on project, locate “Maven > Enable Dependency Management”. Once this option is selected, you should now see that Maven will automatically configure

Enable Dependency using Maven Plug-in

Enable Dependency using Maven Plug-in

1. Source Folder
2. JRE
3. Change project icon to indicate it is Maven and Java Project.
4. A library “Maven Dependencies”

Now I assume that you know basics of Maven, so go ahead and add dependencies and you should see that all those libraries are now available in “Maven Dependencies” library. You don’t have to explicitly add any JAR file for your project. (For e.g. In my POM.xml file I added “commons-logging” dependency and Maven plug-in has successfully imported the Jar in project.)

Imported Dependency

Imported Dependency

Now you can test your project by creating Java files under the package “src/main/java”. You can also execute other Maven goals on project by simply right clicking on project and from Run As options choose required option (Maven build, Maven clean, Maven package etc.)

The definite advantage of this approach is that now developers doesn’t have to run for locating required JAR files, one just need project source and its related POM.xml file. The Maven plug-in will automatically configure the required setting to compile and run your project. But still there is one problem when you want to develop Web Project and run it in Eclipse WTP environment (Pre-configured servers like JBoss, Tomcat etc in Eclipse environment). The WTP will not be able to identify the projects created using above steps. Well sonatype has also thought about it and has created one more plug-in to solve this problem. I will provide the details about same in next article.

Be Sociable, Share!

Leave a Comment.