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 build
task 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).
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.
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.
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.
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.
Add this to the end of your buildscript
build.dependsOn jacocoTestReport
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With