IntelliJ IDEA 13 调试器不会在 Maven 项目的 java 中的断点处停止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22229088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:28:23  来源:igfitidea点击:

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

javadebuggingmavenintellij-ideabreakpoints

提问by mirelon

I have a breakpoint on a line where is the System.out.println("test")command. I believe that the command is reached by execution because I see the printed string "test". But the breakpoint is ignored.

我在System.out.println("test")命令所在的行上有一个断点。我相信该命令是通过执行达到的,因为我看到了打印的字符串“test”。但是断点被忽略了。

Breakpoint is a red circle all the time, without a tick or cross. I think this is an issue when IDEA thinks the class is not loaded, while it is, because the command is executed.

断点一直是一个红色圆圈,没有勾号或叉号。我认为这是一个问题,当 IDEA 认为该类未加载时,因为该命令已执行。

I can reproduce it in various circumstances:

我可以在各种情况下重现它:

  1. When I press debug (with maven configuration install exec:exec -DforkMode=never)

  2. Remote debugging - I run maven goal in debug mode in the console:

    mvnDebug install exec:exec -DforkMode=never

    or

    mvnDebug install exec:exec

    remote debug configuration in IDEA:

    • Arguments for running remote JVM:
      -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
    • For JDK 1.4.X:
      -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
    • Transport: Socket
    • Debugger mode: Attach
    • Host: localhost
    • Port: 8000
  1. 当我按下调试(使用 maven 配置install exec:exec -DforkMode=never

  2. 远程调试 - 我在控制台中以调试模式运行 maven 目标:

    mvnDebug install exec:exec -DforkMode=never

    或者

    mvnDebug install exec:exec

    IDEA中的远程调试配置:

    • 运行远程 JVM 的参数:
      -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
    • 对于 JDK 1.4.X:
      -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
    • 运输方式:插座
    • 调试器模式:附加
    • 主机:本地主机
    • 端口:8000

In both cases the debugger only prints "Connected to the target VM, address: 'localhost:8000', transport: 'socket'"

在这两种情况下,调试器只打印“Connected to the target VM, address: 'localhost:8000', transport: 'socket'”

I have also tried File > Invalidate Caches / Restartand clean build, but the breakpoint is still ignored.

我也尝试过File > Invalidate Caches / Restart清理构建,但仍然忽略断点。

Configuration:

配置:

Ubuntu 13.10
IntelliJ IDEA Ultimate build 133.944
Apache Maven 3.0.4
Java version: 1.7.0_51, vendor: Oracle Corporation
OS name: "linux", version: "3.11.0-17-generic", arch: "amd64", family: "unix"

Ubuntu 13.10
IntelliJ IDEA Ultimate build 133.944
Apache Maven 3.0.4
Java 版本:1.7.0_51,供应商:Oracle Corporation
操作系统名称:“linux”,版本:“3.11.0-17-generic”,架构:“amd64”,系列: “Unix”

EDIT: relevant part of pom.xml:

编辑:pom.xml 的相关部分:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <configuration>
    <executable>java</executable>
      <arguments>
        <argument>-D--secret--.server.configuration=/usr/local/etc</argument>
        <argument>-classpath</argument><classpath/>
        <argument>com.--secret--.Server</argument>
      </arguments>
  </configuration>
</plugin>

采纳答案by Anthony Accioly

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:

你正在运行它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 启动的进程exec:exec

Maven

马文

Change your exec:execgoal to enable remote debugging. I'm using suspend=yand server=n, but feel free to configure the JDWP Agentas you please:

更改exec:exec目标以启用远程调试。我正在使用suspend=yand server=n,但可以随意配置JDWP 代理

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

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

This will notbe passed directly to the maven JVM, instead it will be passed to exec.argswhich will be used by exec:exec:

不会直接传递给 Maven JVM,而是传递给 JVMexec.args将被使用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 Remoteconfiguration (again I'm using a Listenstrategy. You should adjust it according to your process settings):

创建一个Remote配置(我再次使用Listen策略。您应该根据您的流程设置对其进行调整):

enter image description here

在此处输入图片说明

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

现在切换断点并调试远程配置。使用上面的设置,它将等到您的过程开始:

enter image description here

在此处输入图片说明

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

最后运行exec:exec上面的行并随意调试您的应用程序:

enter image description here

在此处输入图片说明



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

所以基本上你需要两个“运行/调试”配置才能工作:

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

    enter image description here

  2. The remote configuration acting as a client.

  1. exec:exec带有系统属性和 JDWP 代理配置的Maven配置:

    在此处输入图片说明

  2. 充当客户端的远程配置。

回答by Jonathan

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

exec目标将在一个单独的进程中执行程序,因此调试器可能无法连接到正确的JVM。而是尝试使用java目标,例如:

mvnDebug install exec:java 

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

这将在同一进程中执行您的程序,并希望您能达到断点。

回答by Angular University

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:

要使用 Intellij 社区版调试 Maven 项目中的 Web 应用程序,您可以像这样向 WAR pom 添加一个 tomcat 或 jetty 插件:

<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):

然后使用这些插件可以在命令行中启动应用程序(从 pom 目录):

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.

通过命令行以调试模式运行应用程序(您不需要从 Intellij 运行它),执行类似于您发布的内容的远程调试配置,并应命中断点。

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.

如果您使用 Intellij Ultimate Edition,那么这不是必需的,因为您可以为 Tomcat 或任何其他服务器创建服务器配置,并以完全集成的方式部署应用程序,调试和热部署透明处理。

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

30 天的试用期,您可以在其中评估此功能和其他功能。