Search

Dark theme | Light theme

October 18, 2010

Gradle Goodness: Excluding Tasks for Execution

In Gradle we can create dependencies between tasks. But we can also exclude certain tasks from those dependencies. We use the command-line option -x or --exclude-task and specify the name of task we don't want to execute. Any dependencies of this task are also excluded from execution. Unless another task depends on the same task and is executed. Let's see how this works with an example:

task copySources << {
    println 'Copy sources.'
}

task copyResources(dependsOn: copySources) << {
    println 'Copy resources.'
}

task jar(dependsOn: [copySources, copyResources]) << {
    println 'Create JAR.'
}

task deploy(dependsOn: [copySources, jar]) << {
    println 'Deploy it.'
}

We execute the deploy task:

$ gradle -q deploy 
Copy sources.
Copy resources.
Create JAR.
Deploy it.

Now we exclude the jar task. Notice how the copySources task is still executed because of the dependency in the deploy task:

$ gradle -q deploy -x jar
Copy sources.
Deploy it.

Written with Gradle 0.9.