Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute task with build task

Tags:

gradle

jacoco

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).

like image 231
sockeqwe Avatar asked Jul 08 '16 11:07

sockeqwe


People also ask

Which task is executed to use Gradle build in plugin?

You can simply execute the task named init in the directory where you would like to create the Gradle build. There is no need to create a “stub” build. gradle file in order to apply the plugin. The Build Init plugin also uses the wrapper task to generate the Gradle Wrapper files for the build.

Does Gradle run tasks in order?

We said earlier that the core of Gradle is a language for dependency based programming. In Gradle terms this means that you can define tasks and dependencies between tasks. Gradle guarantees that these tasks are executed in the order of their dependencies, and that each task is executed only once.

What is build task in Gradle?

The work that Gradle can do on a project is defined by one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.


2 Answers

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.

like image 53
Dennis Kühn Avatar answered Oct 06 '22 00:10

Dennis Kühn


Add this to the end of your buildscript

build.dependsOn jacocoTestReport
like image 25
Mark Fisher Avatar answered Oct 06 '22 00:10

Mark Fisher