3 ways to debug maven build : maven debug tutorials

Maven build debug

Maven is automated build project management tool and automates the build process by defining the all the dependencies in pom.xml.Developers encounters lot of issues while building maven project in development phase as well as release phase.we need to know the ways to debug any issues with maven build.I am listing down the different ways to debug the maven build issues.

There are number of ways to debug an maven project.
I am listing down the required steps for findout the real issues.

1. maven debug mode:-
if you are experience with running maven goals like compile or install, mostly informative message will bedisplayed. if any failure happens, These messages are not helpful to find out the exact cause.
In such case, try to run maven with -X flag to give the clear message of where the error is occured and also plugin configuration and detailed class loading information which is very helpful for the coders to find the real cause of an issue. This is perfect suitable debug option for maven plugin coders.
mvn -X  prints debugging messages
 mvn -e shows error messages
Please remember that this is debugging information for what maven do while building your project. This is not for your project debugging. i.e different concept. i will write one more post about the java project debug tips later posts.

This option will start the maven in debug mode. It will give the complete information messages like what are the different dependencies that are loading to your project and what goals are running.

2.dependencies issues in maven:-

In a real time, for maven project build,we have seen that our project has direct dependencies as well as indirect dependencies.

suppose in your project, you have direct dependency on one module say log4j-1.2.12 and you have other indirect dependency of log4j-1.2.12(coming from spring 2.0.6 which has dependency for your project). This would be the case of your project having two same modules(log4j) with different versions. This would be an class incompatible issue once your project is started in webserver. Then you have to keep the required module in your pom.xml and ignoring other module by specifying exclusion configuration.

To find out the dependency tree for your project, you have to use the below command
mvn dependency:tree
This list out all the dependencies for your project in tree explorer format.all these dependencies are downloaded to your local repository.

3.Debugging tests with maven surefire:-

we will use mvn test for running test cases of your maven project. As you know maven surefire plugin is used to run with your unit test of your project.

you have to provide the maven.surefire.debug property with maven test goal
mvn -Dmaven.surefire.debug test

This will run the tests in separate process.This will listen on port 5005. You can attach this process to eclipse for remote debug.
In the eclipse you have to create and configure the maven debug options as follows.
hostname is localhost
and port  is 5005

This is one of coolest feature provided by maven with eclipse remote debugging.
whenever we started the maven test case execution, you can put break points for stopping the debugger in your test classes for debugging purpose. Once the maven execution is over, eclipse debugger terminate the process.

and other way is to not to run the debugger in fork mode meaning is to execute the tests in the same process, we can use

mvn -DforkMode never test

No comments:

Post a Comment

Note: only a member of this blog may post a comment.