xeruf
xeruf

Reputation: 2990

Gradle disable automatic subproject execution for specific task

I have a multi-project Gradle build and I customised the "run" task to do something a bit different in the root project. However, I don't want it to call the "run" task of each sub-project after completion, as it does now. But this behaviour should only be for this task, I want every other task to be recursively executed as is the default, but the run task not. I also cannot disable the run task globally for every subproject, because it does have a purpose in each subproject when executed on its own.

Upvotes: 3

Views: 3442

Answers (1)

Michael Easter
Michael Easter

Reputation: 24468

In the root build.gradle, consider the following (full example here):

gradle.taskGraph.whenReady { graph ->
    def hasRootRunTask = graph.hasTask(':run')

    if (hasRootRunTask) {
        graph.getAllTasks().each { task ->
            // look for :abc:run, :def:run etc
            def subRunTask = (task.path =~ /:.+:run/)
            if (subRunTask) {
                println "TRACER skipping ${task.path} because ':run' was specified"
                task.enabled = false
            }
        }
    }
} 

This will check the task graph for :run. When it exists, then :abc:run (that is, a subproject task) will be disabled.

Example output for root run task:

$ gradle -q run 
TRACER skipping :abc:run because ':run' was specified
TRACER skipping :def:run because ':run' was specified
TRACER executing run for path :

Example output for run task in abc subproject on its own:

$ gradle -q :abc:run 
TRACER executing run for path :abc

Upvotes: 5

Related Questions