I am trying to develop a grade plugin in java to configure version catalogs. The plugin can configure a version catalog in one of two ways.
- Add a file to
libs.versions.toml
to the dependent projectsgradle
folder. - Apply a version catalog as part of the plugin by configuring it something like this. Assume
libs.versions.toml
file is available as part of the plugin source.
toml
file I defined.
[versions]
groovy = "3.0.5"
checkstyle = "8.37"
[libraries]
groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8, 4.0[", prefer="3.9" } }
[bundles]
groovy = ["groovy-core", "groovy-json", "groovy-nio"]
[plugins]
jmh = { id = "me.champeau.jmh", version = "0.6.5" }
Plugin
public class SettingsSupportPlugin implements Plugin<Settings> {
@Override
public void apply(@Nonnull Settings settings) {
configureDependencyResolution(settings);
}
private void configureDependencyResolution(Settings settings) {
copyFileFromJar("libs.versions.toml", "build/libs.versions.toml");
settings.getGradle().rootProject((rootProject) -> {
FileCollection tomlFile = rootProject.files("build/libs.versions.toml");
settings.dependencyResolutionManagement(
dependencyResolutionManagement -> dependencyResolutionManagement.versionCatalogs(
versionCatalogContainer -> versionCatalogContainer.register("libs",
versionCatalogBuilder -> versionCatalogBuilder.from(tomlFile))));
});
}
}
After publishing the plugin, and applying it in dependent projects settings.gradle
file. when I try to access one of the dependency using implementation libs.groovy.core
I run into the issue
> Could not get unknown property 'libs' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
My initial guess was the libs
version catalog is not created. So, I added a libs.versions.toml
file in gradle
directory of the dependent project, along with applying the above plugin (option 1+ 2). This gives me an error
Cannot add a VersionCatalogBuilder with name 'libs' as a VersionCatalogBuilder with that name already exists.
This shows the version catalog
with the name libs
is being created by the plugin
but somehow I am not able to reference it in my dependent project. I am not sure what I am doing wrong here.
Any thoughts?