Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle: change default port from 5005

I want to debug some JVM instances that are running at the same time. I know that I can run gradle using --debug-jvm so that the JVM will wait until I start the IDE debugger so that it connects to the JVM but it uses port 5005 by default. That's fine for debugging one instance of JVM... but if I want to debug more than one instance, I'll need to define a different port from 5005. How can I achieve this with gradle?

like image 572
eftshift0 Avatar asked Nov 16 '18 19:11

eftshift0


People also ask

How do I change my Gradle from org to Java home?

Right click on the deploy or any other task and select "Open Gradle Run Configuration..." Then navigate to "Java Home" and paste your desired java path. Please note that, bin will be added by the gradle task itself.

What is Gradle_user_home?

GRADLE_USER_HOME. Specifies the Gradle user home directory (which defaults to $USER_HOME/. gradle if not set). JAVA_HOME. Specifies the JDK installation directory to use for the client VM.


2 Answers

In my case I wanted to debug a specific file, so I included the following code in build.gradle:

task execFile(type: JavaExec) {
    main = mainClass

    classpath = sourceSets.main.runtimeClasspath
    
    if (System.getProperty('debug', 'false') == 'true') {
        jvmArgs "-Xdebug", "-agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=y"
    }

    systemProperties System.getProperties()
}

and I can run with:

gradle execFile -PmainClass=com.MyClass -Dmyprop=somevalue -Ddebug=true

The custom execFile task receives:

  • -PmainClass=com.MyClass: the class with the main method I want to execute (in the script, main = mainClass)
  • -Dmyprop=somevalue: a property whose value be retrieved in the application calling System.getProperty("myprop") (in the script, systemProperties System.getProperties() was needed for that)
  • -Ddebug=true: a flag to enable debugging on port 8787 (in the script, see the if condition, and also address=8787, but the port could be changed, and this flag name also could be changed). Using suspend=y the execution is suspended until the debugger is attached to the port (if you don't want this behaviour, you could use suspend=n)

For your use case, you could try to apply the logic behind the line jvmArgs ... to your specific task (or use tasks.withType(JavaExec) { ... } to apply to all tasks of this type).

Using this solution, don't use the --debug-jvm option because you may receive an error about the property jdwp being defined twice.

Update (2020-08-10)

To make sure that the code runs only when I execute the task execFile explicitly (so as to not run when I just build gradle, for example), I changed the code to:

task execFile {
    dependsOn 'build'

    doLast {
        tasks.create('execFileJavaExec', JavaExec) {
            main = mainClass
            classpath = sourceSets.main.runtimeClasspath

            if (System.getProperty('debug', 'false') == 'true') {
                jvmArgs "-Xdebug", "-agentlib:jdwp=transport=dt_socket,address=*:8787,server=y,suspend=y"
            }

            systemProperties System.getProperties()
        }.exec()
    }
}

See more at: Run gradle task only when called specifically

like image 172
Lucas Basquerotto Avatar answered Oct 12 '22 08:10

Lucas Basquerotto


You could modify GRADLE_OPTS environment variable and add standard Java debugger syntax e.g. to use port 8888:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8888
like image 32
Karol Dowbecki Avatar answered Oct 12 '22 06:10

Karol Dowbecki