Gradle app plugin and system environment variables

In my project, I tried to set up my app in system environments like this in my build.gradle:

apply plugin: 'application'
applicationDefaultJvmArgs = ["-Ddw.server.applicationConnectors[0].port=${System.env.PORT}"]

      

This works if the PORT is set to a constant, say 9001. But if I change the PORT to another variable, the executable script does not change, it is already compiled with the old PORT value in the build / install / bin / {executable-script}, for example:

DEFAULT_JVM_OPTS='"-Ddw.server.applicationConnectors[0].port=9001"'

      

instead of what I want

DEFAULT_JVM_OPTS='"-Ddw.server.applicationConnectors[0].port=$PORT"'

      

Is there a way to tell the application plugin to use the system environment variable instead of evaluating the system.env variable?

Btw, I also tried without single quotes to avoid evaluating the $ PORT expression

applicationDefaultJvmArgs = ['-Ddw.server.applicationConnectors[0].port=$PORT']

      

and

applicationDefaultJvmArgs = ['-Ddw.server.applicationConnectors[0].port=\$PORT']

      

But they are both compiled for this, which won't work.

DEFAULT_JVM_OPTS='"-Ddw.server.applicationConnectors[0].port=\$PORT"'

      

+3


source to share


1 answer


(I am a Gradle contributor who posted the applicationDefaultJvmArgs function)

If you write "$ {System.env.PORT}" in the applicationDefaultJvmArgs element, the PORT environment variable will be replaced at build time, i.e. on the developer machine, before Gradle even sees it, and then this value will be used in startup scripts, which you saw.

The fact that the literal $ in applicationDefaultJvmArgs is quoted in the start of the Unix script is not an error, it is intentional. The applicationDefaultJvmArgs array is intended to be portable across all target platforms such as Unix and Windows. Let's say Unix / Windows startup scripts are generated such that the JVMs launched on both platforms match exactly the string arguments that were passed to applicationDefaultJvmArgs at build time. This means that any shell metacharacters such as the $ on these lines must be specified in the start of the Unix script, so that if you have a literal $ PORT string in one of the arguments, it will play exactly the same, variable expansion occurs. If "$ PORT" was written to a unix starter script without the \ in front of the $, then the starter script will do what you want - on Unix. However, on Windows,where the starter script is the cmd batch file, $ has no special meaning (instead, %% should be used to expand the variable), so the string "$ PORT" will literally be passed to the JVM and your program will not work as expected. You may not care about Windows (in fact, I don't either), but Gradle and in particular the app plugin with all its features should be fully portable across all supported platforms, so that's how things are implemented right now. ...don't care about Windows (I don't actually do that either), but Gradle and in particular the app plugin with all its functionality should be fully portable across all supported platforms, so that's how things are done right now.don't care about Windows (I don't actually do that either), but Gradle and in particular the app plugin with all its features should be fully portable across all supported platforms, so that's how things are done right now.



If you really want the $ PORT variable to expand at runtime i.e. when the starter script is run on the user's computer, you need to change the generated starter script accordingly in the post-processing step. For example:

startScripts {
    doLast {
        unixScript.text = unixScript.text.replace('\\$PORT', '$PORT') 
        windowsScript.text = windowsScript.text.replace('\\$PORT', '%PORT%') //untested
    }
}

      

In the future, one might think of having some kind of platform independent notation for runtime variables in applicationDefaultJvmArgs, and the application plugin will then extend this in each starter script to the correct platform variable notation. But this is not currently planned, AFAIK.

+6


source







All Articles