Main Tutorials

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 Author

author image
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

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Abderrahim
1 year ago

Thank you so much

Axel
2 years ago

Thank you so much

Bavo Bruylandt
6 years ago

That works fine, thank you

Horhe
7 years ago

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

Arunkumar Gopalan
9 years ago

Please can you added the grails examples