Commandline Arguments are not set for dependant tasks - SOLVED


(Björn Pipiorke) #1

Hi @all,

I’m writing a custom gradle plugin for gradle 4.5 via java and I having some troubles with command line parameters.
I having two tasks, where one depends from the other.

Both task should react for the same commandline option (–myparam) for that I created a parent class which extends from the DefaultTask.

 @Option(option = "myparam", description = "Option to disable", order = 1)
 Boolean myparam = false;

With setter and getter. 

In the plugin Class I did the following:

Task taskA = project.getTasks().create(.....)
Task taskB = project.getTasks().create(.....)

taskB.dependsOn(taskA)

On the commandline I call

./gradlew taskA --myparam

I get the expected result, the value in the task is set

Same with calling ./gradlew taskB --myparam except the dependent taskA is called without the commandline value.

Can somebody point me in the right direction?
Thanks in advance.


(Sterling Greene) #2

The command-line option is only attached to the task that’s on the command-line. It’s not propagated to anything else.

If you have multiple tasks that all need to behave differently with the same parameter, you might consider using a project property instead. What does the option represent?


(Björn Pipiorke) #3

thanks good to know.
The parameter specifies a test / dry mode behavior, it shows the user what would happen.

I would like to have a behavior like the loglevel / stacktrace parameter, the parameter is nothing what should be persisted in a build.gradle file.

If you are talking about these properties https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html

I can’t see how they could be set via command-line option, is this correct? Or should they be set via the first task and then read from the others?

Thank you for help.


(James Justinic) #4

The project properties being referenced are documented in Build Environment: Project properties.

The same properties are settable from code, environment variables, gradle.properties, or the command line (using -P).


(Björn Pipiorke) #5

Thanks for help I will try that


(Björn Pipiorke) #6

I managed to handle it via ExtraPropertiesExtension

all my Tasks extend from a CommonTask and this set the command line parameters to the extension.
The dependant tasks get the parameter either from the extension or from the parameter.

So thanks for help.