Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle optional @Input

How can I provide an optional property for task?

class CustomTask extends DefaultTask {

    @Input
    Closure preconfig

    // ...    
}

This way obligates user to provide preconfig closure as parameter when defining task with CustomTask type.

How can I achieve declarative way other than defining methods to set properties?

class CustomTask extends DefaultTask {

    @Input
    Closure preconfig

    def preconfig(Closure c){
        this.preconfig = c
    }

    // ...   
}
like image 680
Andrii Abramov Avatar asked Jan 10 '17 10:01

Andrii Abramov


People also ask

What is dependsOn in gradle?

dependsOn(jar) means that if you run assemble , then the jar task must be executed before. the task transitive dependencies, in which case we're not talking about tasks, but "publications". For example, when you need to compile project A , you need on classpath project B , which implies running some tasks of B .

How does gradle determine up to date?

Gradle will determine if a task is up to date by checking the inputs and outputs. For example, your compile task input is the source code. If the source code hasn't changed since the last compile, then it will check the output to make sure you haven't blown away your class files generated by the compiler.

Where are gradle tasks configured?

Locating tasks In general, tasks are available through the tasks collection. You should use of the methods that return a task provider – register() or named() – to make sure you do not break task configuration avoidance. Tasks of a specific type can also be accessed by using the tasks. withType() method.


1 Answers

Actually, I found a solution in assigning default value to the @Input fields.

Example:

class CustomTask extends DefaultTask {

    @Input
    Closure preconfig = null // or { } <- empty closure

    // ...    
}

And then check if the @Input variable is not null:

// ...

@TaskAction
def action(){
    if (preconfig) { preconfig() }
}

// ...

Also there is useful annotation @Optional:

class CustomTask extends DefaultTask {

    @Input @Optional
    Closure preconfig

    // ...    
}
like image 52
Andrii Abramov Avatar answered Sep 19 '22 15:09

Andrii Abramov