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'
}
Related
I have a multi-project gradle build with four Kotlin Multiplatform modules, two of which have tests. When I run gradle check, if any of the tests from one of the modules fails, the tests for the other module do not get executed.
I'm using Gradle 7.3, Java 17 and kotlin.test. Tests for both projects are located in the commonTest source set. Also tried Gradle 7.1 and Java 11 with the same behavior.
Excerpt from settings.gradle.kts:
include(":ProjectA")
include(":ProjectB") // B has tests and depends on D, its tests are run
include(":ProjectC")
include(":ProjectD") // D has tests but are not run
Excerpt from ProjectB build.gradle.kts:
sourceSets {
val commonMain by getting {
dependencies {
api(compose.runtime)
api(compose.foundation)
api(compose.material)
implementation(project(":ProjectD"))
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
From the output of gradle check I can see that :ProjectB:allTests gets executed and fails but :ProjectB:allTests never gets executed. This is an excerpt from the gradle output:
> Task :ProjectB:desktopTest
com.mylibrary.AppTest[desktop] > helloTestNg[desktop] FAILED
java.lang.AssertionError at AppTest.kt:8
2 tests completed, 1 failed
There were failing tests
> Task :ProjectB:allTests FAILED
FAILURE: Build failed with an exception.
If I do gradle -p ProjectD check tests for ProjectD are executed correctly.
Default Gradle behavior is to stop if any task fails, and Gradle considers a failing test as failing the check task. As a consequence, if any test fails in a certain project, the tests for the projects that have not yet been executed will not get executed.
The --continue can be useful in this case, it changes the default behavior and forces Gradle to continue executing all tasks even if some of them failed.
In this issue it is very well explained https://youtrack.jetbrains.com/issue/KT-49858p
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.
In my Gradle project, I define an additional sourceSet.
sourceSets {
sample {
java {
srcDir 'sample/java'
}
compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath
}
}
I then add a task:
task sampleJar(type: Jar) {
classifier 'sample'
from sourceSets.sample.output
}
artifacts {
archives sampleJar
}
If I do > gradle build the additional jar file builds from the additional source set. However, if I do > gradle jar, it doesn't. any reason why?
When I go through the output messages, I see:
gradle build has sampleJar in the Tasks to be executed:
but
gradle jar doesn't.
But unsure as to why?
Because jar is just the task that assembles the main jar file.
build, on the other hand, is the top-level life-cycle task, which depends on assemble. And assemble is documented as
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration.
Since your sampleJar pecisely creates an artifact attached to the archives configuration, assemble, and thus build depends on it.
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.
I have a gradle project in scala to which I am trying to add sonarRunner. Things are working fine, but compilation and test is running twice on executing task sonarRunner as SonarRunner adds test as a dependency. However, scoverage always recompiles the code for instrumentation and then runs the tests.
Is there a way to avoid running the tests twice. I tried ignoring tests in sonarRunner task, but then test task won't run at all. I tried guarding ignore test in sonarRunner task by something like below mentioned code and that basically does not do anything.
task sonarRunner {
test {
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask('sonarRunner')) {
enabled = false
}
}
}
dependsOn 'reportScoverage'
}
Any suggestion will be helpful.
You can remove the dependency to the test task by overriding the sonarRunner task's dependencies.
tasks.sonarRunner {
dependsOn = []
}