injecteer
injecteer

Reputation: 20699

Read command line gradle vars in class

In my build.gradle I have a task:

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

I want to specify command-line args and read them in my class.

I tried:

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

but the lines in my class:

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

log nulls for both cases.

What am I missing?

Upvotes: 3

Views: 64

Answers (1)

Opal
Opal

Reputation: 84756

This:

.\gradlew.bat run -Dhttpport=8825

you are passing system properties to the gradle itself, not to the process it will start. To make it work this way you need to configure run as follows:

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

and:

.\gradlew.bat run -Dhttpport=8825

You can also configure system properties using project properties (-P) so:

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

and then:

.\gradlew.bat run -Phttpport=8825

Upvotes: 3

Related Questions