3 ways to debug maven build: maven debug tutorials

Maven is an automated build project management tool that automates the build process by defining all the dependencies in pom.xml.

Developers encounter a lot of issues while building and running a maven project in the development phase as well as the release phase.

This article explains multiple ways how to debug any issues with maven build. You can check my other posts on maven commands

There are many ways to debug a maven project.

Following are different ways to debug the maven build issues.

  • command line using -x' or—debugor-e`.
  • mvn dependency tree
  • debugging with the maven surefire plugin

I am listing down the required steps for finding out the real issues.

maven debug mode

If you are experiencing issues with running maven goals like compile or install, the information 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 the maven command with the option -X, -e or --debug flag to give a clear message of where the error occurs, plugin configuration, and detailed class loading information which is very helpful for the coders to find the real cause of an issue.

It is a perfectly suitable approach for maven-plugin coders.

mvn -X  prints debugging messages
or
mvn -e shows error messages
mvn -e shows error messages

Please remember that this is debugging information for what maven does while building your project.

This is not for your project debugging. i.e different concepts. 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.

Dependencies issues in maven

In real-time, for the 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 causes incompatible issues in class loading during the project is started in the webserver. Then you have to keep the required module in your pom.xml and ignore another module by specifying the exclusion configuration.

To find out the dependency tree for your project, you have to use the below command.

mvn dependency:tree

This lists out all the dependencies for your project in tree explorer format. all these dependencies are downloaded to your local repository.

You can find out which version to keep and exclude as seen below if we want to exclude log4j from spring dependency, You can use the exclusions and exclusion tags in the dependency tag.

<dependencies>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.1</version>
            <exclusions>
              <exclusion>
                <groupId>log4j</groupId>
                <artifactId>jetty</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
        </dependencies>

Debugging tests with maven surefire

We will use mvn test for running test cases of your maven project. As you know the maven surefire plugin is used to run 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 debugging.

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 features 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 purposes. Once the maven execution is over, the eclipse debugger terminates the process.

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

mvn -DforkMode never test