Using Version Catalog Plugins in Convention Plugins

I am uging “Gradle 8.1.1.” I have a question about using a plugin declared in a version-catalog inside a pre-compiled convention plugin? The version-catalog is published to a maven private repo with URL Rubens Private Maven Repo from a Git project found at BitBucket at
gradle-catalog

Below is my consuming Gradle convention plugin:
├── LICENSE.txt
├── README.md
├── build.gradle.kts
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── src
└── main
└── kotlin
└── com.rubensgomes.gradle.kotlin-conventions.gradle.kts

settings.gradle.kts:

rootProject.name = "gradle-conventions"

// centralized plugin dependency repositories
pluginManagement {
    // property defined in ~/.gradle/gradle.properties
    val myMavenRepoReadUrl: String by settings
    repositories {
        gradlePluginPortal()
        mavenCentral()
        // Rubens Gomes private maven consuming artifacts repo
        maven ( url = myMavenRepoReadUrl )
    }
}

// centralized library dependency repositories
dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            from("com.rubensgomes.gradle:gradle-catalog:1.0.0")
        }
    }

    // property defined in ~/.gradle/gradle.properties
    val myMavenRepoReadUrl: String by settings
    repositories {
        // add gradlePluginPortal so external plugins can be resolved
        // in the dependencies section
        gradlePluginPortal()
        mavenCentral()
        // Rubens Gomes private maven consuming artifacts repo
        maven ( url = myMavenRepoReadUrl )
    }

}

plugins {
    // following plugin must be applied below to be used by the gradle build.
    id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
}

build.gradle.kts:

plugins {
    `java`
    `maven-publish`
    `kotlin-dsl`
}

group="com.rubensgomes.gradle"
version="1.0.0-SNAPSHOT"

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
        vendor.set(JvmVendorSpec.AMAZON)
    }
}

publishing {
    publications {
        create<MavenPublication>("Kotlin-Gradle-Conventions") {
            from(components["java"])
            pom {
                name.set("Gradle Kotlin Conventions")
                description.set("Shareable configurations, plugins, tasks, and libraries to be used accross Kotlin development projects")
                url.set("https://bitbucket.org/rubens-gomes/gradle-catalog/")
                licenses {
                    license {
                        name.set("The Apache License, Version 2.0")
                        url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
                    }
                }
                developers {
                    developer {
                        id.set("rubenssgomes")
                        name.set("Rubens Gomes")
                        email.set("rubens.s.gomes@gmail.com")
                    }
                }
                scm {
                    developerConnection.set("scm:git:ssh:git@bitbucket.org:rubens-gomes/gradle-conventions.git")
                }
            }
        }
    }
    repositories {
        // defined in ~/.gradle/gradle.properties
        val myMavenRepoWriteUrl: String by project
        val myMavenRepoWriteUsername: String by project
        val myMavenRepoWritePassword: String by project
        maven {
            // private maven publishing artifacts repo
            url = uri(myMavenRepoWriteUrl)
            credentials {
                username = myMavenRepoWriteUsername
                password = myMavenRepoWritePassword
            }
        }
    }
}

tasks.publish {
    dependsOn("check")
}

src/main/kotlin/com.rubensgomes.gradle.kotlin-conventions.gradle.kts:

plugins {
    `java`
    // Following does not work, and is my questions?
    alias(libs.plugins.kotlin.jvm)
    alias(libs.plugins.spotless)
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
        vendor.set(JvmVendorSpec.AMAZON)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // SLF4J + Log4J Implementation
    implementation(libs.bundles.logging)
    // kotlin testing API + Junit engine
    implementation(libs.bundles.testing)
}

tasks.named<Test>("test") {
    useJUnitPlatform()
}

// Java/Kotlin code formatter
spotless {
    java {
        importOrder()
        googleJavaFormat()
        formatAnnotations()
        removeUnusedImports()
    }
    kotlin {
        ktfmt()
    }
}

Even with my hack-around described in the associated issue ticket, you cannot use the version catalog for plugins in convention plugins.

You anyway need to declare the dependency on those plugins in build.gradle.kts of the project that builds the convention plugin, and then just use the String ID to apply it in the convention plugin.

In my build.gradle.kts I am getting the below build error. I am assuming that’s because in my TOML Found Here
those dependencies are actually declared in the version catalog as “plugins”.

How do I fix this? And once I fix that, could you verify that my src/main/kotlin/com.rubensgomes.gradle.kotlin-conventions.gradle.kts plugins section is correct?

Selected primary task 'build' from project :
Could not execute [class org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatHandler.buildFinished]

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':compileJava'.
> Could not resolve all dependencies for configuration ':compileClasspath'.
   > Cannot convert the provided notation to an object of type Dependency: org.jetbrains.kotlin.jvm:1.8.21.

What you are after is Accept plugin declarations from version catalog also as libraries · Issue #17963 · gradle/gradle · GitHub which also contains work-arounds.

That is a description of my hack-around and what was asked for is not doable with it.

1 Like

You’re completely right, I meant that was you described is doable and provide gradle - Gradle7 Version Catalog: How to use it with buildSrc? - Stack Overflow just for further reference - sorry for the mixup, I want to put the blame on the late hour and internet as a medium of communication :smiley:

1 Like