Hi! I have a issue when trying to configure JaCoCo...
# gradle
m
Hi! I have a issue when trying to configure JaCoCo. In plugin section i have added
jacoco
. Then I have added that reports should be finalized after test runs
Copy code
withType<Test> {
        useJUnitPlatform()
        testLogging {
            exceptionFormat = FULL
            events("passed", "skipped", "failed")
        }

        finalizedBy("jacocoTestReport") // report is always generated after tests run

        // For å øke hastigheten på build kan vi benytte disse metodene
        maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
        reports.forEach { report -> report.required.value(false) }
    }
And when trying to configure the
jacocoTestReport
tasks i get error that it could not find
report
? Anyone know what I am missing ?
s
I would expect
withType
to have a type parameter, e.g.
Copy code
withType<JacocoReport>()
v
Probably not compatible with Kotlin sources?
Btw. do not use
withType<...> { ... ]
but
withType<...>().configureEach { ... }
or you work against task configuration avoidance.
j
Why
withType<...> { ... }
is not using internally
configureEach
in 8.0? I hope intuitive APIs start to do the right things
v
Backwards compatibility If they change that, it could easily break existing builds. And not necessarily in obvious ways. It could also just result in sneaky behavior change that is not easily recognized for example
m
@Vampire what do you mean by not compatible with Kotlin sources? Is there any configuration to fix it?
v
I have no idea, I didn't yet use JaCoCo with Kotlin. But I wouldn't expect a magical configuration switch to fix an incompatibility that I guessed. 😄
m
I see. Thank you 🙂
But you mention something about not using `withType<...> { ... }`but `withType<...>().configureEach { ... }`instead? So you mean this gradle config is working against task configuration avoidance?
Copy code
tasks {

    withType<KotlinCompile> {
        compilerOptions.jvmTarget.set(JVM_17)
    }

    withType().named("buildFatJar") {
        ktor {
            fatJar {
                archiveFileName.set("app.jar")
            }
        }
    }

    withType().named("jar") {
        enabled = false
    }

    withType<Test> {
        useJUnitPlatform()
        testLogging {
            exceptionFormat = FULL
            events("passed", "skipped", "failed")
        }


        // For å øke hastigheten på build kan vi benytte disse metodene
        maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
        reports.forEach { report -> report.required.value(false) }
    }

    withType<Wrapper> {
        gradleVersion = "7.6"
    }
}
@Vampire you mean like this?
Copy code
tasks {

    withType<KotlinCompile>().configureEach {
        compilerOptions.jvmTarget.set(JVM_17)
    }

    withType().named("buildFatJar") {
        ktor {
            fatJar {
                archiveFileName.set("app.jar")
            }
        }
    }

    withType().named("jar") {
        enabled = false
    }

    withType<Test>().configureEach {
        useJUnitPlatform()
        testLogging {
            exceptionFormat = FULL
            events("passed", "skipped", "failed")
        }


        // For å øke hastigheten på build kan vi benytte disse metodene
        maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
        reports.forEach { report -> report.required.value(false) }
    }

    withType<Wrapper>().configureEach {
        gradleVersion = "7.6"
    }
}
v
I don't know what you mean with "backwards compatibility" in this context. But yes, this works against task configuration avoidance. You can easily see this for example in a build scan if you execute a build without compiling Kotlin code or running tests or writing the wrapper, you will then see at those tasks still got realized and configured which is because of the usage of
withType<...> { ... }
. And I already told you what to use instead, so what more example should I give? Btw. using
withType()
without giving any type is pretty useless, that's just a waste of time and clutters the code. Just omit it. And for tasks that are registered in plugins applied in the
plugins { ... }
you also do not need to use
named
, but you can use the type-safe accessors that are generated for them, so just use their name like
tasks.jar { ... }
or
tasks { jar { ... } }
.
withType
just makes sense if you want to get tasks by type for example to configure all tasks of a specific type.