ALT logo
Home
Articles
How to Debug a Maven Plugin with Intellij
Alexandre Therrien
By Alexandre Therrien
LinkedIn Github Gmail
Article content
Being able to use Maven's plugins can be enough in most circumstances. However, that changes when you do not understand why the plugin isn't working the way you want it to. In this article, I will show you how to debug a Maven plugin to understand its inner workings. 
1. Requirements
  • Java and Maven setup
  • Some knowledge of Maven and Git
  • Working environment with Intellij
  • Maven plugin's code, found on Github
2. Important Terminology

In this article, I will be referring to the project you are currently working on as 'your project'. The other project, which contains the Maven plugin's code, will be referred to as the 'Maven plugin's project'.

3. Setup the Maven Plugin Project with Your POM Version

If you haven't already done it, get the Maven plugin's code by cloning the project on your local machine.

It is important that the plugin's code corresponds to the version configured in your project's pom.xml file. Normally, there should be tags delimiting versions for the project - check the tags available in the Github project, and switch to the one corresponding to your pom.xml's plugin version.

If you cannot find your version, you can stay on the default 'master' or 'main' branch. See the following section for more details.

4. (Optional) Setup the Maven Plugin Project with the Main Branch

If you haven't already done it, get the Maven plugin's code by cloning the project on your local machine.

Warn
Warning

If you have a settings.xml file that doesn't rely on the Maven central repository, backup the file and deactivate it temporarily (by renaming it) before the next step. You might not be able to pull certain libraries if you rely on that settings.xml file.

Execute

mvn clean install

so maven archives this SNAPSHOT version in the .m2 folder.

Now, go to your project's pom.xml file, and make sure to specify that the plugin will use the SNAPSHOT version that you just compiled (using the <version> tag) by doing the following:

<build> <plugins> <plugin> <artifactId>YOUR_PLUGIN</artifactId> <version>YOUR_VERSION-SNAPSHOT</version> </plugin> </plugins> </build>

You can find the version of your plugin in the Maven plugin's project, in the pom.xml file.

5. Specify the JVM Debug Configuration

Create a 'Remote JVM Debug' configuration for your Maven plugin's project in Intellij. Go to your Run/Debug Configurations screen, and click on the '+' icon. Select that configuration, and modify the port number to what you want (i.e., for this example, 5005).

Visual help
Source: baeldung.com. View of the 'Debug Configurations' screen

Set the following details:

  1. Debugger mode: Attach to remote JVM
  2. Transport: Socket
  3. Host: localhost
  4. Command line arguments for remote JVM: -agentlib:jdwp=transport=dt_socket, server=y, suspend=n, address=*:5005

If you try to start your configuration now, it will give you the error :

Unable to open debugger port (localhost:5005): java.net.ConnectException "Connection refused: connect"

That is because there has to be a process that wants to connect to that port!

6. Setup Your Project to Debug the Maven Plugin

From this point on, have two Intellij windows open: one for your project, and the other for the Maven plugin's project.

Go back to your main project from which you want to debug the maven plugin from.

Go to:

File > Settings > Build, Execution, Deployment > Build Tools > Maven > Runner

Add the 'command line arguments for remote JVM' mentioned above into the field VM options. (-agentlib:jdwp=…)

Then, do a

mvn clean install

(or whatever maven operation that triggers the plugin) to start the maven process. You will notice that it is waiting for activity on port 5005 before continuing its process.

This is when you go back to your Maven plugin project in Intellij (in a different window) and start your 'Remote JVM Debug' configuration with the bug icon.

Now, you'll be able to debug your Maven plugin. Make sure you have put a breakpoint in your Maven plugin code.

7. Conclusion

In this article, we have gone over how to debug a Maven plugin using Intellij. This required us to create debugging configurations for both your project and the Maven plugin's project to allow them to communicate. Once maven is started on your project, it will wait for the Maven plugin's debugging configuration to start, which will allow you to debug the code.

Happy coding!

Alexandre Therrien
By Alexandre Therrien
LinkedIn Github Gmail