Skip to content

Commit

Permalink
Avoid excluding parent poms
Browse files Browse the repository at this point in the history
In 3d05983, the fix for gh-310, an attempt was made to prevent
dependencies in a transitive platform dependency from stoping
exclusions from being applied correctly. Unfortunately, it went
to far which led to a dependency's parent pom being excluded. This
could cause resolution failures if that parent contained
dependency management that was required for the child's dependencies
to resolve correctly.

This commit reworks that changes so that the platform dependency
itself is added to the included nodes but to then stop processing.
This prevents the platform dependency itself from being excluded but
ensures that any of its dependencies due not influence the
application of exclusions.

Fixes gh-360
  • Loading branch information
wilkinsona committed Jul 13, 2023
1 parent 19e3971 commit cf49bef
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -135,19 +135,21 @@ private Configuration copyConfiguration() {
private Set<DependencyCandidate> determineIncludedComponents(ResolvedComponentResult root,
Map<String, Exclusions> pomExclusionsById) {
LinkedList<Node> queue = new LinkedList<>();
queue.add(new Node(root, getId(root), new HashSet<>()));
queue.add(new Node(root, getId(root), new HashSet<>(), false));
Set<ResolvedComponentResult> seen = new HashSet<>();
Set<DependencyCandidate> includedComponents = new HashSet<>();
while (!queue.isEmpty()) {
Node node = queue.remove();
includedComponents.add(new DependencyCandidate(node.component.getModuleVersion()));
for (DependencyResult dependency : node.component.getDependencies()) {
if (dependency instanceof ResolvedDependencyResult) {
handleResolvedDependency((ResolvedDependencyResult) dependency, node, pomExclusionsById, queue,
seen);
}
else if (dependency instanceof UnresolvedDependencyResult) {
handleUnresolvedDependency((UnresolvedDependencyResult) dependency, node, includedComponents);
if (!node.platform) {
for (DependencyResult dependency : node.component.getDependencies()) {
if (dependency instanceof ResolvedDependencyResult) {
handleResolvedDependency((ResolvedDependencyResult) dependency, node, pomExclusionsById, queue,
seen);
}
else if (dependency instanceof UnresolvedDependencyResult) {
handleUnresolvedDependency((UnresolvedDependencyResult) dependency, node, includedComponents);
}
}
}
}
Expand All @@ -157,12 +159,10 @@ else if (dependency instanceof UnresolvedDependencyResult) {
private void handleResolvedDependency(ResolvedDependencyResult dependency, Node node,
Map<String, Exclusions> pomExclusionsById, LinkedList<Node> queue, Set<ResolvedComponentResult> seen) {
ResolvedComponentResult child = dependency.getSelected();
if (isPlatform(child)) {
return;
}
String childId = getId(child);
if (!node.excluded(childId) && seen.add(child)) {
queue.add(new Node(child, childId, getChildExclusions(node, childId, pomExclusionsById)));
boolean platform = isPlatform(child);
queue.add(new Node(child, childId, getChildExclusions(node, childId, pomExclusionsById), platform));
}
}

Expand Down Expand Up @@ -225,10 +225,13 @@ private static final class Node {

private final Set<Exclusion> exclusions;

private Node(ResolvedComponentResult component, String id, Set<Exclusion> exclusions) {
private final boolean platform;

private Node(ResolvedComponentResult component, String id, Set<Exclusion> exclusions, boolean platform) {
this.component = component;
this.id = id;
this.exclusions = exclusions;
this.platform = platform;
}

private boolean excluded(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ void constraintsInTransitivePlatformDependenciesDoNotPreventExclusionsFromWorkin
assertThat(readLines("resolved.txt")).noneMatch((line) -> line.contains("junit"));
}

@Test
void resolutionSucceedsWhenDependencyReliesOnDependencyManagementFromItsAncestors() {
this.gradleBuild.runner().withArguments("resolve").build();
assertThat(readLines("resolved.txt")).containsExactly("dependency-management-child-1.0.jar",
"spring-core-5.3.27.jar", "spring-jcl-5.3.27.jar");
}

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

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

dependencies {
implementation("test:dependency-management-child:1.0");
}

tasks.register("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" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>test</groupId>
<artifactId>dependency-management-parent</artifactId>
<version>1.0</version>
<relativePath></relativePath>
</parent>

<artifactId>dependency-management-child</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test</groupId>
<artifactId>dependency-management-grandparent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.27</version>
</dependency>
</dependencies>
</dependencyManagement>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>test</groupId>
<artifactId>dependency-management-grandparent</artifactId>
<version>1.0</version>
<relativePath></relativePath>
</parent>

<artifactId>dependency-management-parent</artifactId>
<packaging>pom</packaging>

</project>

0 comments on commit cf49bef

Please sign in to comment.