Adding tasks in other ways
Until now, we have added tasks to our build project using the task
keyword followed by the name of the task. But there are more ways to add tasks to our project. We can use a string
value with the task name to define a new task:
task 'simple' << { task -> println "Running ${task.name}" }
We can also use variable expressions to define a new task. If doing so, we must use parentheses, because otherwise the expression cannot be resolved. The following sample script defines a variable simpleTask
with the string
value simple
. This expression is used to define the task. The result is that our project now contains a task with the name simple
:
def simpleTask = 'simple' task(simpleTask) << { task -> println "Running ${task.name}" }
We can run the tasks
task to see our newly created task:
$ gradle -q tasks ... Other tasks ----------- simple ...
We can also use the power of Groovy to add new tasks. We can use Groovy's GString
notation to dynamically...