简体   繁体   English

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

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

I have a breakpoint on a line where is the System.out.println("test") command.我在System.out.println("test")命令所在的行上有一个断点。 I believe that the command is reached by execution because I see the printed string "test".我相信该命令是通过执行达到的,因为我看到了打印的字符串“test”。 But the breakpoint is ignored.但是断点被忽略了。

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 )当我按调试时(使用 maven 配置install exec:exec -DforkMode=never

  2. Remote debugging - I run maven goal in debug mode in the console:远程调试 - 我在控制台中以调试模式运行 maven 目标:

    mvnDebug install exec:exec -DforkMode=never

    or或者

    mvnDebug install exec:exec

    remote debug configuration in IDEA: IDEA中的远程调试配置:

    • Arguments for running remote JVM:运行远程 JVM 的参数:
      -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
    • For JDK 1.4.X:对于 JDK 1.4.X:
      -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
    • Transport: Socket运输方式:插座
    • Debugger mode: Attach调试器模式:附加
    • Host: localhost主机:本地主机
    • Port: 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 / Restart and clean build, but the breakpoint is still ignored.我也试过File > Invalidate Caches / Restart和 clean build,但断点仍然被忽略。

Configuration:配置:

Ubuntu 13.10 Ubuntu 13.10
IntelliJ IDEA Ultimate build 133.944 IntelliJ IDEA Ultimate 版本 133.944
Apache Maven 3.0.4 Apache Maven 3.0.4
Java version: 1.7.0_51, vendor: Oracle Corporation Java 版本:1.7.0_51,供应商:Oracle Corporation
OS name: "linux", version: "3.11.0-17-generic", arch: "amd64", family: "unix"操作系统名称:“linux”,版本:“3.11.0-17-generic”,arch:“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>

Update 2021: 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.较新版本的 IntelliJ IDEA(使用 2020.3 测试)现在可以自动检测maven exec配置并添加适当的选项以启用调试。 See IDEA-189973 for further info.有关更多信息,请参阅IDEA-189973 Thanks @Gili for opening a ticket for this functionality back in 2018.感谢@Gili早在 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).尽管如此,我的原始答案对于旧版本的 IntelliJ、远程调试或在使用某些 Maven/Gradle 插件进行调试时仍然有用,这些插件 Fork VM 并需要手动向下游传递调试选项(相应地调整配置)。


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 Maven exec:exec .通过一些“启动魔法”,我们可以调试由 Maven exec:exec启动的进程。

Maven马文

Change your exec:exec goal to enable remote debugging.更改您的exec:exec目标以启用远程调试。 I'm using suspend=y and server=n , but feel free to configure the JDWP Agent as you please:我正在使用suspend=yserver=n ,但可以随意配置JDWP 代理

-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 :不会直接传递给 Maven JVM,而是会传递给exec.args ,该exec.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 Remote configuration (again I'm using a Listen strategy. You should adjust it accordingly):创建一个Remote配置(我再次使用监听策略。你应该相应地调整它):

在此处输入图片说明

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:最后运行上面的exec:exec行并随意调试您的应用程序:

在此处输入图片说明


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:带有系统属性和 JDWP 代理配置的exec:exec Maven 配置:

在此处输入图片说明

  1. 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. exec目标将在单独的进程中执行您的程序,因此调试器可能没有连接到正确的 JVM。 Instead try using the java goal, eg:而是尝试使用java目标,例如:

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:要使用 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 trial where you can evaluate this feature and others.30 天的试用期,您可以在其中评估此功能和其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 IntelliJ IDEA 13 社区版中创建 java 项目 - Can't create java project in IntelliJ IDEA 13 Community Edition 想法intellij maven项目无法做到 - idea intellij maven project can't make 调试器不会停留在 Intellij IDEA 中的源代码处 - Debugger doesn't stop at source code in Intellij IDEA IntelliJ IDEA为什么我在导入对话框中看不到该方法? (已经在项目中,并且如果将复制粘贴放入类中,则可以访问)。 Maven + Java项目 - IntelliJ IDEA why I don't see the method in import dialog? (have in project and if put with copy-past to class, it's accessible). Maven+Java project IntelliJ IDEA 调试器不适用于 Grails 项目 - IntelliJ IDEA Debugger isn't working on a Grails Project Intellij-调试器在Maven项目中不起作用 - Intellij - Debugger not working in maven project 为什么IntelliJ IDEA无法在Maven项目中将Oracle数据库与Java代码连接起来? - Why IntelliJ IDEA doesn't connect oracle database with java code in maven project? intellij 想法,maven spring-boot:运行不:不停止 - intellij idea, maven spring-boot:run doesn:'t stop 如何使用 Maven 在纯 Java 项目中尝试 Dagger 2 - intellij IDEA - How to try Dagger 2 with pure java Project using Maven - intellij IDEA intellij 想法:如何调试 java:fx maven 项目? - intellij idea : how to debug a java:fx maven project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM