Unable to expand JAVA_OPTS post gradle-7.2

defaultJvmOpts is no longer working in gradle-7.2

Could be because of this: Arbitrary code execution via specially crafted environment variables · Advisory · gradle/gradle · GitHub

But what is the solution ?

It is working, it just might work differently.
“what is the solution” probably depends on what you try to do.

I want to expose few ports & properties; like:

ext.getJvmArgs = { jmxPort, jdwpPort, initialRamPercentage = 50.0, maxRamPercentage = 70.0 ->
    return ["-XX:InitialRAMPercentage=$initialRamPercentage",
            "-XX:MaxRAMPercentage=$maxRamPercentage",
            "-XX:+UseStringDeduplication",
            // Force G1GC as the GC collector. In a container environment, GC is selected by docker based on CPU and Memory.
            "-XX:+UseG1GC",
            "-Duser.timezone=\"UTC\"",
            "-Dcom.sun.management.jmxremote",
            "-Dcom.sun.management.jmxremote.authenticate=false",
            "-Dcom.sun.management.jmxremote.ssl=false",
            "-Dcom.sun.management.jmxremote.local.only=false",
            "-Dcom.sun.management.jmxremote.port=$jmxPort",
            "-Dcom.sun.management.jmxremote.rmi.port=$jmxPort",
            "-Djava.rmi.server.hostname=127.0.0.1",
            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$jdwpPort"]
}
task task_name(type: CreateStartScripts) {
    mainClass = "com.a.b.c.d.e"
    applicationName = "sample_name"
    int jmxPort = 2609
    int jdwpPort = 2611
    float initialRamPercentage = 30.0
    float maxRamPercentage = 60.0
    defaultJvmOpts = project.getJvmArgs(jmxPort, jdwpPort, initialRamPercentage, maxRamPercentage) + ['-XX:ErrorFile=/vdt/hs_err_pid%p.log']
    outputDir = new File(project.buildDir, 'scripts')
    classpath = jar.outputs.files + configurations.runtimeClasspath
}

Besides that using ext is usually a bad idea and bad practice, when you just want a local variable.
But from a cursory look, it should probably work.
Can you provide a full MCVE that shows the problem?

build.gradle:

ext.getJvmArgs = { jmxPort, jdwpPort, initialRamPercentage = 50.0, maxRamPercentage = 70.0 ->
    return ["-XX:InitialRAMPercentage=$initialRamPercentage",
            "-XX:MaxRAMPercentage=$maxRamPercentage",
            "-XX:+UseStringDeduplication",
            // Force G1GC as the GC collector. In a container environment, GC is selected by docker based on CPU and Memory.
            "-XX:+UseG1GC",
            "-Duser.timezone=\"UTC\"",
            "-Dcom.sun.management.jmxremote",
            "-Dcom.sun.management.jmxremote.authenticate=false",
            "-Dcom.sun.management.jmxremote.ssl=false",
            "-Dcom.sun.management.jmxremote.local.only=false",
            "-Dcom.sun.management.jmxremote.port=$jmxPort",
            "-Dcom.sun.management.jmxremote.rmi.port=$jmxPort",
            "-Djava.rmi.server.hostname=127.0.0.1",
            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$jdwpPort"]
}

subproject.gradle:

task task_name(type: CreateStartScripts) {
    mainClass = "com.a.b.c.d.e"
    applicationName = "sample_name"
    int jmxPort = 2609
    int jdwpPort = 2611
    float initialRamPercentage = 30.0
    float maxRamPercentage = 60.0
    defaultJvmOpts = project.getJvmArgs(jmxPort, jdwpPort, initialRamPercentage, maxRamPercentage) + ['-XX:ErrorFile=/vdt/hs_err_pid%p.log']
    outputDir = new File(project.buildDir, 'scripts')
    classpath = jar.outputs.files + configurations.runtimeClasspath
}

Error:

Forwarding from 127.0.0.1:2609 -> 2609
Forwarding from [::1]:2609 -> 2609
Handling connection for 2609
E0705 15:15:18.230959   13229 portforward.go:406] an error occurred forwarding 2609 -> 2609: error forwarding port 2609 to pod 51482420046fe7d4c65e09edfb0dacf3185d9f235f8155330a2ae711140dbf2e, uid : failed to execute portforward in network namespace "/var/run/netns/cni-52a11e9e-af7a-0cc9-dc6e-19dd6309cafd": failed to connect to localhost:2609 inside namespace "51482420046fe7d4c65e09edfb0dacf3185d9f235f8155330a2ae711140dbf2e", IPv4: dial tcp4 127.0.0.1:2609: connect: connection refused IPv6 dial tcp6: address localhost: no suitable address found 
E0705 15:15:18.231266   13229 portforward.go:234] lost connection to pod
Handling connection for 2609
E0705 15:15:18.232054   13229 portforward.go:346] error creating error stream for port 2609 -> 2609: EOF

Its not actually a local variable. I have defined getJvmArgs in main build.gradle and using it in multiple sub-projects .gradle

build.gradle …

That’s not really qualifying for C of MCVE :wink:

Its not actually a local variable. I have defined getJvmArgs in main build.gradle and using it in multiple sub-projects .gradle

Ah, I see.
Different bad practice but still one. :smiley:
To centralize and reuse build logic, better use convention plugins in buildSrc or an included build, for example implemented as precompiled script plugins. :slight_smile:

But besides that, how does the generated start script look like?
Maybe also output the result of your getJvmArgs call in your build script.

How can I actually check that ?

How can I actually check that ?

Open it in a text editor?

Unable to connect visualvm to java application after 7.3 gradle upgrade

That does not seem like there is anything that should be releated to the mentioned change, you only put static stuff in there, from the point of view of executing.