Creating a task that runs before all other tasks in gradle

前端 未结 3 1837
不思量自难忘°
不思量自难忘° 2021-02-14 21:09

I need to create an initialize task that will run before all other task when I execute it.

task A {
    println \"Task A\"
}

task initializer {
   println \"ini         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 21:32

    Seems like you aim execution phase, and you want a task precursing each task or just run as a first task in the execution phase?

    If you want a task to always execute in every project before each other task after its being evaluated you can add a closure to he main build.gradle:

    allprojects {
      afterEvaluate {
        for(def task in it.tasks)
          if(task != rootProject.tasks.YourTask)
          task.dependsOn rootProject.tasks.YourTask
      }
    }
    

    or

    tasks.matching {it != YourTask}.all {it.dependsOn YourTask}

    You can also use the Task Execution Graph to define the lifecycle. There are few ways of achieving your goal, depending on your needs and a project structure.

提交回复
热议问题