11

I have a traditional java project using gradle build system. I would like to add jacoco code coverage report generation. So I applied jacoco plugin and everything works as expected when I call gradle build jacocoTestReport

I was wondering how can I define in my build.gradle script that jacocoTestReport task should run automatically after buildtask has been finished.

The goal is to only run gradle build from command line and this will automatically execute test and jacocoTestReport (so that I don't have to pass jacocoTestReport as commandline parameter explicitly).

sockeqwe
  • 15,574
  • 24
  • 88
  • 144

2 Answers2

10

I would suggest

build.finalizedBy(jacocoTestReport)

This way, the jacocoTestReport task is only executed after the build task, as you specified. In the accepted answer, the build task depends on the test report task, which means build will be executed after your custom task.

Dennis Kühn
  • 371
  • 2
  • 11
5

Add this to the end of your buildscript

build.dependsOn jacocoTestReport
Mark Fisher
  • 9,838
  • 3
  • 32
  • 38
  • think of gradle asking each task what its dependencies are (i.e. what needs to be run) before it can be run. "before build can be run, x must run", so "build depends on x" – Mark Fisher Jul 08 '16 at 12:04
  • 2
    Doing this in gradle 5.0 with Kotlin DSL fails as it cannot find 'build'. Having a hard time finding the proper syntax to reference the build lifecycle phases like build, test, clean, etc. – Bellini Dec 09 '18 at 06:34