繁体   English   中英

如何使用Netbeans调试使用Ant的自定义build.xml编译的代码?

[英]How to use Netbeans to debug the code compiled using Ant's custom build.xml?

我有自己的build.xml文件,包含几个目标,用于编译和运行我的Java项目。 以下是这一部分的相关部分:

<path id="libpath">
    <fileset dir="${lib.dir}" includes="**/*.jar" />
</path>

<patternset id="resources">
    <include name="relative/path/to/resources/" />
</patternset>

<path id="resourcespath">
    <fileset dir="${src.dir}">
        <patternset refid="resources" />
    </fileset>
</path>

<target name="compile">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${classes.dir}" />
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="libpath" includeantruntime="false" debug="true" debuglevel="lines,vars,source" />
    <copy todir="${classes.dir}">
        <path refid="resourcespath" />
    </copy>
</target>

<target name="debug" depends="compile">
    <java fork="true" classname="${main-class}">
        <sysproperty key="java.library.path" path="${dist.dir}"/>
        <classpath>
            <pathelement location="${classes.dir}" />
            <path refid="libpath" />
        </classpath>
    </java>
</target>

我想要做的就是通过运行build.xmldebug目标,使用Netbeans debug ${classes.dir}的编译代码。 目前,此目标启动已编译的应用程序,但没有机会在断点处停止。 我知道Netbeans会生成默认的build-impl.xml文件,但是这个文件太大而且我很难理解。 这就是为什么我想知道是否可以使用Netbeans IDE来调试Ant使用我自己的build.xml文件编译的Java代码。

在Eclipse中使用Ant脚本时,还有类似的调试Java代码的问题,但我可以看到提出的解决方案是Eclipse特定的。

Sergey提出的方法有效,但每次调试项目时都必须执行以下操作:

  1. Debug Main Project按钮( Ctrl+F5 ),Netbeans将等待debbuger附加;
  2. 从Netbeans主菜单中选择“ Debug ”>“ Attach Debugger ... ”,在出现的对话框中指定调试器的参数,然后按“ OK ”按钮或从工具栏的调试按钮的下拉列表中选择以前使用的debbuger(例如Attach to 5432 );
  3. 调试您的应用程序然后关闭它( Shift+F5不会像往常一样终止应用程序,它只会完成调试会话)。

在我看来,使用“ Attach Debugger ... ”的更好方法是选择SocketListen连接器:

  1. 执行前面步骤链中的步骤2(选择Listen to 5432 ) - 调试器将等待应用程序启动(请注意, jvmarg的子选项server必须设置为n - 请参阅远程调试常见问题解答 )。
  2. Debug Main Project按钮( Ctrl+F5 )。
  3. 调试应用程序,然后关闭它或通过按Shift+F5同时终止调试会话。

无论如何,这种方法也很不舒服,特别是如果您习惯于只需按Ctrl+F5或按相应的工具栏按钮即可开始调试。 每次从下拉菜单中选择所需的调试器都很烦人。

所以这是最好的解决方案 - 直接从Ant的debug目标启动调试器。 就我而言,它看起来像这样:

<target name="debug" depends="compile">
    <nbjpdastart addressproperty="jpda.address" name="MyProjectName" transport="dt_socket">
        <classpath>
            <pathelement location="${classes.dir}" />
            <path refid="libpath" />
        </classpath>
    </nbjpdastart>
    <java fork="true" classname="${main-class}">
        <sysproperty key="java.library.path" path="${dist.dir}" />
        <classpath>
            <pathelement location="${classes.dir}" />
            <path refid="libpath" />
        </classpath>
        <jvmarg value="-Xdebug" />
        <jvmarg value="-Xnoagent" />
        <jvmarg value="-Djava.compiler=none" />
        <jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}" />
    </java>
</target>

要获得更多信息,请查看创建目标以调试Java SE应用程序

Eclipse主题的建议也适合你。

  1. 使用下一个JVM参数(将它们添加到java目标调用):

    <jvmarg value="-Xdebug" />
    <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432" />

  2. 在netbeans中,使用Debug-> Attach Debugger和端口5432

暂无
暂无

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

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