[英]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:我可以在各种情况下重现它:
When I press debug (with maven configuration install exec:exec -DforkMode=never
)当我按调试时(使用 maven 配置
install exec:exec -DforkMode=never
)
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中的远程调试配置:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=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>
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
启动的进程。
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=y
和server=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"
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:所以基本上你需要两个“运行/调试”配置才能工作:
exec:exec
with the system properties and JDWP agent configuration:exec:exec
Maven 配置: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.