From bf92f662cefc0c061dd9b02f96dfadeab3202396 Mon Sep 17 00:00:00 2001 From: Nick Cross Date: Wed, 14 Aug 2024 15:02:33 +0100 Subject: [PATCH] NCLSUP-1107 Handle versions with strict constraints --- ...eProjectWithMavenPluginFunctionalTest.java | 7 +++++- .../build.gradle | 7 ++++++ .../manipulation.json | 5 ++++ .../actions/OverrideDependenciesAction.java | 24 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/manipulation/src/functTest/java/org/jboss/gm/manipulation/SimpleProjectWithMavenPluginFunctionalTest.java b/manipulation/src/functTest/java/org/jboss/gm/manipulation/SimpleProjectWithMavenPluginFunctionalTest.java index 1bbb0db8..58b821ad 100644 --- a/manipulation/src/functTest/java/org/jboss/gm/manipulation/SimpleProjectWithMavenPluginFunctionalTest.java +++ b/manipulation/src/functTest/java/org/jboss/gm/manipulation/SimpleProjectWithMavenPluginFunctionalTest.java @@ -22,6 +22,7 @@ import org.junit.rules.TestRule; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; public class SimpleProjectWithMavenPluginFunctionalTest { @@ -69,6 +70,9 @@ public void ensureProperPomGeneratedForLegacyPlugin() throws IOException, URISyn final String repoPathToPom = PATH_IN_REPOSITORY.resolve(ARTIFACT_NAME + ".pom").toString(); + assertTrue(systemOutRule.getLog().contains( + "Replacing strictly with forced version for ch.qos.logback:logback-classic:1.1.3 with ch.qos.logback:logback-classic:1.1.2")); + // verify installed artifacts verifyArtifacts(m2Directory); verifyPom(m2Directory, repoPathToPom, alignment); @@ -89,7 +93,8 @@ private void verifyPom(File repoDirectory, String pathToPom, ManipulationModel a TestUtils.getAlignedTuple(module, "commons-lang3", "3.8.1"), TestUtils.getAlignedTuple(module, "hibernate-core"), TestUtils.getAlignedTuple(module, "undertow-core"), - TestUtils.getAlignedTuple(module, "junit", "4.12")); + TestUtils.getAlignedTuple(module, "junit", "4.12"), + TestUtils.getAlignedTuple(module, "logback-classic", "1.1.2")); assertThat(modelAndModule.getLeft().getOrganization().getName()).isEqualTo("JBoss"); assertThat(modelAndModule.getLeft().getLicenses().get(0).getName()).isEqualTo("Apache License, Version 2.0"); } diff --git a/manipulation/src/functTest/resources/simple-project-with-maven-plugin/build.gradle b/manipulation/src/functTest/resources/simple-project-with-maven-plugin/build.gradle index 2585afa2..daa0f59b 100644 --- a/manipulation/src/functTest/resources/simple-project-with-maven-plugin/build.gradle +++ b/manipulation/src/functTest/resources/simple-project-with-maven-plugin/build.gradle @@ -13,6 +13,13 @@ repositories { } dependencies { + testImplementation('ch.qos.logback:logback-classic') { + version { + strictly '1.1.3' + } + } + + implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1' implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1' implementation group: 'org.hibernate', name: 'hibernate-core', version: '5.3.7.NOT_A_VALID_VERSION' implementation group: 'io.undertow', name: 'undertow-core', version: '2.0.15.NOT_A_VALID_VERSION' diff --git a/manipulation/src/functTest/resources/simple-project-with-maven-plugin/manipulation.json b/manipulation/src/functTest/resources/simple-project-with-maven-plugin/manipulation.json index 5e7f5f75..ee526dc3 100644 --- a/manipulation/src/functTest/resources/simple-project-with-maven-plugin/manipulation.json +++ b/manipulation/src/functTest/resources/simple-project-with-maven-plugin/manipulation.json @@ -12,6 +12,11 @@ "groupId": "io.undertow", "artifactId": "undertow-core", "version": "2.0.15.Final" + }, + "ch.qos.logback:logback-classic:1.1.3": { + "groupId": "ch.qos.logback", + "artifactId": "logback-classic", + "version": "1.1.2" } } } diff --git a/manipulation/src/main/java/org/jboss/gm/manipulation/actions/OverrideDependenciesAction.java b/manipulation/src/main/java/org/jboss/gm/manipulation/actions/OverrideDependenciesAction.java index 59a164a4..9ad40673 100644 --- a/manipulation/src/main/java/org/jboss/gm/manipulation/actions/OverrideDependenciesAction.java +++ b/manipulation/src/main/java/org/jboss/gm/manipulation/actions/OverrideDependenciesAction.java @@ -4,10 +4,12 @@ import java.util.Map; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef; import org.gradle.api.Action; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.ExternalModuleDependency; import org.gradle.api.artifacts.ModuleVersionSelector; import org.gradle.api.internal.artifacts.DefaultModuleVersionSelector; import org.gradle.api.logging.Logger; @@ -78,6 +80,28 @@ public void execute(Project project) { logger.debug("Forced resolution strategy is now {} ", forced); configuration.getResolutionStrategy().setForcedModules(forced.toArray()); } + + configuration.getAllDependencies().forEach(d -> { + if (d instanceof org.gradle.api.artifacts.ExternalModuleDependency) { + ExternalModuleDependency externalModuleDependency = (ExternalModuleDependency) d; + if (StringUtils.isNotEmpty(externalModuleDependency.getVersionConstraint().getStrictVersion())) { + logger.debug("Found version constraint of {} for {}", + externalModuleDependency.getVersionConstraint(), d); + final ProjectVersionRef requestedGAV = withGAV( + externalModuleDependency.getModule().getGroup(), + externalModuleDependency.getModule().getName(), + externalModuleDependency.getVersionConstraint().getStrictVersion()); + final ProjectVersionRef aligned = alignedDependencies.get(requestedGAV.toString()); + if (aligned != null) { + logger.info("Replacing strictly with forced version for {} with {}", requestedGAV, aligned); + forced.add( + new DefaultModuleVersionSelector(externalModuleDependency.getModule().getGroup(), + externalModuleDependency.getModule().getName(), aligned.getVersionString())); + } + configuration.getResolutionStrategy().setForcedModules(forced.toArray()); + } + } + }); } }); }