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 create a task name. It is just like using expressions in the previous sample, but expressed in a Groovy GString:

def simpleTask = 'simple'

task "${simpleTask}" << { task ->
    println "Running ${task.name}"
}

// Or use loops to create multiple tasks
['Dev', 'Acc', 'Prod'].each { environment ->
    task "deployTo${environment}" << { task ->
        println "Deploying to ${environment}"
    }
}

If we run the tasks task, we can see we have four new tasks:

$ gradle -q tasks
...
Other tasks
-----------
deployToAcc
deployToDev
deployToProd
simple
...

Another way to add a new task is through the tasks property of a project. Remember that, in our build script, we have access to the Project object. Either we use the project variable explicitly, or we use methods and properties of the Project object implicitly, without using the project variable. The tasks property of a project is basically a container for all tasks in our project. In the following build script, we use the add method to add a new task:

def printTaskName = { task ->
    println "Running ${task.name}"
}

// Use project variable.
project.tasks.add(name: 'first') << printTaskName

// Let Gradle resolve tasks to project variable.
tasks.add(name: 'second', dependsOn: 'first') << printTaskName

Using task rules

We have seen how we can add tasks dynamically to our build project. But we can also define so-called task rules . These rules are very flexible and allow us to add tasks to our project based on several parameters and project properties.

Suppose we want to add an extra task for every task in our project that shows the description of a task. If we have a task first in our project, we want to add a task descFirst to show the description property of the task first. With task rules, we define a pattern for new tasks. In our sample this is desc<TaskName>; it is the prefix desc followed by the name of the existing task. The following build script shows the implementation of the task rule:

task first(description: 'First task')

task second(description: 'Second task')

tasks.addRule("Pattern: desc<TaskName>: show description of a task.") { taskName ->
    if (taskName.startsWith('desc')) {
        def targetTaskName = taskName - 'desc'
        def targetTaskNameUncapitalize = targetTaskName[0].toLowerCase() + targetTaskName[1..-1]
        def targetTask = project.tasks.findByName(targetTaskNameUncapitalize)
        if (targetTask) {
            task(taskName) << {
                println "Description of task ${targetTask.name} -> ${targetTask.description}"
            }
        }
    }
}

If we run the tasks task, we see an extra Rules section in the output:

$ gradle tasks
...
Rules
-----
Pattern: desc<TaskName>: show description of a task.
...

So, we know we can invoke descFirst and descSecond for our project. Note that those two extra tasks are not shown in the Other tasks section, but the Rules section shows the pattern we can use.

If we execute the descFirst and descSecond tasks, we get the following output:

$ gradle descFirst descSecond
:descFirst
Description of task first -> First task
:descSecond
Description of task second -> Second task

BUILD SUCCESSFUL

Total time: 2.223 secs
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.39.60