Command line options for tasks


(saberduck) #1

I would like to create a task with some additional options. org.gradle.api.internal.tasks.options.Option annotation seems to be used internally by gradle to achieve this. I found this topic in old forum Expose the org.gradle.api.internal.tasks.options.Option annotation for use in user tasks , is there any plan to make this annotation available to user tasks?


How to get --tests command params in plugin?
(Luke Daley) #2

We want to open up something like this, but have no concrete plans or schedule to do this.


(saberduck) #3

And is it possible to use it now? even thought it’s internal api? can I get it on the classpath of buildscript? thx


(Benjamin Muschko) #4

It’s possible to use @Option right now. You’d just have to import the class into your build script or custom task class (depending on where you decide to use it).

import org.gradle.api.internal.tasks.option.Option;

Keep in that the class is an internal API and might change package, name, behavior or go away with future versions of Gradle.


(Sebastian Schuberth) #5

Would you have an example on how exactly use Option to make a task put into build.gradle read options from the command line?


(Sebastian Schuberth) #6

Here’s an example from one of Gradle’s own integration tests:

import org.gradle.api.internal.tasks.options.Option
import org.gradle.api.internal.tasks.options.OptionValues

subprojects{
    task hello(type: CustomTask)
}

class CustomTask extends DefaultTask {
    @TaskAction
    void doSomething() {
    }

    @Option(option = "stringValue", description = "Configures a string value in CustomTask.")
    public void setStringValue(String value) {
    }

    @OptionValues("stringValue")
    public List<String> possibleValues(){
        return Arrays.asList("optionA", "optionB", "optionC", "$path")
    }
}

(Fabien Renaud) #7

Why not making @option public/not-internal?


(Benjamin Muschko) #8

There was a PR for making it public but the contributor dropped off due to lack of time. Feel free to pick it up again. There’s nothing speaking against making it public per-se.