Skipping tests when building with composite build in Gradle - gradle

I have 5 projects that are dependent. So I have made new 'dummy' project that is a composite build of these 5 projects and have the following task:
task build {
dependsOn gradle.includedBuilds*.task(':build')
}
I can run the build of all projects in composite using: gradlew build? Or how to this in other way?
How can I now pass the -x test to skip the test execution for composite build?

Instead of trying to exclude tasks (build tasks depend on test), you can use the assemble task instead:
task buildWithoutTests {
dependsOn gradle.includedBuilds*.task(':assemble')
}
According to the task listing (gradle tasks), the difference is:
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.

Related

Gradle - how to prevent dependent submodule's tasks from running if dependent task fails?

In a multi-module project with modules mod-a and mod-b, mod-b has mod-a as a project lib dependency:
project(":mod_b") {
dependencies {
implementation project(':mod_a')
}
}
Both mod-a and mod-b are configured to publish Maven artifacts (jars) if their tests pass, using the maven-publish plugin:
// within each module's build.gradle
tasks.withType(AbstractPublishToMaven) {
dependsOn test
}
When I run Gradle with the --continue flag then if tests in mod-a fail then no mod-a artifacts are published. However, artifacts are published for mod-b. Why is this and how to I prevent this happening (I wish to continue to use the --continue option)?
e.g. this results in publishing of com.example:mod-b:x.y.z, when I do not wish it to:
./gradlew --continue clean publishToMavenLocal
i.e. if there were 3 modules (mod-a -depends-on-> mod-b -depends-on-> mod-c), then if only mod-b's tests fail and then only mod-a should be published).
The solution is to make the publish tasks also depend on the buildNeeded task. The buildNeeded task builds and tests all other projects that the project in question requires. Thus if a dependent task fails, buildNeeded will be marked as having failed and thus the publish task won't fire.

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'
}

How to add task dependency on all test tasks in gradle in multi module project

I have java gradle project with multiple modules and each module has its tests. How can I configure gradle to run some task (run docker compose up to give you some context, but that's not a part of the question) before first test task is run and some another task after last test has finished?
You can use both dependsOn and finalizedBy on all test tasks from the root project. If all tasks share a common base class (Test in this example), you can use withType:
subprojects*.tasks.withType(Test)*.configure {
dependsOn myPreparationTask
finalizedBy myCleanUpTask
}
If you need to configure tasks of different types, use the matching method instead with a custom condition.

Gradle - run testReport task after test phase of all submodules

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.

Gradle task at the end of build with a multiproject build

I am attempting to create my own task, that will create a package of all the artifacts generated during my multi-project build.
Currently what I do right is now just:
gradle build createPackage
Which gives output like the following:
:test-utility:compileJava UP-TO-DATE
:test-utility:processResources UP-TO-DATE
...
:test-utility:check UP-TO-DATE
:test-utility:build UP-TO-DATE
Creating Package...
Created.
BUILD SUCCESSFUL
Total time: 4.654 secs
With the createPackage task being:
task createPackage {
println "Creating Package..."
println "Created."
}
However, I would like to simply it by running just one command, so what would the best way to do this and ensure the order stays maintained?
Ideally I would like to call build within the createPackage task or append to the build task with my task.
Ok reading between the lines there are a few things to clear up
1) Your printlns are being run in the configuration phase, not the execution phase. See http://www.gradle.org/docs/current/userguide/build_lifecycle.html for more info.
2) You do not have a single 'build' task. gradle build on the command line will run the 'build' task of each of your subprojects. Your package task would need to depend on all of them. Something like..
apply plugin: 'java'
evaluationDependsOnChildren()
task createPackage(type:Zip) {
dependsOn subprojects*.build
}
3) You can be more declarative with gradle - just tell it what you want to package up and it will figure out what it needs to run. For example, you can say that you want to zip up all the jars from your subprojects.
apply plugin: 'java'
evaluationDependsOnChildren()
task createPackage(type:Zip) {
from subprojects*.jar
}
There are plenty of ways. One suggestion is to modify build to depend on createPackage to make sure you can call just gradle build. Then you want to enhance your task and tell what are its inputs (probably those are outputs of some other tasks like jar). Gradle will add these tasks to execution when you run the build and can re-run them based on up-to-date status of each task. This is documented in userguide - http://www.gradle.org/docs/current/userguide/more_about_tasks.html#sec:up_to_date_checks and later in a chapter on custom tasks.

Resources