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
}
// ...
}
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 .
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.
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.
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
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With