Reputation: 2505
I have a task from a plugin which I call on command line like this:
$ gradle myTask -PsomeArg=<value>
I want to define another task that would do exactly the same with a default parameter value. I expect to be able to call it like this:
$ gradle otherTask
I assume my task should look something like this:
task otherTask (dependsOn: 'myTask', someArg: 'value') {...}
Upvotes: 1
Views: 280
Reputation: 12106
You could define a task that would set default value for project property someArg
when this value is not provided as command line parameter :
task otherTask (){
finalizedBy myTask
doFirst{
if (!project.hasProperty("someArg")) {
project.ext.set("someArg", "defaultValue")
}
}
}
Note that you need to use finalizedBy
dependency type here: if you use otherTask.dependsOn myTask
the task myTask
will be executed first (so default value for property someArg
will not be set yet)
Execution result:
./gradlew myTask -PsomeArg=myValue
> Task :myTask
executing task pluginTask with arg: myValue
./gradlew otherTask -PsomeArg=myValue
> Task :myTask
executing task pluginTask with arg: myValue
./gradlew otherTask
> Task :myTask
executing task pluginTask with arg: defaultValue
Upvotes: 2