Build.gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 30: Line 30:
}
}
</syntaxhighlight>
</syntaxhighlight>
=Respond to Task Lifecycle Events=
Examples of how to respond to [[Gradle_Concepts_ToDeplete#Task|task]] lifecycle events.
==Task Creation==
<syntaxhighlight lang='groovy'>
tasks.whenTaskAdded { task ->
      task.ext.srcDir = 'src/main/java'
}
</syntaxhighlight>
==Task Execution==
<syntaxhighlight lang='groovy'>
gradle.taskGraph.beforeTask { Task task ->
      println "executing $task ..."
}
gradle.taskGraph.afterTask { Task task, TaskState state ->
      if (state.failure) {
          println "FAILED"
      }
      else {
          println "done"
      }
}</syntaxhighlight>

Revision as of 23:59, 19 May 2018


Respond to Build Lifecycle Events

Example of how to respond to project build lifecycle events.

allprojects {
      afterEvaluate { project ->
          if (project.hasTests) {
              println "Adding test task to $project"
              project.task('test') {
                   doLast {
                         println "running tests for $project"
                   }
              }
          }
     }
}
gradle.afterProject {project, projectState ->
      if (projectState.failure) {
          println "Evaluation of $project FAILED"
      } else {
          println "Evaluation of $project succeeded"
      }
}