From ce870a8aa5545acfec04d8ee66ea9bba286bd847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Champeau?= Date: Mon, 27 May 2024 13:12:18 +0200 Subject: [PATCH] Support overriding versions from any imported catalog (#1002) The Micronaut Gradle plugin provided a way to override versions defined in the Micronaut platform catalog (named "mn") using a specific file called `gradle/mn-override.versions.toml`. However, there was no such mechanism for other catalogs imported from a remote repository (for example a user may import a corporate catalog file). This commit expands our override logic so that it can handle any catalog by convention. The override file name is `-override.versions.toml`. --- .../VersionOverrideFunctionalTest.groovy | 70 +++++++++++++++++++ .../MicronautCatalogSettingsPlugin.java | 33 ++++----- 2 files changed, 87 insertions(+), 16 deletions(-) diff --git a/functional-tests/src/test/groovy/io/micronaut/gradle/settings/VersionOverrideFunctionalTest.groovy b/functional-tests/src/test/groovy/io/micronaut/gradle/settings/VersionOverrideFunctionalTest.groovy index f62ee746..50d7b76d 100644 --- a/functional-tests/src/test/groovy/io/micronaut/gradle/settings/VersionOverrideFunctionalTest.groovy +++ b/functional-tests/src/test/groovy/io/micronaut/gradle/settings/VersionOverrideFunctionalTest.groovy @@ -48,6 +48,76 @@ micronaut-core = "2048" } + def "can override the any version from user-defined catalogs"() { + given: + file("gradle").mkdirs() + file("gradle/my.versions.toml") << """ +[versions] +foo = "1.0" + +[libraries] +foo = { module = "com.group:foo", version.ref = "foo" } + """ + settingsFile << """ + plugins { + id 'io.micronaut.platform.catalog' + } + + rootProject.name = 'hello-world' + + dependencyResolutionManagement { + versionCatalogs { + create("my") { + from(files("gradle/my.versions.toml")) + } + } + } + """ + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + } + + micronaut { + runtime "netty" + testRuntime "junit5" + } + + $repositoriesBlock + mainClassName="example.Application" + + configurations { + dummy + } + + dependencies { + dummy my.foo + } + + tasks.register("checkOverride") { + doLast { + println configurations.dummy.files() + } + } + """ + file("gradle.properties") << """ + micronautVersion=$micronautVersion + """ + + file('gradle').mkdir() + file('gradle/my-override.versions.toml') << """ +[versions] +foo = "2048" +""" + + when: + def result = fails('checkOverride') + + then: + result.output.contains('Could not find com.group:foo:2048') + + } + private File writeExampleClass() { def javaFile = testProjectDir.newFile("src/test/java/example/ExampleTest.java") diff --git a/platform-catalog-plugin/src/main/java/io/micronaut/gradle/catalog/MicronautCatalogSettingsPlugin.java b/platform-catalog-plugin/src/main/java/io/micronaut/gradle/catalog/MicronautCatalogSettingsPlugin.java index abde21a6..633d5de3 100644 --- a/platform-catalog-plugin/src/main/java/io/micronaut/gradle/catalog/MicronautCatalogSettingsPlugin.java +++ b/platform-catalog-plugin/src/main/java/io/micronaut/gradle/catalog/MicronautCatalogSettingsPlugin.java @@ -37,7 +37,7 @@ public abstract class MicronautCatalogSettingsPlugin implements Plugin { private static final Logger LOGGER = Logging.getLogger(MicronautCatalogSettingsPlugin.class); - public static final String MN_OVERRIDE_VERSIONS_TOML_FILE = "mn-override.versions.toml"; + public static final String OVERRIDE_VERSIONS_TOML_FILE = "override.versions.toml"; abstract RegularFileProperty getDefaultGradleVersionCatalogFile(); @@ -61,21 +61,22 @@ private static void registerMicronautVersionCatalog(Settings settings, Provider< }); } } - drm.versionCatalogs(vcs -> - vcs.create("mn", catalog -> { - catalog.from("io.micronaut.platform:micronaut-platform:" + micronautVersion); - File catalogOverrideFile = new File(settings.getSettingsDir(), "gradle/" + MN_OVERRIDE_VERSIONS_TOML_FILE); - if (catalogOverrideFile.exists()) { - LenientVersionCatalogParser parser = new LenientVersionCatalogParser(); - try (InputStream in = new FileInputStream(catalogOverrideFile)) { - parser.parse(in); - VersionCatalogTomlModel model = parser.getModel(); - fixupMicronautCatalogWith(catalog, model); - } catch (IOException e) { - throw new RuntimeException(e); - } + drm.versionCatalogs(vcs -> { + vcs.create("mn", catalog -> catalog.from("io.micronaut.platform:micronaut-platform:" + micronautVersion)); + vcs.configureEach(catalog -> { + File catalogOverrideFile = new File(settings.getSettingsDir(), "gradle/" + catalog.getName() + "-" + OVERRIDE_VERSIONS_TOML_FILE); + if (catalogOverrideFile.exists()) { + LenientVersionCatalogParser parser = new LenientVersionCatalogParser(); + try (InputStream in = new FileInputStream(catalogOverrideFile)) { + parser.parse(in); + VersionCatalogTomlModel model = parser.getModel(); + fixupMicronautCatalogWith(catalog, model); + } catch (IOException e) { + throw new RuntimeException(e); } - })); + } + }); + }); } )); } @@ -112,7 +113,7 @@ private static void fixupMicronautCatalogWith(VersionCatalogBuilder catalog, } }); if (!model.getLibrariesTable().isEmpty()) { - LOGGER.warn("The " + MN_OVERRIDE_VERSIONS_TOML_FILE + " file should only contain entries overriding the Micronaut Platform versions. Use your own version catalog to declare new libraries"); + LOGGER.warn("The " + OVERRIDE_VERSIONS_TOML_FILE + " file should only contain entries overriding the Micronaut Platform versions. Use your own version catalog to declare new libraries"); } }