I’m using the application plugin and am creating custom start scripts for my main class. At runtime, that main class needs to be aware of the application home directory where it’s being installed to via installDist
as I’m bundling some files and directories with the distribution by adding some custom content to the distributions {}
stanza. Hence I’m using the defaultJvmOpts
parameter to pass the $APP_HOME
variable which is defined in the start script template and the resulting script generated from it: defaultJvmOpts = ['-Dfoo=$APP_HOME']
. The $
sign gets escaped in the process, so I have been using a hack that worked for years to remove the escaping character using a doLast
action. See this minimal build script for a working example: gradle-scripts-tests/build.gradle at master · sebkur/gradle-scripts-tests · GitHub
I think I originally came across the idea on either of these sources:
- https://mkyong.com/gradle/gradle-application-plugin-app_home-in-applicationdefaultjvmargs/
- java - Pass $ character to Gradle start scripts (Application plugin) - Stack Overflow
- Gradle Application Plugin : How can I run jvm application with -javaagent options? - Stack Overflow
This approach worked until Gradle 6.9.1 and seems to stop working starting with Gradle 6.9.2.
The script template changed significantly across the two versions, so I’m not sure what exactly is causing this.
I came across two related issues:
- $APP_HOME is not getting populated in Gradle 7.2 · Issue #18425 · gradle/gradle · GitHub
- CreateStartScript task: use environment variables in JVM Options · Issue #19795 · gradle/gradle · GitHub
I have set up a testing project that exhibits the behavior both for some older versions of Gradle where this was still working and some more recent versions where it does no longer work: GitHub - sebkur/gradle-scripts-tests
I realize this is a hack and maybe it was never supposed to work like this as I’m kind of exploiting some internal information from the CreateStartScripts
task’s script template (the variable name of $APP_HOME
). Also it seems passing variables is not something supported as by default the $
gets escaped.
Could this be fixed somehow? Or is there a better way to make the main class aware of the application home directory at runtime?