Monday, December 27, 2010

Managing dependencies in Maven

Managing dependencies in Maven, particularly managing the transitive dependencies and versions of the dependencies used can sometimes become a daunting task. Thankfully, there are some maven plugins and other resources to help in this. Just thought of sharing them in this post.
  1. Intro to maven dependency management - An intro to maven dependency mechanism can be found here (http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html). This article gives an introduction about transitive dependencies, dependency scopes and dependency management in maven. This is a must read for any maven beginner.
  2. Maven dependency plugin - You can find the goals and documentation for it here (http://maven.apache.org/plugins/maven-dependency-plugin/). The goals that I use frequently are dependency:resolve (resolves the dependencies and displays the versions used), dependency:tree (resolves the dependencies and displays them in tree structure - an easy way to find out which dependency brings which transitive dependency etc.,), dependency:copy-dependencies (copies all the dependencies including transitive dependencies to a specified location). dependency:analyze could be useful too.
  3. Maven versions plugin - You can find the goals and documentation for it here (http://mojo.codehaus.org/versions-maven-plugin/). The goals that I use frequently are versions:display-dependency-updates (tells which dependencies use the latest versions and which ones have newer versions available - could be very useful to see how stale your pom is), versions:use-latest-versions (searches pom for dependencies which have newer versions and replace them with latest version - could be useful to bulk update your pom to use latest version of all dependencies).
  4. Maven Repository Browser - This is a website (http://www.mvnbrowser.com/index.html), which I just encountered today while trying to search for a missing dependency in my pom. POM report (http://www.mvnbrowser.com/pom-report.html) seems interesting. You can copy paste your POM or just the dependencies section and this would tell you what are the available versions for all dependencies in your POM and if you are using the latest one or not. It also lists what other dependencies are in your classpath. Seems like a simple UI version of what maven dependency and versions plugin does. Apart from this, it also lists the licenses involved. I have used this website very little as I just found it, but it seems useful.
I just mentioned the goals I use most. These plugins have many other useful goals as well, which you might want to explore. Try these maven goals in your maven projects and see for yourself the power of these maven plugins. Hope this post introduced you some new things to manage your maven dependencies and pointed you to some good resources.