Skip to content

Commit

Permalink
Consider constraints when copy configuration to determine exclusions
Browse files Browse the repository at this point in the history
Closes gh-340
  • Loading branch information
wilkinsona committed Sep 30, 2022
1 parent 1a869a8 commit 5be22d6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Expand Up @@ -27,6 +27,8 @@
import io.spring.gradle.dependencymanagement.internal.DependencyManagementConfigurationContainer.ConfigurationConfigurer;
import org.gradle.api.Action;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencyConstraintSet;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.ResolvableDependencies;
Expand Down Expand Up @@ -106,10 +108,7 @@ private void applyMavenExclusions(ResolvableDependencies resolvableDependencies)
}

private Set<DependencyCandidate> findExcludedDependencies() {
DependencySet allDependencies = this.configuration.getAllDependencies();
Configuration configurationCopy = this.configurationContainer.newConfiguration(this.configurationConfigurer,
allDependencies.toArray(new org.gradle.api.artifacts.Dependency[allDependencies.size()]));
ResolutionResult resolutionResult = configurationCopy.getIncoming().getResolutionResult();
ResolutionResult resolutionResult = copyConfiguration().getIncoming().getResolutionResult();
ResolvedComponentResult root = resolutionResult.getRoot();
final Set<DependencyCandidate> excludedDependencies = new HashSet<DependencyCandidate>();
resolutionResult.allDependencies(new Action<DependencyResult>() {
Expand All @@ -136,6 +135,20 @@ else if (dependencyResult instanceof UnresolvedDependencyResult) {
return excludedDependencies;
}

private Configuration copyConfiguration() {
DependencySet allDependencies = this.configuration.getAllDependencies();
Configuration configurationCopy = this.configurationContainer.newConfiguration(this.configurationConfigurer,
allDependencies.toArray(new Dependency[allDependencies.size()]));
try {
DependencyConstraintSet constraints = this.configuration.getAllDependencyConstraints();
configurationCopy.getDependencyConstraints().addAll(constraints);
}
catch (NoSuchMethodError ex) {
// Continue
}
return configurationCopy;
}

private Set<DependencyCandidate> determineIncludedComponents(ResolvedComponentResult root,
Map<String, Exclusions> pomExclusionsById) {
LinkedList<Node> queue = new LinkedList<Node>();
Expand Down
Expand Up @@ -462,6 +462,13 @@ void exclusionThatIsMalformedIsTolerated() {
"commons-logging-1.1.3.jar");
}

@Test
void exclusionsAreAppliedToDependenciesVersionedWithConstraints() {
this.gradleBuild.runner().withArguments("resolve").build();
assertThat(readLines("resolved.txt")).containsOnly("transitive-exclude-1.0.jar",
"spring-beans-4.1.2.RELEASE.jar", "spring-tx-4.1.2.RELEASE.jar", "spring-core-4.1.2.RELEASE.jar");
}

private void writeLines(Path path, String... lines) {
try {
Path resolvedPath = this.gradleBuild.runner().getProjectDir().toPath().resolve(path);
Expand Down
@@ -0,0 +1,27 @@
plugins {
id "io.spring.dependency-management"
id "java"
}

repositories {
mavenCentral()
maven {
url file("maven-repo")
}
}

dependencies {
implementation 'test:transitive-exclude'
constraints {
implementation 'test:transitive-exclude:1.0'
}
}

task resolve {
doFirst {
def files = project.configurations.compileClasspath.resolve()
def output = new File("${buildDir}/resolved.txt")
output.parentFile.mkdirs()
files.collect { it.name }.each { output << "${it}\n" }
}
}

0 comments on commit 5be22d6

Please sign in to comment.