问题
I'm writing a custom Gradle task that accepts an option from the command line. That part works as expected. What's causing me issues is that calling a dependent task, the command line option is rejected because it's not relevant to the dependent task. Here's a sample that demonstrates the issue:
class CustomTask extends DefaultTask {
@Option(option = "stuff", description = "Custom task stuff")
String stuff
@TaskAction
void action() {
if (this.stuff?.trim()) {
println this.stuff
}
else {
throw new InvalidUserDataException("No stuff!")
}
}
}
task custom(type: CustomTask)
task depends(dependsOn: 'custom')
Here are the paths:
gradle custom
correctly throws an exceptiongradle custom --stuff=mystuff
emitsmystuff
when the task runs.gradle depends
correctly throws the same exceptiongradle depends --stuff=mystuff
fails withUnknown command-line option '--stuff'
How do I make the command line option --stuff
pass through to the custom
task when calling the depends
task?
回答1:
Unfortunately, that's one of the documented limitations of options https://docs.gradle.org/current/userguide/custom_tasks.html#limitations
You could use build properties instead, and pass them in with -Pstuff=something
来源:https://stackoverflow.com/questions/52171801/gradle-dependent-task-doesnt-pass-command-line-options-to-parent-task