Maven build debug
Maven is an automated build project management tool and automates the build process by defining the all the dependencies in pom.xml.Developers encounter a lot of issues while building a maven project in the 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 a number of ways to debug a maven project.
I am listing down the required steps for finding out the real issues.
1. maven debug mode:-
If you are experiencing issues with running maven goals like compile or install, the most informative message will be displayed. 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 occurs 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 perfectly 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 a direct dependency on one module say log4j-1.2.12 and you have another indirect dependency of log4j-1.2.12(coming from spring 2.0.6 which has a dependency for your project). This would be the case of your project having two same modules(log4j) with different versions. This would be a class incompatible issue once your project is started in web server. Then you have to keep the required module in your pom.xml and ignoring another module by specifying exclusion configuration.
To find out the dependency tree for your project, you have to use the below command
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 the 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 a 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 the coolest feature provided by maven with eclipse remote debugging. whenever we started the maven test case execution, you can put breakpoints for stopping the debugger in your test classes for debugging purpose. Once the maven execution is over, eclipse debugger terminate the process.
and another 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