What's the currently recommended way of modularizi...
# community-support
e
What's the currently recommended way of modularizing
build.gradle.kts
using the version catalog? I just want to hoist out a plugin's configuration. I tried convention plugins but get unresolved references when I try to use
libs
or the plugin itself (
jooq
).
buildSrc/settings.gradle.kts
Copy code
dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            from(files("../gradle/libs.versions.toml"))
        }
    }
}
buildSrc/build.gradle.kts
Copy code
plugins {
    `kotlin-dsl`
    `kotlin-dsl-precompiled-script-plugins`
}

repositories {
    mavenCentral()
}
buildSrc/src/main/kotlin/jooq.gradle.kts
Copy code
plugins {
    alias(libs.plugin.jooq.codegen)
}
dependencies {
    jooqCodegen(libs.jooq.meta.extensions)
    jooqCodegen(libs.jooq.meta.kotlin)
}
jooq { [configs] }
I get
Unresolved reference: libs
for the last file.
a
version catalogs aren't available in plugins in buildSrc - but there are workarounds here https://github.com/gradle/gradle/issues/15383
v
Version catalogs are available there. "Just" the type-safe accessors (
libs...
) for Kotlin DSL are not. In Groovy DSL it just works. In Kotlin DSL you can use the type-unsafe string-y API via
versionCatalogs
extension. Or you can use my hack-around documented in the issue Adam linked to, to use the type-safe accessors.
e
I am trying to use the workaround above but I can't invoke plugins{} or dependencies{} in the Kotlin DSL:
Copy code
plugins {
    alias(libs.plugins.jooq.codegen)
}

dependencies {
    jooqCodegen(libs.jooq.meta.extensions)
    jooqCodegen(libs.jooq.meta.kotlin)
}
I get an unresolved reference, and if I remove this block, the actual
jooq {}
block that follows it fails. I note that the IDE complains about the libs inside the alias with > 'val libs: LibrariesForLibs' can't be called in this context by implicit receiver. Use the explicit one if necessary. I don't know why it's so hard to merely include a file 😐
v
In the plugins block my hack-around indeed cannot work for technical reasons unfortunately. There just use the plugin id as string. The version is not coming from there anyway.
But in the dependencies block it works if you properly followed all parts of the hack-around
e
Following the manual I tried replacing
libs
variously with "org.jooq.jooq-codegen-gradle", "jooq-codegen-gradle", etc.
Copy code
repositories {
    mavenCentral()
}

plugins {
    id("org.jooq.jooq-codegen-gradle") // version "3.19.9"
}
but I got > Plugin [id: 'org.jooq.jooq-codegen-gradle'] was not found in any of the following sources: > > - Gradle Core Plugins (plugin is not in 'org.gradle' namespace) > - Plugin Repositories (plugin dependency must include a version number for this source) If I include the version number it fails with
Copy code
Plugin requests from precompiled scripts must not include a version number. Please remove the version from the offending request and make sure the module containing the requested plugin 'org.jooq.jooq-codegen-gradle' is an implementation dependency of project ':gradle-configs'.
What is the right string argument to id()? This is the plugin on Maven.
v
The ID is fine. But you have to depend on it as dependency in the build script of your plugin project
e
How, like this in the included build's
build.gradle.kts
?
Copy code
dependencies {
    implementation("org.jooq:jooq-codegen-gradle:3.19.9")
}
This hard codes the version and yields this error:
Copy code
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class org.jooq.codegen.gradle.MetaExtensions.ConfigurationExtension, unresolved supertypes: org.jooq.meta.jaxb.Configuration
...
If I use
implementation(libs.plugins.jooq.codegen)
I get
Copy code
Cannot convert the provided notation to an object of type Dependency: org.jooq.jooq-codegen-gradle:3.19.9
v
That is not the Gradle plugin, but the actual codegen tool. Either
org.jooq:jooq-codegen-gradle:...
or via the marker artifact that is also used to translate plugin ID from
plugins { ... }
block to a dependency in normal build scripts
org.jooq.jooq-codegen-gradle:org.jooq.jooq-codegen-gradle.gradle.plugin:...
e
What finally worked was
Copy code
implementation("org.jooq:jooq-codegen-gradle:${libs.versions.jooq.get()}")
implementation("org.jooq:jooq-meta-extensions:${libs.versions.jooq.get()}")
which is not ideal. Gradle's DX here needs some improvement.
v
Why don't you make entries for it in the version catalog and use it?
e
It is in there. My previous attempt was
implementation(libs.plugins.jooq.codegen)
with libs coming from the catalog. The latest incarnation uses the catalog too, but just for the version.
v
Yeah, that is waiting for https://github.com/gradle/gradle/issues/17963 to work. But you can have those entries you constructed with just the version from the catalog as normal library entries and use those.
e
What do you mean by "normal library entries"? Can you correct my example:
Copy code
implementation("org.jooq:jooq-codegen-gradle:${libs.versions.jooq.get()}")
v
implementation(libs.jooq.codegen.gradle)
, if you added a "normal library entry" to the version catalog for it, as opposed to a plugin entry
🙏 1
1
e
Thanks, problem solved. This was way harder than it had any right to be. Importing a file shouldn't be rocket science.
v
Feel free to open a pull request or feature request if you think the docs are not clear enough. :-)