How to create gradle task which always runs?

后端 未结 3 529
走了就别回头了
走了就别回头了 2021-02-13 14:01

I\'m likely overlooking something pretty core/obvious, but how can I create a task that will always be executed for every task/target?

I can do something like:



        
3条回答
  •  逝去的感伤
    2021-02-13 14:42

    This attaches a closure to every task in every project in the given build:

    def someClosure = { task ->
      println "task executed: $task"
    }
    
    allprojects {
      afterEvaluate {
        for(def task in it.tasks)
          task << someClosure
      }
    }
    

    If you need the function/closure to be called only once per build, before all tasks of all projects, use this:

    task('MyTask') << {
      println 'Pre-build hook!'
    }
    
    allprojects {
      afterEvaluate {
        for(def task in it.tasks)
          if(task != rootProject.tasks.MyTask)
            task.dependsOn rootProject.tasks.MyTask
      }
    }
    

    If you need the function/closure to be called only once per build, after all tasks of all projects, use this:

    task('MyTask') << {
      println 'Post-build hook!'
    }
    
    allprojects {
      afterEvaluate {
        for(def task in it.tasks)
          if(task != rootProject.tasks.MyTask)
            task.finalizedBy rootProject.tasks.MyTask
      }
    }
    

提交回复
热议问题