Saturday, February 8, 2014

How to debug Maven Applications in Intellij IDEA

Before I start, I would like to emphasize that this article is based on a Java console application and you need to make minor justifications in order to make it work for web apps in general.

I am assuming that you have a maven project (check out Getting Started with Maven) to make sure that project structure is valid.

To be able to debug a maven application, you need to set a Maven Debug Configuration in your Intellij IDEA project. If you do not know anything about existence of such thing, do not be scare as I will follow you through the steps.

First, in order to create a maven debug configuration, go to Run and click on Edit Configurations:


After that the Run/Debug Configuration window will pop up. Click on the "+" sign at the top-left part of the window, find Maven and click on it:


Now, you will see your Maven Configuration window. You can set a name  for it based on the goal of this debug or even name of the project. Just right below the name textbox, there are three tabs: Parameters, General and Runner. For this tutorial, we stick with the project settings options for the last two tabs and just consider the options inside the first Tab. As you can see in the following screen capture, the project directory has already been set for you. The most important task here is to set the command line of this configuration plus the profile of which you would like to debug your application.




Since we have a console application here, we will use Exec Maven Plugin which exists in Maven version 1 onwards.

exec:java -Dexec.mainClass=MainClass -Dexec.args=args

The -Dexec.mainClass will be set to the entry point of the application, here is the class with the main method, and -Dexec.args will be a set of arguments which will be passed to that main method. Here is an example:


exec:java -Dexec.mainClass=TopologyMain -Dexec.args=src/main/resources/words.txt

Now after you have set your debug breakpoint, set your Maven configuration in the dropdown of Select Run/Configuration and click on Debug (Or alternatively, go to Run and click on Debug and in the new window, select your Maven Config file):


After that, the Exec Plugin will compile your code and detach to the debugger process, like the following:



Happy Debugging your Maven Code!

2 comments: