I use @Vampire 's hack to access libs
in plugins. It works in actual build, but fails in unit/functional tests.
build-src/build.gradle.kts
dependencies {
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}
build-src/src/main/kotlin/my/Libs.kt
// Is this okay if I want to reuse?
internal val Project.libs get() = the<LibrariesForLibs>()
build-src/src/main/kotlin/my/convention.gradle.kts
plugins {
android
}
dependencies {
implementation(libs.someLib)
}
build-src/src/test/kotlin/my/ConventionTest.kt
class ConventionTest {
@Test fun test() {
val project = ProjectBuilder.builder().build().also {
it.plugins.apply("my.convention")
}
// ...
}
}
build-src/src/test/kotlin/my/ConventionFunctionalTest.kt
class ConventionFunctionalTest {
@field:TempDir
lateinit var projectDir: File
private val buildFile by lazy { projectDir.resolve("build.gradle.kts") }
private val libsVersions by lazy { projectDir.resolve("gradle/libs.versions.toml") }
@Test
fun test() {
buildFile.writeText(
"""
plugins {
id("my.convention")
}
""".trimIndent()
)
libsVersions.apply { parentFile.mkdir() }.writeText(
"""
[versions]
someLib = "1.0.0"
[libraries]
someLib = { module = "org.example:some-lib", version.ref = "someLib" }
""".trimIndent()
)
val result = GradleRunner.create()
.withPluginClasspath()
.withArguments("-q", "help")
.withProjectDir(projectDir)
.build()
// ...
}
}
This is the error message of ConventionTest
:
Caused by: org.gradle.api.UnknownDomainObjectException: Extension of type 'LibrariesForLibs' does
not exist. Currently registered extension types: [ExtraPropertiesExtension, BasePluginExtension,
DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaToolchainService,
JavaPluginExtension, BaseAppModuleExtension, ApplicationAndroidComponentsExtension,
NamedDomainObjectContainer<BaseVariantOutput>]
This is the error message of ConventionFunctionalTest
:
* What went wrong:
An exception occurred applying plugin request [id: 'my.convention']
> Failed to apply plugin 'my.convention'.
> Extension of type 'LibrariesForLibs' does not exist. Currently registered extension types:
[ExtraPropertiesExtension, LibrariesForLibs, VersionCatalogsExtension, BasePluginExtension,
DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaToolchainService,
JavaPluginExtension, BaseAppModuleExtension, ApplicationAndroidComponentsExtension,
NamedDomainObjectContainer<BaseVariantOutput>]
Is there another workaround for this?
Thank you,