Search code examples
gradlegradle-task

Can I run a specific group of gradle tasks


I have a set of gradle tasks which can run in any order and I grouped them into 3 sets A, B and C

is there a way to run a specific group of task ? like I want to run only A only B and may be a combination of A & B


Solution

  • I think you pretty much answered it yourself, unless I am missing something :)

    You group them into aggregate tasks, and this allows you to do what you want. These tasks don't need to have any actions attached.

    For example, if you have a bunch of task like foo, bar and baz, you can group them like this:

    // Individual tasks
    task foo { doLast { logger.quiet("foo!")} }
    task bar { doLast { logger.quiet("bar!")} }
    task baz { doLast { logger.quiet("baz!")} }
    
    // Aggregate tasks (groups)
    task A(dependsOn: ["foo", "bar"])
    task B(dependsOn: ["foo", "baz"])
    task C(dependsOn: ["baz"])
    

    Running A executes foo and bar:

    $ gradle A
    
    > Task :bar
    bar!
    
    > Task :foo
    foo!
    
    BUILD SUCCESSFUL in 1s
    2 actionable tasks: 2 executed
    

    Running both A and B executes them all:

    $ gradle A B
    
    > Task :bar
    bar!
    
    > Task :foo
    foo!
    
    > Task :baz
    baz!
    
    BUILD SUCCESSFUL in 1s
    3 actionable tasks: 3 executed
    

    Update: Sorry, didn't realize you were talking about the actual group property on tasks.

    I am pretty sure that you will still have to create a task yourself for each group, but you can simplify the dependency definition with something like this:

    A.dependsOn(tasks.matching { it.group = "A" })

    If you have many groups, you can further automate the creation of the grouping tasks like this:

    tasks.findAll{}.each { task ->
        tasks.maybeCreate(task.group).dependsOn(task)
    }