Tip #15: How To Debug Unit Tests During Maven Builds
- by ByronNevins
It must be really really hard to step through unit tests in a debugger during a maven build. Right?
Wrong!
Here is how i do it:
1) Set up these environmental variables:
MAVEN_OPTS=-Xmx1024m -Xms256m -XX:MaxPermSize=512mMAVEN_OPTS_DEBUG=-Xmx1024m -Xms256m -XX:MaxPermSize=512m -Xdebug (no line break here!!) -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9999MAVEN_OPTS_REG=-Xmx1024m -Xms256m -XX:MaxPermSize=512m
2) create 2 scripts or aliases like so:
maveny.bat:
set MAVEN_OPTS=%MAVEN_OPTS_DEBUG%
mavenn.bat:
set MAVEN_OPTS=%MAVEN_OPTS_REG%
To debug do this:
run maveny.bat
run mvn install
attach your debugger to port 9999 (set breakpoints of course)
When maven gets to the unit test phase it will hit your breakpoint and wait for you.
When done debugging simply run mavenn.bat
Notes
If it takes a while to do the build then you don't really need to set the suspend=y flag.
If you set the suspend=n flag then you can just leave it -- but only one maven build can run at a time because of the debug port conflict.