How do you debug java annotation processors using intellij?
Preferably using 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.
If running javac directly, you can debug this by specifying the following extra parameters:
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.
If running Ant, add the following to the ANT_OPTS environment variable before running:
-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. IntelliJ instructions for this are here. For Eclipse, here.
This tutorial is written for an Android project. Main module name is "app" as usual. The project contains a submodule called "annotation" which is subdependency of "app". "app" module runs annotation processing with gradle declaration
apt project(':annotation') .
SIMPLE VERSION
(run compilation from terminal and attach from IDE)
[REQUIRED] Add a new project configuration "+" -> "Remote". Check "Single instance only". All other settings are generated automatically. Leave <whole project> as the classpath. Port should be left as the default 5005.
[REQUIRED] Make sure you stop all gradle instances by calling: ./gradlew --stop
[REQUIRED] Run the command : ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac
Run the APT project configuration in debug mode as fast as possible :)
[HINT] We start with an EMPTY gradle.properties file
[HINT] DO NOT USE gradle daemon ( --no-daemon / org.gradle.daemon=false option )
[HINT] Run gradle in debug mode ( org.gradle.debug=true option )
[HINT] Run app's module compilation not the processor's module compilation (app's compilation runs annotation processing!)
We DO NOT normally add any Java compiler settings in Android Studio (i.e. File -> other settings -> Default settings)
EXTENDED VERSION (use gradle.properties)
Add the following to your gradle.properties file:
org.gradle.daemon=false
org.gradle.debug=true
Run the compilation from terminal:
./gradlew :app:clean :app:compileDebugJavaWithJavac
ADVANCED VERSION (just press debug in IDE)
Add a bash script to your project main dir (e.g. compile.sh)
#!/bin/bash
./gradlew :app:clean :app:compileDebugJavaWithJavac &
Remember about the '&' sign for background processing.
Go to APT configuration settings we created in step 1 and add a Before launch configuration. Select Run external tool.
Add the path to the compile.sh script we created earlier.
Warning
Messing up gradle compilation, NullPointer exceptions during compilation etc. sometimes result in AndroidStudio being stuck (frozen on gradle refresh stage). If you cannot stop gradle from the IDE then use this command in the terminal:
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.
Follow these steps, These worked for me on android studio for gradle project:-
1).In gradle.properties add following lines
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
3).Run recently created run configuration APT.
4).Add break point in annotation processing code and build overall project
It is possible to run javac and debug it, as indicated higher. However in my case it was tedious to write the complete classpath, so I wanted to leave this to IDEA. So in the module where I wanted to apply my annotation processor, just create a class with main method.
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). This is the same reason why appservers compiling JSPs need this JAR on their classpath - they need the compiler.
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!
Benefit - classpath is set already by IDEA and used by the "inner" javac, because here it is not a separate process. I believe it's possible to translate it to other IDEs too.
For a Maven project, this post, which explains the following steps in a little more detail, worked for me:
Add "Remote" run configuration and set "port" to 8000.
Issue the command mvnDebug clean install from the project's directory (on the command line).
Run the run configuration. In order to start a new session after the processes quit, repeat from (2).
Remember to run mvn install on the project's dependencies when they change (e.g. if the annotation processor is in a different artifact than the project you are debugging it from).
I found the following resource that can help you: http://code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse
The guy explains step-by-step how to debug annotation processors using Eclipse.
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. Then you can put breakpoints in the Annotation Processor's files.
See Debugging an Annotation Processor in any project.
Disclaimer: I wrote the post.
Debugging an annotation processor with IntelliJ IDEA and Gradle
Set a custom VM option -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.
Create a remote debug configuration with default parameters: Run -> Edit Configurations... -> Add New Configuration (Alt + Insert) -> Remote.
Set breakpoints.
Build with Gradle from the terminal: $ ./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).
Debug the remote debug configuration within the IDE (see step 3): select a suitable remote debug configuration and press Shift + F9.
Hope it helps somebody :)
Related
I have one Jenkins job which does following things
Checks out the Jenkinsfile from github at some location (c:\jenkins\workspace\my_build)
Jenkinsfile checks out java source code to (c:\jenkins\workspace\my_build#2)
mvn clean install
When I run "mvn clean install" on my build machine it works perfectly fine.
But when I run it thorough Jenkisnfile I have few unit test cases which runs when building my project, those units tests are failing with java.io.FileNotFoundException(The system cannot find the path specified) exception/error.
When I ran maven in debug mode(using -X) I found out the workspace path(c:\jenkins\workspace\my_build#2) is being converted to c:\jenkins\workspace\my_build%402 hence maven is unable to find the file which is required for my unit test cases to pass.
How can I fix this issue?
I managed to fix the issue by using a custom workspace. Something like this
ws("c:\jenkins\my_custom_location") {
// git checkout
// mvn clean install
}
Jenkins didn't create any directory with #2 or #3 when using custom workspace.
#user3847894,
You did not fix the issue, merely worked around the problem (avoidance). Now, if you run builds in parallel, they will all use the same workspace, probably with horribly unintended consequences.
You can try choose a different symbol:
hudson.slaves.WorkspaceList
Since: 1.424 Default:
# Description: When concurrent builds is
enabled, a unique workspace directory name is required for each
concurrent build. To create this name, this token is placed between
project name and a unique ID, e.g. "my-project#123".
Or figure out the real problem:
where your system is pulling the wrong character set (ANSI vs UTF-8 ? locale), encoding or something is wrongly "sanitizing the path" (eg: OWASP Sanitizer).
You'd have to provide way more info re: OS, jdk, system and startup parameters, etc to diagnose, list of plugins (maven and Jenkins), so can't help further. Check controller and agent system info (${JENKINS_URL}/systemInfo and ${JENKINS_URL}/computer/myNode/systemInfo) and also see what maven reports in the settings and help:system. On your own; good luck
I use IntelliJ IDEA CE in two environments (CentOS and Ubuntu).
The two share a project via GitHub.
In CentOS, the project is regarded as Java project? with a file build.gradle.
In the other, the project is fully managed as Gradle-Java project.
When I run a main method in CentOS, IDE just runs Java (after some compiles if are required)
On the other hand, when I run the same main method in Ubuntu, IDE activates Gradle and run that.
I think this difference happens from the module structures.. which I have not tried though....
(Am I wrong?)
One question I really want to know here is,
in CentOS, when I run a code, it shows how IDE ran the code in the Run window, like...
java [too many options] foo.target.Hoge
If I copy and paste this line, I can activate foo.target.Hoge anywhere, even where I do not set CLASSPATH because [too many options] contain that.
I always do it (I am afraid if I should not though)
However, in Ubuntu (Gradle-based environment)
the windows show only
time PM: Executing task 'Hoge.main()'...
I can not copy, paste and run it...
I want to know where I can found the actual command activated.
or.... using Gradle.. are there any simpler ways?? like
gradle run Java Hoge ...
When I run a main method in CentOS, IDE just runs Java (after some compiles if are required) On the other hand, when I run the same main method in Ubuntu, IDE activates Gradle and run that.
For Gradle-based projects this behaviour is controlled in Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Build and run using option. When Gradle is set IDE uses Gradle to run and build the project. When IntelliJ is set IDE uses it's own builder and Run/Debug Configurations to run.
Intellij is giving me errors all around for brand new kotlin/spring project and I cannot build or run the project from the IDE.
If I do it from the command line however, there are no issues and I can build and run the app.
'classpath' in 'org.gradle.api.artifacts.dsl.DependencyHandler' cannot be applied to '(groovy.lang.GString)'
'apply' in 'org.gradle.api.plugins.PluginAware' cannot be applied to '(['plugin':java.lang.String])'
Cannot access class 'java.lang.String'. Check your module classpath for missing or conflicting dependencie
Type mismatch.
Required:
java.lang.String
Found:
kotlin.String
Any ideas to what may be the issue?
I tried Kotlin multiplatform JVM type mismatch in InteliJ but doesn't seem to fix the issue.
UPDATE:
Cleared gradle caches, reinstalled Intellij, Import project that was created from start.spring.io with Gradle and Kotlin selected.
Using default gradle wrapper and project jdk (the path says jre)? gives me an error. Open gradle settings just opens the file explorer.
Using default gradle wrapper and machine local JDK same issues with the dependencies from above.
This issue comes up if you set up your own module inside IntelliJ and you think that since you are doing a Kotlin (Maven) project, the SDK should be set to Kotlin. Wrong!
The problem is shown in the first image. The project SDK is set to Kotlin.
Change it to Java. Probably any 8+ Java will be good enough.
This solves the IDE errors and the compiler errors as well.
Unset KOTLIN_HOME and other Kotlin- or Java-related settings you may have in your environment (env to check, unset NAME to unset.)
Then kill any Gradle daemon still running (pkill -f GradleDaemon) and test your Gradle build from the terminal. If all goes well, remove the .idea directory; restart IDEA, making sure to run it without the stray environment variables (for example, launch idea.sh from the terminal where you unset them); and re-import your project, with the choice of using the default Gradle wrapper.
If you need to use standalone Kotlin versions, installed for example through SDKMAN, consider taking the SDKMAN activation lines out of your shell init file (.bashrc for Bash) and into a standalone script (say, ~/bin/sdkman) that will also change your shell prompt (PS1 in Bash) to remind you that you have entered a SDKMAN-managed CLI session.
I'm developing a project using Vert.X framework using Gradle build tool. The problem I have is that breakpoints in IntelliJ simply doesn't work in any way I've tried.
Here is a run configuration for Vert.X which I use in Gradle:
run {
args = [
'run', mainVerticleName,
"-conf", confPath,
"--redeploy=$project.ext.watchForChange",
"--launcher-class=$mainClassName",
"--on-redeploy=$project.ext.doOnChange"
]
}
So the deployment is done using Gradle, runs without any issues, IntelliJ debugger is connected, but breakpoints are not working.
The ways I've tried to make it work:
1) Gradle run configuration. Here is a run configuration for Intellij IDEA:
Tried to use a Remote debugging tool, started application with the following VM options:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
But this didn't work.
2) Application configuration:
In this case I just can't start the project as I get the following message on the startup:
Error: Could not find or load main class io.vertx.core.Launcher
Vert.X Core library is in the classpath and configuration seems to be correct, so can't get were is the problem.
The source code of the project is public and can be found on GitHub:
vertx-gradle-architecture-starter
Vert.X version - 3.4.0. Gradle version - 3.4.1. IntelliJ IDEA version - 2016.3.5. OS - MacOS Sierra 10.12.3.
Interesting fact is when I deploy Vert.X from tests - breakpoints work. Any ideas why breakpoints doesn't work in cases I've described above?
Here are solutions to both issues. Thanks to #CrazyCoder for help on this.
1) run command is run in separate VM. So, to make it work, I've added --java-opts argument to the script:
run {
args = [
'run', mainVerticleName,
"-conf", confPath,
"--redeploy=$project.ext.watchForChange",
"--launcher-class=$mainClassName",
"--on-redeploy=$project.ext.doOnChange",
// used for attaching remote debugger
"--java-opts", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"
]
}
This allows to attach Remote debug configuration on port 8000.
2) By default, Intellij IDEA creates separate modules per source sets, so I had source sets for api_main and api_test modules. After turning off this feature - Application debug run started to work.
This can be turned off in Gradle Settings. Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle: uncheck create separate modules per source set.
This is an IntelliJ IDEA issue - reference.
I had the exact same issue and following worked for me. redeploy, launcher and on-redeploy options are not necessary in intelliJ. if we remove those the debug works after application is up.
run {
args = [
'run', mainVerticleName,
"-conf", confPath
]
}
Are there caches or other environment files that can cause gradle to throw this error?
Exception in thread "main" java.lang.NullPointerException
at org.gradle.wrapper.BootstrapMainStarter.findLauncherJar(BootstrapMainStarter.java:34)
at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:25)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:127)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:58)
I'm building a Jenkins CI server for building android apps. The Jenkins master passes the job off to an AWS linux slave, where it
Wipes the previous workspace
Downloads the github repo
moves into the project folder
./gradlew clean build
Then it throws the exception above.
Other repos using ./gradlew build on this machine.
This repo built prior to: adding additional disk space to the machine and downloading more android SDKs, neither of which should affect gradlew. Attempting to build the exact repo that built, with the same configuration that built still throws the same exception.
I added echoes to the gradlew file to see where the problem happens, and all of them sound prior to the final line:
exec "$JAVACMD" "${JVM_OPTS[#]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$#"
What do I need to do to make this build?
This took some hunting, but here is the solution that I found. Jenkins (by default) does not have access to the $PATH or any default applications (so that the computer can run Ruby 2.2 and your CI jobs can run Ruby 1.0 for example)
Iff you look in the script that is causing the error, it's because it calls a script which calls a script ect, until at some level down (I want to say 7) it calls the diff function. because this dependency was written assuming that your UNIX box knows the baked in applications. Because the Jenkins user calling this function doesn't know that this binary exists, and the way the entire script is written it throws the exception above.
Hope this helps
Something must be messing with your gradle wrapper setup. I would highly recommend the Jenkins Gradle Plugin. It has made my gradle builds trivial. You can specify any path to the build.gradle if it is not in the root workspace.