Understanding Gradle task dependency (dependsOn)

后端 未结 2 2119
醉梦人生
醉梦人生 2020-12-08 01:43

Two questions:

  1. What is the gradle way to specify that 1 task is comprised of several other tasks?
  2. It seems like gradle\'s taskName.execute()
相关标签:
2条回答
  • 2020-12-08 02:44

    Gradle's task model is "flat" and doesn't have a concept of aggregation. (It's important to note that TaskInternal#execute is an internal method, and must not be called from build scripts.) Aggregation is often simulated with a lifecycle task (a task with task dependencies but without any task actions):

    task allTests {
        dependsOn tasks.withType(Test)
    }
    

    Besides dependsOn, the following task relationships are supported: mustRunAfter, shouldRunAfter, and finalizedBy.

    0 讨论(0)
  • 2020-12-08 02:44

    Typically, you do not invoke task.execute().

    You can specify that one task is comprised of other tasks in the following manner:

    task task1 << {
       println "Hello"
    }
    
    task task2 << {
       println "World"
    }
    
    task task3(dependsOn: 'task3dependency') << {
       println "QBert"
    }
    
    task task3dependency << {
       println "MR"
    }
    
    task tests(dependsOn: ['task1', 'task2', 'task3'])
    

    This outputs:

    $ gradle tests
    :task1
    Hello
    :task2
    World
    :task3dependency
    MR
    :task3
    QBert
    :tests
    
    BUILD SUCCESSFUL
    

    Keep in mind that the order in which your dependency tasks are run is not always guaranteed, but you can mitigate this by specifying the order task2.mustRunAfter task1. Usually though, the tasks are run in the order you would expect.

    Also, you should read up on Gradle's Build Lifecycle. When you use the syntax task task1 << {...}, you are specifying a closure that is run in the execution phase of the task. Before execution is run, the configuration phase evaluates your build script and determines the tasks to be run and in what order. When you manually execute tasks, as in:

    task tests << {
        task1.execute()
        task2.execute()
        task3.execute()
    }
    

    you have bypassed Gradle's ability to evaluate the task dependencies of task3, hence it runs only task3.

    0 讨论(0)
提交回复
热议问题