Gradle Application Plugin APP_HOME in applicationDefaultJvmArgs
By:Roy.LiuLast updated:2019-08-17
In Gardle, the application plugin, you can pass the system properties via applicationDefaultJvmArgs :
gradle.build
apply plugin:'application' mainClassName = "com.mkyong.analyzer.engine.hydra.entryPointForJar" applicationName = 'analyzer' distZip { archiveName 'analyzer-' + version + '.zip' applicationDefaultJvmArgs = ["-Dlogback.configurationFile=logback.xml"]
The problem is how to get the APP_HOME for logback.xml?
gradle.build
applicationDefaultJvmArgs = ["-Dlogback.configurationFile=APP_HOME/logback.xml"]
You can hard code the APP_HOME, but this will only work for one platform (Windows or *nix).
1. Solution
To fix it, create a custom “MY_APP_HOME” variable, and replace it with doLast
gradle.build
applicationDefaultJvmArgs = ["-Dlogback.configurationFile=MY_APP_HOME/logback.xml"] startScripts { doLast { unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME') windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..')
Note
This solution works in both Windows and *nix platforms. Tested with Gradle 2.0
This solution works in both Windows and *nix platforms. Tested with Gradle 2.0
Build it.
gradle distZip
Output
${project}\build\distributions\${project-name}\bin\analyzer
#!/usr/bin/env bash ############################################################################## ## ## analyzer start up script for UN*X ## ############################################################################## DEFAULT_JVM_OPTS='"-Dlogback.configurationFile=$APP_HOME/logback.xml"
${project}\build\distributions\${project-name}\bin\analyzer.bat
@if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @rem analyzer startup script for Windows @rem @rem ########################################################################## set DEFAULT_JVM_OPTS="-Dlogback.configurationFile=%~dp0../logback.xml"
2. Solution – Custom Startup Script
This is for the custom startup script:
gradle.build
task abcStartScripts(type: CreateStartScripts) { mainClassName = "com.mkyong.analyzer.engine.hydra.entryPointForJar" classpath = startScripts.classpath outputDir = startScripts.outputDir applicationName = 'analyzer' defaultJvmOpts = ["-Dlogback.configurationFile=MY_APP_HOME/logback.xml"] abcStartScripts { doLast { unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME') windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..') applicationDistribution.into("bin") { from(hostingStatStartScripts) fileMode = 0755
References
- Gradle – The Application Plugin
- Gradle DSL reference – CreateStartScripts
- Gradle DSL Reference – The Application plugin
From:一号门
COMMENTS