Until now, we have added tasks to our build project using the task
keyword, followed by the name of the task. However, 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, as follows:
task 'simple' << { task -> println "Running ${task.name}" }
We can also use variable expressions to define a new task. If doing so, we must use parenthesis, otherwise the expression cannot be resolved. The following sample script defines a simpleTask
variable with the simple
string value. This expression is used to define the task. The result is that our project now contains a task with the name simple:
// Define name of task // as a variable. def simpleTask = 'simple' // Variable is used for the task name. 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...