Heyho, I have a gradle multi module project. Some ...
# gradle
b
Heyho, I have a gradle multi module project. Some of my modules are using Kotlin for jvm. Some may use Kotlin JS. I’d like to specify JUnitPlatform for all modules that have the
KotlinPlatformJvmPlugin
applied. This is what I tried so far in my root `build.gradle.kts`:
Copy code
subprojects {
    plugins.withType<KotlinPlatformJvmPlugin> {
        dependencies {
            "testImplementation"(kotlin("test-junit5"))
        }

        tasks.withType<Test> {
            useJUnitPlatform()
        }
    }
}
and
Copy code
subprojects {
     if (pluginManager.hasPlugin("kotlin-platform-jvm")) {
        // dependency
        // useJunit blabla
     }
}
Both do not work. Is there a way to do this?
1
a
I think you’ll have to wrap at least the call to pluginManager.hasPlugin in afterEvaluate { } so that the check is done after the project’s build script has been run (and declared what plugins it is using), not at the time the root build script is run
b
Works like a charm:
Copy code
afterEvaluate {
        if (pluginManager.hasPlugin("org.jetbrains.kotlin.jvm")) {
            tasks.withType<Test> {
                useJUnitPlatform()
            }
        }
    }
Thank you very much 🙂
v
But actually you should avoid using
afterEvaluate
wherever you can and instead use reactive APIs. Your first example should work fine I think, if the class was the correct one. Which can easily be the wrong one if you for example use script plugins and so on as the class could come from a different class loader. So maybe you want to try using
plugins.withId("org.jetbrains.kotlin.jvm") { ... }
instead.
2
❤️ 1
1
j
You could also define a precompiled script plugin, containing all the conventions shared by your Kotlin JVM projects (i.e. using JUnit 5 for example) and apply it on all those JVM projects. See https://docs.gradle.org/current/samples/sample_convention_plugins.html for an example.
1