Skip to content

Commit

Permalink
NCLSUP-1107 Handle versions with strict constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Aug 14, 2024
1 parent 056f1c5 commit 477f0ca
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
});
}
});
}
Expand Down

0 comments on commit 477f0ca

Please sign in to comment.