Skip to content

Commit

Permalink
Support overriding versions from any imported catalog (#1002)
Browse files Browse the repository at this point in the history
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 `<catalog>-override.versions.toml`.
  • Loading branch information
melix authored May 27, 2024
1 parent c5be326 commit ce870a8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

public abstract class MicronautCatalogSettingsPlugin implements Plugin<Settings> {
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();

Expand All @@ -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);
}
}));
}
});
});
}
));
}
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit ce870a8

Please sign in to comment.