在课堂上阅读命令行gradle vars

时间:2017-10-25 13:36:03

标签: java gradle command-line environment-variables args

在我的build.gradle我有一项任务:

run {
  dependsOn ':jar'
  args = [ 'run', project.mainVerticleName, '-cluster', "-launcher-class=$mainClassName", '-Denvironment=development' ]
}

我想指定命令行参数并在我的课程中阅读它们。

我试过了:

.\gradlew.bat run -Dhttpport=8825 -Phttpport=8825

但我班上的行:

log.info "port = ${System.getProperty( 'httpport' )}"
log.info "port = ${System.getenv( 'httpport' )}"

两种情况都记录null

我错过了什么?

1 个答案:

答案 0 :(得分:3)

此:

.\gradlew.bat run -Dhttpport=8825

您将系统属性传递给gradle本身,而不是它将启动的进程。要使其以这种方式工作,您需要按如下方式配置run

run {
  dependsOn ':jar'
  args = [ 'run', project.mainVerticleName, '-cluster', "-launcher-class=$mainClassName", '-Denvironment=development' ]
  systemProperties System.properties
}

.\gradlew.bat run -Dhttpport=8825

您还可以使用项目属性(-P)配置系统属性,以便:

run {
  dependsOn ':jar'
  args = [ 'run', project.mainVerticleName, '-cluster', "-launcher-class=$mainClassName", '-Denvironment=development' ]
  systemProperties [httpport:project.httpport]
}

然后:

.\gradlew.bat run -Phttpport=8825