Gradle Application Plugin APP_HOME in applicationDefaultJvmArgs

摘要: In Gardle, the application plugin, you can pass the system properties via 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

上一篇: Spring Data MongoDB Aggregation Grouping Example
下一篇: Spring MVC Beans loaded twice
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章来源于互联网, 若有侵权, 联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布文章.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号