Best way to write a Gradle task that can be configured from the command line?

I want to provide default values ​​for my properties, but allow them to be redefined from the command line in a relatively simple way. I am using Gradle 1.0 Milestone 3.

This is what I have, it works almost the way I want, but it is quite a lot. It just doesn't look like Gradle -ish - unlike the actual work I do, where 150-even lines of Java code are compressed to about two lines of Gradle script.

task sbtCopyTask { fromProp = "defaultCopyFrom" toProp = "defaultCopyTo" overrideFromProp = System.getProperty('fromSysProp') if( overrideFromProp != null && !overrideFromProp.trim().empty ){ fromProp = overrideFromProp } doLast { println "copying ${fromProp} to ${toProp}" } } 

performance

 $GRADLE_HOME/bin/gradle sbtCopyTask 

leads to:

 copying defaultCopyFrom to defaultCopyTo 

and execution

 $GRADLE_HOME/bin/gradle sbtCopyTask -DfromSysProp="wibble" 

leads to:

 copying wibble to defaultCopyTo 
  • I know that the task of creating a copy in Gradle is the question I ask - "how do I write a script assembly that is configured from the command line".
  • I tried the "-P" command line option - it doesn't seem to do what I want.
  • I want this to be done from the command line, property files or environment variables would be inconvenient.

Edit: Doh, 10 seconds after posting - this seems obvious.

So, this works great and is pretty good for me (if not the best way?)

 fromProp = System.getProperty('fromProp', 'defaultFromProp') 
+4
source share

All Articles