The Maven tip file

9

This is a raw dump of some random Maven tips that I use to debug things that come up with Maven. Instead of stashing these away in a text file I thought maybe someone else could find some use in them.

Debugging Maven Two of the most important command-line options for debugging Maven issues are -e and -X. -e will turn on full exception stack traces if you are seeing an error. Perhaps more useful, -X will turn on Maven debug logging and dump a lot more output about how things are being resolved, full command lines of launched processes, and many other useful tidbits of information to help you get to the bottom of an issue.

Dependencies Maven is famously known for managing dependencies for your project. But it can’t do this without your help – you need to be fully aware what things you depend on both directly (in your pom), indirectly (through parent poms), and transitively (behind the others). The most essential tool in your arsenal here is the dependency plugin.

There are a bunch of useful goals on the plugin but the one I use constantly is mvn dependency:tree. This will show you a tree of all your dependencies, and transitively their dependencies.

Often you’ll find you depend on some other project and the dependencies of that project are screwed up, usually by depending on stuff as a compile-time dependency that really should be “runtime” or “provided” scope. You’ll want to investigate dependency exclusions which let you cut off those nasty transitive dependencies.

The dependency:analyze goal is also pretty useful in tracking which of those dependencies aren’t needed at all.

If you’re using the m2eclipse plugin (and if you’re using Eclipse, you absolutely should be), it actually will show you graphs of the dependencies of your project and their transitive dependencies, including the ability to filter different scopes, etc. Good stuff.

Pom analysis I find the help plugin to be pretty helpful at understanding how poms and parent poms interact to define your project. In particular, I find help:effective-pom, help:effective-settings, help:active-profiles and help:all-profiles all come in handy.

Debugging tests One option I use frequently to skip unit tests altogether is -Dmaven.test.skip=true.

An essential option to know is how to debug a unit test run from Maven with -Dmaven.surefire.debug. This will spawn the test and wait for you to connect with a remote debugger. If you use m2eclipse, it can simplify this a great deal, letting you debug a test more directly.

Hope those help someone…

Comments

9 Responses to “The Maven tip file”
  1. Kyle says:

    Alex, this is a really good list. Now if only you had posted this a year ago when my team first started using Maven. ;-)

  2. mauhiz says:

    As for the Eclipse plugin I think q4eclipse is neater. Have you tried it ?

  3. Mike says:

    > help:effective-pom

    Very cool. Thanks.

  4. Alex says:

    @mauhiz: I have and I think m2eclipse is better.

  5. Good list. For skipping unit tests, I usually prefer -DskipTests, which only skips test execution but not test compilation. By the way, “=true” is redundant in Maven.

  6. Alex says:

    @Peter: Thanks for the tip! That reminds me that I also can’t live without:

    -Dtest=FooTest

    which just runs FooTest. Note that the omission of a package is intentional -> it is unnecessary.

  7. @Alex you should be able to run tests in Eclipse with simple “Run as / JUnit”, but it reminds me that there is an enhancement requesting for “Run as / Maven test” for classes (essentially, an equivalent of -Dtest=FooTest). Also, you may find this little feature handy for debugging tests spawned in a separate VM.

  8. Alex says:

    @Eugene: Yep, I think I mentioned that above, but works great. Cool tip on the feature though – hadn’t seen that one.