繁体   English   中英

IntelliJ&Eclipse:调试由Maven exec:exec启动的Java应用程序

[英]IntelliJ & Eclipse: debug Java application started by Maven exec:exec

在我的项目中,我有一个配置,例如

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions> 
        <execution>
            <id>default-cli</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <commandlineArgs>${additionalExecArgs} -jar ${project.build.directory}/someDir/some.jar</commandlineArgs>
                <executable>java</executable>
                <classpathScope>runtime</classpathScope>
            </configuration>
        </execution>
    </executions>
</plugin>

使用Maven exec:exec启动可执行JAR。 默认情况下, additionalExecArgs属性为空。

在NetBeans中,我可以通过在nbactions.xml文件中指定以下操作来实现此目标:

<action>
    <actionName>run</actionName>
    <goals>
        <goal>exec:exec</goal>   
    </goals>
</action>

为了在NetBeans中调试此目标,我向nbactions.xml文件中添加了以下操作:

<action>
    <actionName>debug</actionName>
    <goals>
        <goal>exec:exec</goal>   
    </goals>
    <properties>
        <jpda.listen>true</jpda.listen>
    </properties>
    <activatedProfiles>
        <activatedProfile>debug</activatedProfile>
    </activatedProfiles>
</action>

因此,除了运行exec:exec之外,还将NetBeans(?)属性jpda.listen为true并激活Maven配置文件debug

我还将以下Maven配置文件添加到了我的POM中:

<profile>
    <id>debug</id>
    <properties>
        <additionalExecArgs>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}</additionalExecArgs>
    </properties>
</profile>

使用此配置,我可以简单地从项目上下文菜单/工具栏运行调试操作。

但是请注意jpda.address Maven属性。 NetBeans似乎填充了此属性。

如果我尝试在IntelliJ或Eclipse中使用调试配置文件运行exec:execjpda.address收到错误消息,可能是因为未设置jpda.address Maven属性。

例如,我在IntelliJ中遇到以下错误:

/usr/lib/jvm/java-8-oracle/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:42300,suspend=y,server=n -Dmaven.home=/usr/share/maven -Dclassworlds.conf=/usr/share/maven/bin/m2.conf -Dfile.encoding=UTF-8 -classpath /usr/share/maven/boot/plexus-classworlds-2.x.jar:/opt/idea-IC-141.1010.3/lib/idea_rt.jar org.codehaus.classworlds.Launcher -Didea.version=14.1.3 -Dmaven.repo.local=/home/someUser/.m2/repository exec:exec -P debug
Connected to the target VM, address: '127.0.0.1:42300', transport: 'socket'
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building test-application 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.4.0:exec (default-cli) @ test-application ---
ERROR: transport error 202: connect failed: Verbindungsaufbau abgelehnt
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [debugInit.c:750]
FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

端口随每次呼叫而变化。

我尝试将jpda.address替换为固定的地址/端口(例如127.0.0.1:8000),但我也遇到了相同的错误,现在在NetBeans中也是如此。

那么,如何在IntelliJ和Eclipse中获得与NetBeans类似的行为,如何直接使用Maven运行配置调试exec:exec目标,而不必诉诸于远程调试器配置等?

听起来您可以使用Before launch Maven目标在intellij中设置Remote debug配置。

设置如下:

Run/Debug Configurations ->添加Remote -> Before Launch添加-> Maven >指定正确的模块和目标。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM