Skip to content

Commit caad92a

Browse files
committed
Apply exclusions earlier to avoid deprecation warning
Previously, the dependency management plugin used a before resolve action to apply Maven-style exclusions. Gradle performs configuration resolution in two passes and, despite its name, a before resolve action is called after the first pass. With Gradle 8.8 and later, using a before resolve action to configure the exclusions can result in a deprecation warning. The deprecation warning states that support for mutating the dependency attributes of a configuration after it has been resolved has been deprecated. This commit addresses the deprecation warning by configuring the exclusions earlier, in the withDependencies callback. The callback is called before the first pass of the resolution process, thereby avoiding the warning. Configuring the exclusions at this step prevents the exclusions from being configured on a per-dependency basis so they are now configured on the configuration. Without any additional changes, this regresses the fix for gh-21 as the exclusions are now inherited and cause an exclusion in one configuration where it should apply to leak into another configuration where it should not. To overcome this regression, exclusions are only applied to configurations that can be resolved. Typically, these are configurations like compileClasspath, testRuntimeClasspath and so on, that are leaves and are not extended by other configurations, thereby minimizing the risk of any inheritance-related problems. Closes gh-384
1 parent 68f86ea commit caad92a

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

src/main/java/io/spring/gradle/dependencymanagement/internal/DependencyManagementApplier.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
2020
import org.gradle.api.Action;
2121
import org.gradle.api.Project;
2222
import org.gradle.api.artifacts.Configuration;
23-
import org.gradle.api.artifacts.ResolvableDependencies;
23+
import org.gradle.api.artifacts.DependencySet;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

@@ -75,11 +75,11 @@ public void execute(Configuration configuration) {
7575
.getManagedVersionsForConfiguration(configuration));
7676
VersionConfiguringAction versionConfiguringAction = new VersionConfiguringAction(this.project,
7777
this.dependencyManagementContainer, configuration);
78-
configuration.getIncoming().beforeResolve(configureMavenExclusions(configuration, versionConfiguringAction));
78+
configuration.withDependencies(configureMavenExclusions(configuration, versionConfiguringAction));
7979
versionConfiguringAction.applyTo(configuration);
8080
}
8181

82-
private Action<ResolvableDependencies> configureMavenExclusions(Configuration configuration,
82+
private Action<DependencySet> configureMavenExclusions(Configuration configuration,
8383
VersionConfiguringAction versionConfiguringAction) {
8484
return new ExclusionConfiguringAction(this.dependencyManagementSettings, this.dependencyManagementContainer,
8585
this.configurationContainer, configuration, this.exclusionResolver, versionConfiguringAction::applyTo);

src/main/java/io/spring/gradle/dependencymanagement/internal/ExclusionConfiguringAction.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.gradle.api.artifacts.Dependency;
3030
import org.gradle.api.artifacts.DependencyConstraintSet;
3131
import org.gradle.api.artifacts.DependencySet;
32-
import org.gradle.api.artifacts.ModuleDependency;
3332
import org.gradle.api.artifacts.ModuleVersionIdentifier;
3433
import org.gradle.api.artifacts.ResolvableDependencies;
3534
import org.gradle.api.artifacts.component.ComponentSelector;
@@ -48,7 +47,7 @@
4847
*
4948
* @author Andy Wilkinson
5049
*/
51-
class ExclusionConfiguringAction implements Action<ResolvableDependencies> {
50+
class ExclusionConfiguringAction implements Action<DependencySet> {
5251

5352
private static final Logger logger = LoggerFactory.getLogger(ExclusionConfiguringAction.class);
5453

@@ -77,21 +76,18 @@ class ExclusionConfiguringAction implements Action<ResolvableDependencies> {
7776
}
7877

7978
@Override
80-
public void execute(ResolvableDependencies resolvableDependencies) {
81-
if (this.configuration.isTransitive() && this.dependencyManagementSettings.isApplyMavenExclusions()) {
82-
applyMavenExclusions(resolvableDependencies);
79+
public void execute(DependencySet dependencySet) {
80+
if (this.configuration.isCanBeResolved() && this.configuration.isTransitive()
81+
&& this.dependencyManagementSettings.isApplyMavenExclusions()) {
82+
applyMavenExclusions(dependencySet);
8383
}
8484
}
8585

86-
private void applyMavenExclusions(ResolvableDependencies resolvableDependencies) {
86+
private void applyMavenExclusions(DependencySet dependencySet) {
8787
Set<DependencyCandidate> excludedDependencies = findExcludedDependencies();
8888
logger.info("Excluding {}", excludedDependencies);
89-
for (org.gradle.api.artifacts.Dependency dependency : resolvableDependencies.getDependencies()) {
90-
if (dependency instanceof ModuleDependency) {
91-
for (DependencyCandidate excludedDependency : excludedDependencies) {
92-
((ModuleDependency) dependency).exclude(excludedDependency.asMap());
93-
}
94-
}
89+
for (DependencyCandidate excludedDependency : excludedDependencies) {
90+
this.configuration.exclude(excludedDependency.asMap());
9591
}
9692
}
9793

src/test/java/io/spring/gradle/dependencymanagement/GradleVersionCompatibilityIntegrationTests.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package io.spring.gradle.dependencymanagement;
1818

19+
import java.io.File;
20+
import java.io.IOException;
1921
import java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.List;
@@ -38,7 +40,7 @@ class GradleVersionCompatibilityIntegrationTests {
3840
@RegisterExtension
3941
private final GradleBuild gradleBuild = new GradleBuild();
4042

41-
@ParameterizedTest(name = "Gradle {0}")
43+
@ParameterizedTest(name = "{displayName} (Gradle {0})")
4244
@MethodSource("gradleVersions")
4345
void pluginIsCompatible(String gradleVersion) {
4446
BuildResult result = this.gradleBuild.runner()
@@ -48,9 +50,21 @@ void pluginIsCompatible(String gradleVersion) {
4850
assertThat(result.task(":resolve").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
4951
}
5052

53+
@ParameterizedTest(name = "{displayName} (Gradle {0})")
54+
@MethodSource("gradleVersions")
55+
void buildDoesNotProduceDeprecationWarnings(String gradleVersion) throws IOException {
56+
File projectDir = this.gradleBuild.runner().getProjectDir();
57+
File javaSource = new File(projectDir, "src/main/java/com/example/Main.java");
58+
javaSource.getParentFile().mkdirs();
59+
javaSource.createNewFile();
60+
BuildResult result = this.gradleBuild.runner().withGradleVersion(gradleVersion).withArguments("build").build();
61+
assertThat(result.getOutput()).doesNotContainIgnoringCase("deprecated");
62+
assertThat(result.task(":build").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
63+
}
64+
5165
static List<String[]> gradleVersions() {
5266
List<String> versions = Arrays.asList("6.8.3", "6.9.4", "7.0.2", "7.1.1", "7.2", "7.3.3", "7.4.2", "7.5.1",
53-
"8.0.2", "8.1.1", "8.2.1", "8.3", "8.4", "8.5", "8.6", "8.7");
67+
"8.0.2", "8.1.1", "8.2.1", "8.3", "8.4", "8.5", "8.6", "8.7", "8.8");
5468
List<String[]> result = new ArrayList<>();
5569
for (String version : versions) {
5670
result.add(new String[] { version });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id "java"
3+
id "io.spring.dependency-management"
4+
}
5+
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
dependencyManagement {
11+
imports {
12+
mavenBom 'org.springframework.boot:spring-boot-dependencies:2.7.15'
13+
}
14+
}
15+
16+
dependencies {
17+
implementation("org.springframework.boot:spring-boot-starter")
18+
implementation("org.keycloak:keycloak-admin-client:25.0.1")
19+
}

0 commit comments

Comments
 (0)