Blog

Migrating your project to Maven 2 – Part I

26 Feb, 2006
Xebia Background Header Wave

Maven 2 is out for quite some time now. This week I decided to try to migrate a project we work on to maven 2. Here’s a part of what I ran into while migrating to maven 2:

The project.xml in maven 1.x is renamed and called the pom.xml in Maven 2. The project.xml of our project looked sort of like this:


  
    
      jar
    
    
      plugin
    
  
  
    src/java
    test/java
    
      
      
      
    
    
  
  
    maven-javadoc-plugin
  

I left out the complete list of dependencies and only put in the relevant tags.When comparing the project.xml (https://maven.apache.org/maven-1.x/reference/project-descriptor.html) with the pom.xml (https://maven.apache.org/maven-model/maven.html) I immediately notice the huge difference in size. The definition of the pom.xml is somewhat 4 times bigger then the project.xml…. I can’t help thinking that this can’t be a good thing, why is it so big?
I decide to start with the dependencies. The new cool feature in maven 2 is Transitive Dependencies (https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html). Let’s say you are using Hibernate in your project. With maven 1.x you had to define all the jars that Hibernate depends on in your project.xml. With maven 2 this is no longer necessary as maven 2 resolves those dependencies for you. This cleans up the pom.xml in comparison with the project.xml. The dependencies are declared the same as you would do in the project.xml:


  org.hibernate
  hibernate
  3.1.1
  jar

Almost all dependencies can be downloaded from https://www.ibiblio.org/maven2/, this is also the default repository used in maven 2. Pay attention to the groupId’s, for some jars minor changes are made and they are located somewhere else. For instance the groupId of Hibernate is now org.hibernate instead of hibernate.
In maven 1.x you could add your own repository locations in the project.properties. As of maven 2 project.properties no longer exists, and you have to declare your own repository locations in the pom.xml:


  jibx.sf.net
  JiBX repository
  https://jibx.sf.net/maven2

Also when you do not want your local repository placed in
{user.home}/.m2/repository you have to create a settings.xml and place it in
{user.home}/.m2. See https://maven.apache.org/maven-settings/settings.html for the complete description of the settings.xml file.


  e:/documents/data/maven2-repo

It takes a while to migrate all the dependencies, most of the time I spend is trying to figure out where to find the jars, but eventually I found all the dependencies (expect for the easymockclassextension-1.2.jar, so I installed that one locally).
In maven 1.x you also defined the plugins as dependencies, in maven 2 the plugins reside on different levels of the pom.xml. We have plugins needed for build time, such as the maven-jibx-plugin, and plugins used for reporting, such as the maven-pmd-plugin. The maven-jibx-plugin is defined in the build tag:


  
    org.jibx
    maven-jibx-plugin
    1.1
    
      
        
          bind
        
      
    
  

The pmd-plugin definition goes within the reporting tag:


  
    
      org.apache.maven.plugins
      maven-pmd-plugin
    
  

The pom.xml now looks roughly like this:



  
    
  
  
    
  
  
    src/java
    test/java
    target/classes
    target/test-classes
    
    
    
  
  
    
  
  
  
    false
    target
    
      
  

I left out all the dependency names for readability. Total lines in pom.xml: 285, total lines in project.xml 491. Wow, although the pom.xml looked like a huge thing when I first looked at it, it actually caused my pom.xml to shrink. Score: 1-0 for maven 2 with the Transitive Dependencies!
So, now I have migrated all the dependencies and plugins, it’s time to test the new pom.xml and compile the source. The new name of the maven “executable” is now mvn. I run mvn compile and it all compiles, this is too good to be true!
Next step is to test the project, let’s run mvn test. Ah finally something goes wrong ;-)… I get a NoSuchMethodException on some utility class (TestSetupUtils) that I use in my test classes. Strange error, the class it is complaining about is compiled and in the testOutputDirectory as I declared. What’s wrong then?
It seems that the surfire-plugin tries to run my utility class as a TestCase, this is because as by default one of the patterns in the include list is: **/Test*.java. Doh!
In maven 1.x you could define your excludes in the unitTest tag. Now you have to define your test excludes within the declaration of the surfire-plugin that runs my tests, which sounds logical to me.
Here’s how to add classes to the exclude list:


  org.apache.maven.plugins
  maven-surefire-plugin
  
    
      **/TestSetupUtils*.java
    
  

After this addition to the pom.xml the tests run and we are one step closer to maven 2.
However I am far from done, I still have to gather all the reporting plugins we use now and figure out how to deal with the maven.xml file we have. As from maven 2 the maven.xml file no longer exists as everything should be a plugin now. We have quite some ant code in maven.xml, most part is for generating a distribution file. Also I have just migrated one project that is part of a multiproject setup, so I expect that will be a nice challenge.
To summarize this part of the migration I have to say that I really like the Transitive Dependency feature that cleans up the pom.xml in respect to the project.xml. Another good thing is that almost all the dependencies can be found at the https://www.ibiblio.org/maven2/, so there is no need to Google around for other repository locations.
There are also two things that I noticed in general that I didn’t like:

  • The first thing is that the documentation could be better, for instance I hoped to find a clear migration guide from maven 1 to maven 2, but the only thing I found on the maven site was this: https://maven.apache.org/maven1.html#changed, which is by far not enough and I had to figure out a lot of possible common questions, like the exclude list of the tests.
  • The second very annoying thing is that the links on the site of the description of the pom.xml, https://maven.apache.org/maven-model/maven.html, do not work in firefox (1.5.0.1). So be prepared to scroll a lot on that page.

That’s all for now, I’ll blog later about the next steps to maven 2.
Lars

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts