如何配置 gradle 应用程序插件以将我的 user.dir 设置为脚本的位置?
How to configure gradle application plugin to set my user.dir to the location of the script?
gradle 应用程序插件运行并创建以下目录结构
应用
bin - 启动服务器的脚本
lib-jar 文件
{src/dist 中的任何其他内容都将进入应用程序}
我无法弄清楚的是脚本不会在 app 目录中创建服务器 运行,这很烦人。有没有办法可以配置它并将 运行 放在应用程序目录中? (或者一种使其将 user.dir 设置为 .. 相对于 bin 目录的方法)。
这非常令人沮丧,因为我必须进行某种检查并出错,表明该程序必须来自应用程序目录 运行,这样我就知道如何查找文件了。
现在,如果你有 属性
-Dlogback.configurationFile=config/logback.xml
并且您 运行 从机器上除了 app 目录之外的任何地方启动脚本,记录静默停止工作。
谢谢,
院长
以下代码对我有用(仅在 linux 上测试,不确定 windows 部分(
// set user.dir to the distribution directory so that the config directory can be located
CreateStartScripts startScripts = project.startScripts
startScripts.with {
doLast {
unixScript.text = unixScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)((\'|\")(.*)(\'|"))(?=\n)',
"-Duser.dir=$APP_HOME")
windowsScript.text = windowsScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)(.*)(?=\r\n)',
'-Duser.dir=%APP_HOME%')
}
}
根据 Jeff Gaer 的回答,我的 gradle/groovy 版本得到了以下答案。 gradle 版本 2.14-rc-1 groovy 2.4.4
我只在 mac.
上测试过
重要提示:bash 脚本中有“"args"”,这很烦人,而 windows 只有 "args",所以我必须做的替换是有点不同。我将他的回答标记为正确,因为它让我找到了答案。
applicationDefaultJvmArgs = ['JVM_ARGS_TRICK']
//Here, we must modify the start scripts to set user.dir property so no matter where the script is run from,
//the user.dir will point to APPNAME directory
CreateStartScripts startScripts = project.startScripts
startScripts.with {
doLast {
//A slash in groovy allows us to not escape double quotes AND single quotes
def slashString = /'"JVM_ARGS_TRICK"'/
//for some reason, gradle prints '"jvm args"' why? I don't know but must swap quote and double quote
unixScript.text = unixScript.text.replace(slashString,
'"-Dlogback.configurationFile=$APP_HOME/config/logback.xml -Duser.dir=$APP_HOME"')
windowsScript.text = windowsScript.text.replace('"JVM_ARGS_TRICK"',
'"-Dlogback.configurationFile=%APP_HOME%/config/logback.xml -Duser.dir=%APP_HOME%"')
}
}
gradle 应用程序插件运行并创建以下目录结构
应用 bin - 启动服务器的脚本 lib-jar 文件 {src/dist 中的任何其他内容都将进入应用程序}
我无法弄清楚的是脚本不会在 app 目录中创建服务器 运行,这很烦人。有没有办法可以配置它并将 运行 放在应用程序目录中? (或者一种使其将 user.dir 设置为 .. 相对于 bin 目录的方法)。
这非常令人沮丧,因为我必须进行某种检查并出错,表明该程序必须来自应用程序目录 运行,这样我就知道如何查找文件了。
现在,如果你有 属性 -Dlogback.configurationFile=config/logback.xml
并且您 运行 从机器上除了 app 目录之外的任何地方启动脚本,记录静默停止工作。
谢谢, 院长
以下代码对我有用(仅在 linux 上测试,不确定 windows 部分(
// set user.dir to the distribution directory so that the config directory can be located
CreateStartScripts startScripts = project.startScripts
startScripts.with {
doLast {
unixScript.text = unixScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)((\'|\")(.*)(\'|"))(?=\n)',
"-Duser.dir=$APP_HOME")
windowsScript.text = windowsScript.text.replaceFirst('(?<=DEFAULT_JVM_OPTS=)(.*)(?=\r\n)',
'-Duser.dir=%APP_HOME%')
}
}
根据 Jeff Gaer 的回答,我的 gradle/groovy 版本得到了以下答案。 gradle 版本 2.14-rc-1 groovy 2.4.4
我只在 mac.
上测试过重要提示:bash 脚本中有“"args"”,这很烦人,而 windows 只有 "args",所以我必须做的替换是有点不同。我将他的回答标记为正确,因为它让我找到了答案。
applicationDefaultJvmArgs = ['JVM_ARGS_TRICK']
//Here, we must modify the start scripts to set user.dir property so no matter where the script is run from,
//the user.dir will point to APPNAME directory
CreateStartScripts startScripts = project.startScripts
startScripts.with {
doLast {
//A slash in groovy allows us to not escape double quotes AND single quotes
def slashString = /'"JVM_ARGS_TRICK"'/
//for some reason, gradle prints '"jvm args"' why? I don't know but must swap quote and double quote
unixScript.text = unixScript.text.replace(slashString,
'"-Dlogback.configurationFile=$APP_HOME/config/logback.xml -Duser.dir=$APP_HOME"')
windowsScript.text = windowsScript.text.replace('"JVM_ARGS_TRICK"',
'"-Dlogback.configurationFile=%APP_HOME%/config/logback.xml -Duser.dir=%APP_HOME%"')
}
}