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

你说的曾经没有我的故事 提交于 2019-12-02 20:37:36

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 according to your process settings):

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

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


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:

  2. The remote configuration acting as a client.

The exec goal will execute your program in a separate process, so the debugger may not be connecting to the right JVM. Instead try using the java goal, e.g.:

mvnDebug install exec:java 

This will execute your program in the same process and hopefully you will hit your breakpoint.

To debug web applications in maven projects using the Intellij Community Edition, you can add a tomcat or jetty plugin to your WAR pom like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8080</port>
                <path>/yourapp</path>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
        </plugin>
    </plugins>
</build>

It's possible if needed to add database drivers like this:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <dependencies>
        <dependency>
           ... your database driver groupId and artifactId ...
        </dependency>
    </dependencies>
</plugin>

Then using these plugins the application can be started in the command line (from the pom directory):

mvnDebug clean install tomcat7:run-war

Or for jetty:

mvnDebug clean install jetty:run-war

With the application running in debug mode from the command line (you don't need to run it from Intellij), do a remote debugging configuration similar to what you posted and the breakpoint should be hit.

If you use Intellij Ultimate Edition then this is not necessary, because you can create a server configuration for Tomcat or any other server and deploy the application in a fully integrated way, with debugging and hot deployment handled transparently.

There is a 30 day trial where you can evaluate this feature and others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!