Reputation: 55
I am trying to pass an commandLine Argument to a Gradle Exec task. So I wanna execute a bash script and give it a parameter. However executing the script without the parameter goes fluently. It is just the parameter itself with the @Option
that is giving a null when printing. My code is the following
import org.gradle.api.tasks.options.Option;
task buildSamples(type: SampleExecTask){
command()
}
class SampleExecTask extends Exec {
private String argument;
@Option(option = "argument", description = "An argument for the script")
public void setSample(String argument) {
this.argument = argument;
}
@Input
public String getArgument() {
return argument;
}
void command() {
println(argument);
commandLine 'sh', 'myBashScript.sh'
}
}
Does anyone have any idea why my argument value is null and doesn't get set? Thanks in advance!
Upvotes: 1
Views: 733
Reputation: 7600
It does get set, but not at the time you are referencing it. Most objects in Gradle are lazily evaluated and at configuration time it is not guaranteed to be fully configured.
If you put command()
in a doFirst
block, it should not be null.
However, the more correct way to augment the Exec task with an extra argument from a command-line option is probably to configure the standard args
properties in the setter:
// Groovy DSL
task buildSamples(type: SampleExecTask)
class SampleExecTask extends Exec {
@Option(option = "argument", description = "An argument for the script")
public void setSample(String argument) {
args(argument)
}
// Base configuration can be set in the constructor
SampleExecTask() {
executable('sh')
args('myBashScript.sh')
}
}
Upvotes: 2