Tuesday, December 27, 2011

Debugging with Maven

For Non-Forked Process
The way to debug any project using maven is to use "mvnDebug" command line tool instead of "mvn". So, if I want to debug a tomcat based project, I would do:
mvnDebug tomcat:run

which would enable the debugging on the JVM and would wait for the debugger to connect to the vm:

By default, the JDWP port that mvnDebug listens is on 8000. 

The next step is to connect to this vm through the debugger in Eclipse - 
Go to Run->Debug Configurations->Remote Java Application and create a new "launch configuration" along these lines:

Click on debug and eclipse should connect to the maven vm and the tomcat should start up at this point. 

For Forked Process
The above approach will however not work for forked processes like JUnit tests - tests by default are forked by maven. 
There are two workarounds for debugging JUnit tests:
To prevent forking of Junit tests, this can be done using a forkMode parameter this way:
mvnDebug test -Dtest=GtdProjectDaoIntegrationTest -DforkMode=never

The second workaround is to use the "maven.surefire.debug" property:
mvn -Dmaven.surefire.debug test -Dtest=GtdProjectDaoIntegrationTest

This would, by default, start up the debugger at port 5005. A variation of this is to explicitly specify the port where the debugger is to be started and with additional JDWP options:
mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" test



  1. One quick hint.

    The actual name of this binary is "mvnDebug"; it doesn't matter if you're running on Windows since Windows is not case sensitive, but if you're running on Unix you have say "mvnDebug". If you updated this in your post, it would definitely save a little time for the Unixers in the audience.