Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

5 Maven Tips

October 05, 2010 AT 11:50 AM
  • submit to reddit

I have been working with Maven for 3 years now and over that time I have learned some tips and tricks that help me work faster with Maven. In this article, I am going to talk about 5 of those tips.

  1. Maven rf option
  2. Most of us work in a multi-module environment and it happens very often that a build fails at some module. It's a big pain to rerun the entire the build. To save you from going through this pain, Maven has an option called rf (i.e. resume) from which you can resume your build from the module where it failed. So, if your build failed at myproject-commons you can run the build from this module by typing:
    mvn -rf myproject-commons clean install

  3. Maven pl option
  4. This next Maven option helps you build specified reactor projects instead of building all projects. For example, if you need to build only two of your modules myproject-commons and myproject-service, you can type:
    mvn -pl myproject-commons,myproject-service clean install
    This command will only build commons and service projects.

  5. Maven Classpath ordering
  6. In Maven, dependencies are loaded in the order in which they are declared in the pom.xml. As of version 2.0.9, Maven introduced deterministic ordering of dependencies on the classpath. The ordering is now preserved from your pom, with dependencies added by inheritence added last. Knowing this can really help you while debugging NoClassDefFoundError. Recently, I faced NoClassDefFoundError: org/objectweb/asm/CodeVisitor while working on a story where cglib-2.1_3.jar was getting loaded before cglib-nodep-2.1_3.jar. And as cglib-2.1_3.jar does not have this CodeVisitor class I was getting the error.

  7. Maven Classifiers
  8. We all know about qualifiers groupId, artifactId, version that we use to describe a dependency but there is  fourth qualifier called classifier. It allows distinguishing two artifacts that belong to the same POM but were built differently, and is appended to the filename after the version. For example, if you want to build two separate jars, one for Java 5 and another for Java 6, you can use classifier. It lets you locate your dependencies with the further level of granularity.
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
    <classifier>jdk16</classifier>
    </dependency>
  9. Dependency Version Ranges
    Have you ever worked with a library that releases too often when you want that you should always work with the latest without changing the pom. It can be done using dependency version changes. So, if you want to specify that you should always work with the version above a specified version, you will write:
  10. <version>[1.1.0,)</version>

    This line means that the version should always be greater than or equal to 1.1.0. You can read more about dependency version ranges at this link.

          These are my five tips. If you have any tips please share.

Comments

Ankur Gupta replied on Tue, 2010/10/05 - 10:06pm

Hmmm 'rf' and 'pl' options are nice. Have explored 'classifier' in the past and it works well even with Maven v2.0.7

Martijn Verburg replied on Wed, 2010/10/06 - 6:00am

Useful tips, have passed onto my team - nice work!

Nicolas Richeton replied on Wed, 2010/10/06 - 7:38am

Nice tips, will try -rf on the next build failure :-)

Christian Schli... replied on Fri, 2010/10/08 - 4:21am

Very good tips - thank you! It would be nice to discuss the classifier a bit more since I could not find much information in the POM reference. When is it best to use a classifier rather than appending a suffix to the artifactId. So when should one do

<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>X.Y</version>
<classifier>classifier</classifier>

rather than

<groupId>groupId</groupId>
<artifactId>artifactId-classifier</artifactId>
<version>X.Y</version>

Shekhar Gulati replied on Sat, 2010/10/09 - 4:35am in response to: IllegalException

Its good that you found these tips useful. I think your question is best answered by this Sonatype Classifer blog. Hope this helps.

Tire Works replied on Thu, 2011/08/04 - 9:03am

This installment in the 5 things series looks beyond Maven's build features, and even its basic tools for managing the project life cycle, delivering 5 tips that will improve the productivity and ease with which you manage applications in Maven. Thanks for this tips very informative. -Tire Works

Gar Labs replied on Sun, 2011/10/23 - 10:06am

Nice tips. Very practical and simple. I can't wait for another round of tips coming from you. Thanks man. - GAR Labs

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.