IntelliJ IDEA 13 debugger don't stop on breakpoint in java for maven project

Update 2021:

Nowadays, on most situations, debugging should work out of the box.

Newer versions of IntelliJ IDEA (tested with 2020.3) can now auto-detect maven exec configurations and add the proper options to enable debugging. See IDEA-189973 for further info. Thanks @Gili for opening a ticket for this functionality back in 2018.

Nevertheless my original answer bellow can still be useful for older versions of IntelliJ, Remote Debugging or to debug while using certain Maven / Gradle plugins that Fork the VM and require debugging options to be manually passed downstream (adjust configuration accordingly).


My solution:

Considering that you have a program that depends on system properties:

package com.mycompany.app;


public class App {

    private static final String GREETING = System.getProperty("greeting", "Hi");

    public static void main(String[] args) {
        int x = 10;
        System.out.println(GREETING);
    }
}

And you are running it with exec:exec:

mvn exec:exec -Dexec.executable=java "-Dexec.args=-classpath %classpath -Dgreeting=\"Hello\" com.mycompany.app.App"

With some "inception magic" we can debug the process started by Mavenexec:exec.

Maven

Change your exec:exec goal to enable remote debugging. I'm using suspend=y and server=n, but feel free to configure the JDWP Agent as you please:

-agentlib:jdwp=transport=dt_socket,server=n,address=127.0.0.1:8000,suspend=y`

This will not be passed directly to the maven JVM, instead it will be passed to exec.args which will be used by exec:exec:

mvn exec:exec -Dexec.executable=java "-Dexec.args=-classpath %classpath -agentlib:jdwp=transport=dt_socket,server=n,address=127.0.0.1:8000,suspend=y -Dgreeting=\"Hello\" com.mycompany.app.App"

IntelliJ IDEA

Create a Remote configuration (again I'm using a Listen strategy. You should adjust it accordingly):

enter image description here

Now toggle your breakpoints and Debug your remote configuration. Using the settings above it will wait until your process starts:

enter image description here

Finally run the exec:exec line above and debug your application at will:

enter image description here


So basically you need two "Run/Debug" configurations for this to work:

  1. A Maven configuration for exec:exec with the system properties and JDWP agent configuration:

enter image description here

  1. The remote configuration acting as a client.