How to debug a Gradle build.gradle file (in a debugger, with breakpoints)? - debugging

Is there a tool that will allow me to set breakpoints in a build.gradle file and step through tasks in a debugger?
Note: I believe that I'm asking a different question than similar stackoverflow questions about debugging Gradle plugins, where (presumably) the intent is to step through custom Groovy or Java plugin code located in a separate file. I want to set a breakpoint in a Gradle task in a simple build.gradle file, like...
task example {
println "I want to set a breakpoint here"
}
...so that when I run gradle example I can inspect the context in a debugger.
(For those who would point me to IntelliJ...although JetBrains' website advertises that they support debugging Gradle scripts in IDEA UI, AFAICT this is untrue, as this was reported broken in IDEA13 EAP and hasn't been fixed in IDEA14. See Debugging Gradle build files in Intellij / Android Studio )
Is there any debugging tool that allows me to set a breakpoint in a build.gradle file, or is there something about the Gradle DSL that makes it fundamentally impossible to set breakpoints in a task such as my example, above?

There is the easier way:
just add in your command line -Dorg.gradle.debug=true --no-daemon
For example:
gradle nameOfTask -Dorg.gradle.debug=true --no-daemon
Then you should start your IDE and run remote debugging with localhost port 5005, that all.
Gradle is waiting to you, because standard option server=y
org.gradle.debug
When set to true, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 to the JVM command line and will suspend the virtual machine until a debugger is attached.
Link to docs

IntelliJ 2018.2 added the ability to debug Gradle scripts in a similar fashion to how you might run/debug other projects. You can see the announcement in the release notes here.
Here is a screenshot of some of the documentation from 2018.2:
It does not yet support the kotlin-dsl (see gradle/kotlin-dsl/issues/39).

Personnaly I do this when I need to debug build scripts:
Inside you terminal do
export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Then run your build
gradle clean install
Finally put some breakpoints and launch the remote debug configuration inside your IDE on the port 5005 and you’re good to go!

export GRADLE_OPTS does not work for me.
Try this:
1 Add remote debug config
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
2 add breakpoint in gradle
3 ./gradlew your task --stacktrace -Dorg.gradle.daemon=false -Dorg.gradle.debug=true
4 Attach debug in studio
click icon
Then the breakpoint stops !!
If console is not goiong, click this in debug pannel in studio.

After reading various answers here the following steps will help you debug build.gradle to being able to break and investigate variables inside any custom task. I am using Eclipse remote debugging facilities
Place this simple code where you want to break: try {throw new RuntimeException('Break');} catch (RuntimeException e) {}
As recommended start your task with gradle mytask -Dorg.gradle.debug=true --no-daemon in the command prompt (don't have to do it in Eclipse)
In Eclipse do Run -> Add Java Exception Breakpoint, choose RuntimeException and click "OK"
Again in Eclipse go to Run -> Debug Configurations -> Remote Java Application and create new configuration that listens on localhost:5005. Name it whatever you want. Select a project that contains build.gradle you are debugging. Click Apply and Debug
At this point the execution will start but will pause at the Exception-throwing line. And you can then start looking at your variables in the `Debug -> Variables" view, inspect the stacktrace, step through the code etc.
No magic, alas, you will not see anything highlighted in build.gradle but you can pretty much guess where you are at
Obviously on subsequent runs you don't need step 3 and in 4 you can reuse previously created configuration
If you want to use this in the multiple places simply create a method, use different type of exception and feel free to enhance this idea in any way possible
For example:
void halt() {
try {
throw new RuntimeException('Break');
} catch (RuntimeException e) {
print('Paused')
}
}
task iterateDeclaredDependencies {
doLast {
Object configs = configurations.all
halt();
print(configs)
}
}

I use
set JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8989"
then 2 gradle processes are listening :
Listening for transport dt_socket at address: 8989
Listening for transport dt_socket at address: 8989
I can connect to both of them with 2 remote debug launch configurations in Eclipse although it's the same port.

Related

Can't run more than one LibGDX game

I have developed a game with LibGDX library.
I am using socket programming libraries as a module in the project.
I am planing that every process of my LibGDX game will be a client for my server.
I structured it.
But I can't run more than one game at a time to test multiplayer functionality.
When I run the game first, everything is okay, but the second time I click run anything run until the first instance is closed.
I think that's because of Gradle.
I tried to change "Build and run using IntelliJ Idea" from Gradle settings, but this time I had an exception. It cannot read my assets file.
How can I solve this problem?
enter image description here
I have solved it with a Gradle command.
./gradlew desktop:run
Same problem here. Desktop app starts but gradle build never ends and all following builds have to wait until I exit the app or stop the gradle build process.
Setting the "allow multiple instances" option in the Application run configuration did not help.
Making a gradle configuration with "desktop:run" has the same effect and "allow parallel run" did not help.
This started after the Android Studio updating to version "Arctic Fox 2020.3.1"
Starting from command line with "./gradlew desktop:run" in several terminal windows works, but is laborious.
Update with a temporary solution (until the error is fixed):
Create a JAR-Application run configuration. Build the jar in the "before launch" section with Gradle task "desktop:dist" and point to the jar in "Path to JAR".
Hints:
You can safely ignore all the "xmas" strings in the image :)
"XstartOnFirstThread" is only necessary on Mac OS.
On some recent "intellij/android studio" versions is a bug that breaks "desktop:dist" the solution is to include "duplicatesStrategy = DuplicatesStrategy.INCLUDE" in desktop/build.gradle:
task dist(type: Jar) {
manifest {
attributes 'Main-Class': project.mainClassName
}
dependsOn configurations.runtimeClasspath
duplicatesStrategy = DuplicatesStrategy.INCLUDE
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
Enable Allow multiple instances option in Run/Debug Configuration:

Gradle Script plugin debugging in IntelliJ IDEA

I can debug build.gradle script, but it seems IDE can't properly wire into script plugins of Gradle to debug them. I am using IntelliJ IDEA 2020.3.3.
Its reproducible in the below example project:
There is
build.gradle containing mainTask, and
scriptPlugin.gradle a script plugin with scriptPluginTask task.
And following configuration that enables Gradle script debugging in IntelliJ IDEA.
Now if I run mainTask in debug mode, execution actually stops at the breakpoint set in build.gradle file in mainTask on line 5.
But if I try to debug task that is imported through script plugin, namely scriptPluginTask from scriptPlugin.gradle file then execution just runs through without stopping at the breakpoint on line 3.
Question
Do I need to set additional configuration options to enable debugging Gradle script plugins from IntelliJ IDEA?
Or is this feature currently not supported at all? That means if I would want to debug script plugin I would actually need to copy paste whole content to the build.gradle file itself and debug it there.

Trying to debug an application that starts up via an "exec" gradle task

I have a gradle task (type - exec). In this task I start a WildFly server using a standart bat file (standalone.bat). The war file is already in deployments beforehand, so the server starts up and everything works fine.
The problem appears when I need to debug the actual application on the server. Running the gradle task in a debug mode doesn't help, as it can only debug the actual gradle task itself and not the application that it starts.
I know, there is a solution to this problem using an additional configuration that would allow me to debug a remote java application. However, the goal for me right now is to move eveything onto gradle tasks and not have any configurations set up in my IDE.
I need to be able to run a gradle exec task that would start up my server and than to debug whatever application it deploys. Is there a way to do this? Thank you in advance.
Note: I've never used Wildfly
Ultimately you'll want to edit standalone.bat so that you include an additional arguments to the java.exe invocation. Take a look inside standalone.bat many applications include a line commented out that you can uncomment to enable debugging.
Eg sample argument to add to the java.exe call
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044
You may wish to take a copy of standalone.bat which you call standalone-debug.bat which you invoke via gradle

When running a gradle unit test from command line, how can I get a debugger?

I'm running unit tests from the gradle command line (because running them from IntelliJ was causing problems for some reason.)
So I'd like to turn on a debug port for remote debugging from the unit test. How can I do that? Is there a way to send the -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y into the unit test itself? I'm using Gradle daemon mode so I'm not sure how that all works and can't find any documentation about it.
Actually it turned out that there's no need to set the jvmArgs manually. Instead you just need to invoke (docs):
gradle someTestTask --debug-jvm
and it will stop execution and wait for debugger connection. Now when this process hangs, go to IntelliJ, Eclipse (or other IDE) and set up an remote debugging configuration (remember to set source module - red arrow):
After it's set, run the debugger and you're done. Breakpoints may be set in IntelliJ directly.
Previous answer below:
test {
jvmArgs '-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y'
}

How to set system properties in IntelliJ IDEA 13 gradle task?

I have a Spring Boot project with gradle build tool. The JDBC url, username and password are kept in a property file which is not part of application it's a external property file, the path of the property file is taken from system properties as follows.
export _JAVA_OPTIONS=-DdatabaseConfiguration=db.properties
It is working if I run the application from terminal using gradle bootRun, but when I try to run from Intellij IDEA 13 gradle tasks its not working, the property value is null.
I tried the VM options in Run/Debug Configuration as in the below screen shoot its not working either
How can the JAVA_OPTIONS can be set in Intellij IDEA 13 gradle tasks.
This is because every time you use the Gradle tool window to kick off tasks in IntelliJ, it creates/overwrites the launch configuration for that task.
Basically, I've had to run from the Gradle tool window just once. Then I go into the failed Launch Config (shown in question) and enter the system property in the VM options. From there on out, I need to use that Launch Config to execute the task instead of the Gradle tool window.
Update: Even better solution:
Preferences->Build, Execution, Deployment->Build Tools->Gradle->Gradle VM options
Add your system properties there (i.e. -Dappengine.sdk.root=/opt/google/google-cloud-sdk/platform/appengine-java-sdk)
Doing this will keep them from getting overwritten/lost in the Launch configs that the Gradle tool window generates.
Another thing to note is that using the Gradle tool window causes the commands to be run without access to Environment Variables. This can cause a lot of problems with builds that depend on these env vars.
I ran into this today with the appengine-gradle-plugin and had to put
-Dappengine.sdk.root=/opt/google/google-cloud-sdk/platform/appengine-java-sdk
in the VM options because it was not seeing the env vars. From the command line, it picks up the env vars and works fine. This worked for my appengineRun task.
But it does not work for appengineUpdate since that gives another error caused by lack of env vars: Toolkit not found: apple.awt.CToolkit

Resources