What is the correct (i.e. lazy, unnecessary-task-configuration-avoiding, up-to-date-checking, configuration-cache-compatible) API to use for adding the artifacts of a custom configuration (it’s a resolvable configuration) to the JavaExec and Test task classpaths?
The JavaExec task has a classpath() method that accepts and seems to work with a NamedDomainObjectProvider<Configuration> from configurations.existing. This is somewhat working for me—except not with the run task of the Application plugin if I apply it in the plugins { } block ordered before my precompiled script plugin with this configuration—so I want to be sure I’m truly using the API correctly.
The Test task seems to lack that method, and just has a classpath property. I can’t seem to find the right incantation for appending to the property that resolves the artifacts for the direct and transitive dependencies of my custom configuration, while at the same time not triggering a configuration cache violation by referencing my custom Configuration from the task action.
Here is my latest attempt (no configuration cache warnings, but the custom configuration artifacts aren’t being added to the test classpath):
tasks {
// The variant-aware custom resources resolvable configuration.
val customClasspath by configurations.existing
withType<JavaExec>().configureEach {
// For the `JavaExec` tasks created by the Gradle Application Plugin,
// the Spring Boot Gradle Plugin, or generated by IntelliJ...
if (name in listOf("run", "bootRun", "bootTestRun") ||
name.endsWith("main()")) {
// Add the custom resources to the runtime classpath.
classpath(customClasspath)
}
}
withType<Test>().configureEach {
val customClasspathFileCollection =
customClasspath.get().fileCollection()
doFirst {
// Add the custom resources to the classpath for all `Test` tasks.
classpath = classpath.plus(customClasspathFileCollection)
}
}
}
Full reproducer: GitHub - sdkotlin/sd-kotlin-spring-talks at ea616f42d50e41bc6a22873a9f7d56fa07181a54
- Configuration: sd-kotlin-spring-talks/build-logic/src/main/kotlin/org.sdkotlin.buildlogic.custom-resources-convention.gradle.kts at ea616f42d50e41bc6a22873a9f7d56fa07181a54 · sdkotlin/sd-kotlin-spring-talks · GitHub
- Failing test (remove
@Disabledto run): sd-kotlin-spring-talks/subprojects/app/src/it/kotlin/org/sdkotlin/springdemo/CustomResourcesTransitiveIT.kt at ea616f42d50e41bc6a22873a9f7d56fa07181a54 · sdkotlin/sd-kotlin-spring-talks · GitHub