Gradle - run testReport task after test phase of all submodules - gradle

I have a simple project with subprojects and I want to generate aggregate report for all tests when I execute gradle test command.
I have followed the gradle documentation and added following:
task testReport(type: TestReport) {
// make sure this task is run after all subproject test tasks
mustRunAfter subprojects*.test
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}
This works when I execute gradle test testReport, but when I execute gradle test or gradle build in the root project - the task testReport is not run.
How do make gradle to run the task without specifying it every time?

Add: test.finalizedBy 'testReport' to your build.gradle; just at the root level, doesn't have to be inside any closure.
taskX.finalizedBy taskY
Will run taskY everytime taskX completes execution successfully.

Related

gradle composite build execute tasks transitively

When executing gradle build in a composite gradle build only compileJava task is executed transitively. Others, like the test task are not.
How can I enforce that my tests are also run on build?
I tried:
build.dependsOn gradle.includedBuilds*.task(':build')
but that did not work.
https://docs.gradle.org/current/userguide/composite_builds.html
here is the gist:
// settings
if (file('../myModule').exists()) {
includeBuild('../myModule')
}
// build1
dependencies {
compile 'mygroup:MyModule:1.0.0'
}

Calling Gradle task for only one subproject

How do I configure subprojects for Gradle and run a Gradle task from the build.gradle only for the current directory I'm executing the task from?
For example, suppose I have two projects in a parent folder:
root-project
project-1
project-2
build.gradle
settings.gradle
I have project 1 and 2 included in the settings.gradle file:
include(
'project-1',
'project-2'
)
I have a task configured in the build.gradle file that I would like to call. I've nested the tasks within a subprojects{} block:
subprojects {
...
task check(type: JavaExec) {
main = MAIN_CLASS
classpath = sourceSets.main.runtimeClasspath
}
...
}
However, the build.gradle is run for all subprojects that way.
How can I define all possible subprojects but only call a Gradle task for the current project I'm executing from (i.e., avoid calling the task for all subprojects)? I would like to do this with the current task name only (e.g., gradle check).
Invoking the task in a single project with gradle check is not possible.
You have two options:
Leverage the model Gradle proposes, where tasks can be prefixed by the project name in a multi project build which removes the need to navigate to subprojects.
Which means invoking gradle :project-1:check or gradle :project-2:check depending on which task you want to run.
Define the tasks to have different name in each subprojects. See task rules on how to define tasks with dynamic names.
Which means invoking gradle check1 or gradle check2

Liquibase changesets are not running in bootRepackage gradle task

I have created liquibase changesets, in the Spring boot application. If I execute bootRun task then changesets get executed however, if I run bootRepackage changesets are not executed.
What additional configuration I need to do in order to run changesets with bootRepackage.
I suggest that you add the task tree plugin to your script
plugins {
id "com.dorongold.task-tree" version "1.3"
}
And then run
gradle bootRun bootRepackage taskTree
You should then see which tasks are run by the bootRun task which are not run by the bootRepackage task.
You'd then likely add a task dependency in build.gradle
bootRepackage {
dependsOn someLiquibaseTask
}

Execute a Task of a sub-module of a Gradle multiproject

I have the following multiproject structure:
settings.gradle
rootProject.name = 'toolbox-backend'
include 'toolbox-components-rest'
include 'toolbox-components-executor'
include 'toolbox-components-toolsyncer'
I'd love to create a task in my root build.gradle which will call the clean, build, install (application) and finally the run task of the toolbox-components-rest submodule.
task startREST() {
dependsOn ':toolbox-components-rest:clean'
dependsOn ':toolbox-components-rest:build'
dependsOn ':toolbox-components-rest:bootRun'
println "[Toolbox $version] Starting REST interface..."
}
This does work - BUT the bootRun task is running before build which runs before clean. I'd like to have it exactly the other way around
Fixed the above with
bootRun.mustRunAfter build
build.mustRunAfter clean
in the gradle.build of the toolbox-components-rest submodule

Execute task with build task

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

Resources