[英]how do you debug java annotation processors using intellij?
How do you debug java annotation processors using intellij? 如何使用intellij调试java注释处理器?
Preferably using IDEA IntelliJ. 最好使用IDEA IntelliJ。 I tried setting a breakpoint inside the processor and running but it did not break.
我尝试在处理器内部设置一个断点并运行,但它没有破坏。
If you really need to debug an annotation processor, it might be better to run the annotation processor from the command line rather than within your IDE with debugging enabled and attach to that using your IDE's debugger. 如果您确实需要调试注释处理器,最好从命令行运行注释处理器,而不是在启用了调试的IDE中运行注释处理器,并使用IDE的调试器附加到该处理器。
If running javac
directly, you can debug this by specifying the following extra parameters: 如果直接运行
javac
,可以通过指定以下额外参数来调试它:
javac -J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 ... (usual javac parameters go here)
If running Maven, use mvndebug
instead of the standard mvn
command - Maven runs the compiler in-process. 如果运行Maven,请使用
mvndebug
而不是标准的mvn
命令 - Maven在进程中运行编译器。
If running Ant, add the following to the ANT_OPTS
environment variable before running: 如果运行Ant,请在运行之前将以下内容添加到
ANT_OPTS
环境变量中:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
With all these executions, the javac, Ant or Maven process will wait for you to attach your debugger before it actually starts executing. 通过所有这些执行,javac,Ant或Maven进程将等待您在实际开始执行之前附加调试器。 IntelliJ instructions for this are here .
IntelliJ的说明就在这里 。 For Eclipse, here .
对于Eclipse, 这里 。
Follow these steps, These worked for me on android studio for gradle project:- 按照这些步骤,这些在android studio for gradle项目上为我工作: -
1).In gradle.properties add following lines 1)。在gradle.properties中添加以下行
org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
2).Edit Build COnfiguration and add Remote Configration 2)。编辑构建配置并添加远程配置
3).Run recently created run configuration APT. 3).Run最近创建了运行配置APT。
4).Add break point in annotation processing code and build overall project 4)。在注释处理代码中添加断点并构建整个项目
This tutorial is written for an Android project . 本教程是为Android项目编写的。 Main module name is "app" as usual.
主模块名称像往常一样是“app” 。 The project contains a submodule called "annotation" which is subdependency of "app".
该项目包含一个名为“annotation”的子模块,它是“app”的子顺序。 "app" module runs annotation processing with gradle declaration
apt project(':annotation')
. “app”模块使用gradle声明
apt project(':annotation')
运行注释处理。
SIMPLE VERSION (run compilation from terminal and attach from IDE) SIMPLE VERSION (从终端运行编译并从IDE连接)
<whole project>
as the classpath. <whole project>
保留为类路径。 Port should be left as the default 5005 . ./gradlew --stop
./gradlew --stop
停止所有gradle实例 ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac
./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac
EXTENDED VERSION (use gradle.properties ) 扩展版 (使用gradle.properties )
org.gradle.daemon=false org.gradle.debug=true
./gradlew :app:clean :app:compileDebugJavaWithJavac
ADVANCED VERSION (just press debug in IDE) 高级版本 (只需按IDE中的调试)
#!/bin/bash ./gradlew :app:clean :app:compileDebugJavaWithJavac &
Remember about the '&' sign for background processing. 请记住后台处理的“&”符号。
Warning 警告
Messing up gradle compilation, NullPointer exceptions during compilation etc. sometimes result in AndroidStudio being stuck (frozen on gradle refresh stage). 搞乱gradle编译,编译期间的NullPointer异常等有时会导致AndroidStudio卡住(在gradle刷新阶段冻结)。 If you cannot stop gradle from the IDE then use this command in the terminal:
如果您无法从IDE 停止gradle ,请在终端中使用此命令:
ps -A | grep gradle | awk '{ print $1; }' | xargs kill -9
Turning off debug option during project refresh sometimes helps Android Studio to come back to the right track. 在项目刷新期间关闭调试选项有时可以帮助Android Studio回到正确的轨道。
It is possible to run javac
and debug it, as indicated higher. 可以运行
javac
并对其进行调试,如图所示。 However in my case it was tedious to write the complete classpath, so I wanted to leave this to IDEA. 但是在我的情况下编写完整的类路径是很乏味的,所以我想把它留给IDEA。 So in the module where I wanted to apply my annotation processor, just create a class with main method.
因此,在我想要应用我的注释处理器的模块中,只需使用main方法创建一个类。
public static void main(String[] args) {
com.sun.tools.javac.Main.main("-proc:only",
"-processor", "my.pkgs.MyAnnotationProcessor",
"my/pkgs/any/ClassIWantProcess.java");
}
For this to work you need to add $JAVA_HOME/lib/tools.jar
to your SDK's JARs (by default it is not there). 为此,您需要将
$JAVA_HOME/lib/tools.jar
到SDK的JAR中(默认情况下不存在)。 This is the same reason why appservers compiling JSPs need this JAR on their classpath - they need the compiler. 这就是编译JSP的appservers在其类路径上需要这个JAR的原因 - 他们需要编译器。
Then just set proper working directory for your run configuration (so the relative path to the java file is correct), set your break-point into the processor and debug at your will! 然后只需为您的运行配置设置正确的工作目录(因此java文件的相对路径是正确的),将您的断点设置为处理器并根据您的意愿进行调试!
Benefit - classpath is set already by IDEA and used by the "inner" javac, because here it is not a separate process. 好处 - 类路径已由IDEA设置并由“内部”javac使用,因为这里它不是一个单独的过程。 I believe it's possible to translate it to other IDEs too.
我相信也可以将它翻译成其他IDE。
Annotation processing occurs during compilation, so normal debugging won't work. 注释处理在编译期间发生,因此正常调试将不起作用。 If you want to debug it in the context of you project, you can use IntelliJ remote debugging, while having Gradle or Maven in debug mode.
如果要在项目的上下文中调试它,可以使用IntelliJ远程调试,同时在调试模式下使用Gradle或Maven。 Then you can put breakpoints in the Annotation Processor's files.
然后,您可以在Annotation Processor的文件中放置断点。
See Debugging an Annotation Processor in any project . 请参阅在任何项目中调试注释处理器 。
Disclaimer: I wrote the post. 免责声明:我写了这篇文章。
I found the following resource that can help you: http://code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse 我找到了以下可以帮助您的资源: http : //code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse
The guy explains step-by-step how to debug annotation processors using Eclipse. 这家伙逐步解释了如何使用Eclipse调试注释处理器。
For a Maven project, this post , which explains the following steps in a little more detail, worked for me: 对于Maven项目, 这篇文章更详细地解释了以下步骤,对我有用:
Add "Remote" run configuration and set "port" to 8000. 添加“远程”运行配置并将“端口”设置为8000。
Issue the command mvnDebug clean install
from the project's directory (on the command line). 从项目目录(在命令行上)发出命令
mvnDebug clean install
。
Run the run configuration. 运行运行配置。 In order to start a new session after the processes quit, repeat from (2).
要在进程退出后启动新会话,请从(2)开始重复。
Remember to run mvn install
on the project's dependencies when they change (eg if the annotation processor is in a different artifact than the project you are debugging it from). 记得在项目更改时对项目的依赖项运行
mvn install
(例如,如果注释处理器与您正在调试它的项目位于不同的工件中)。
-Dcompiler.process.debug.port=5005
: press Ctrl + Shift + A and select Edit Custom VM Options... in the list of actions to add a custom VM option then restart the IDE. -Dcompiler.process.debug.port=5005
:按Ctrl + Shift + A并在操作列表中选择编辑自定义VM选项...以添加自定义VM选项,然后重新启动IDE。 $ ./gradlew --no-daemon -Dorg.gradle.debug=true clean build
(it's okay if the execution of the command is frozen, don't terminate a process). $ ./gradlew --no-daemon -Dorg.gradle.debug=true clean build
(如果命令的执行被冻结,则不行,不要终止进程)。 Hope it helps somebody :) 希望它有助于某人:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.