Gradle Application Plugin – APP_HOME in applicationDefaultJvmArgs

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

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

  1. Gradle – The Application Plugin
  2. Gradle DSL reference – CreateStartScripts
  3. Gradle DSL Reference – The Application plugin

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Bavo BruylandtHorheArunkumar GopalanGradle – Multiple start script examples Recent comment authors
newest oldest most voted
Arunkumar Gopalan
Guest
Arunkumar Gopalan

Please can you added the grails examples

Horhe
Guest
Horhe

since version 2.13 you can put $APP_HOME in your defaultJvmOpts. But this will broke the windows script.

trackback
Gradle – Multiple start script examples

[…] Gradle Application Plugin – APP_HOME […]

Bavo Bruylandt
Guest
Bavo Bruylandt

That works fine, thank you