Skip to content

Commit cef7ad7

Browse files
author
Phillip Webb
committed
Support automatic exclude rules with Gradle
Update the Spring Boot Gradle plugin to automatically apply exclude rules to dependencies. See gh-1047
1 parent addc1f7 commit cef7ad7

File tree

7 files changed

+234
-47
lines changed

7 files changed

+234
-47
lines changed

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPlugin.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ import org.gradle.api.plugins.ApplicationPlugin
2222
import org.gradle.api.plugins.BasePlugin
2323
import org.gradle.api.plugins.JavaPlugin
2424
import org.springframework.boot.gradle.agent.AgentPluginFeatures
25+
import org.springframework.boot.gradle.exclude.ExcludePluginFeatures
2526
import org.springframework.boot.gradle.repackage.RepackagePluginFeatures
2627
import org.springframework.boot.gradle.resolve.ResolvePluginFeatures
28+
import org.springframework.boot.gradle.resolve.SpringBootResolutionStrategy
2729
import org.springframework.boot.gradle.run.RunPluginFeatures
2830

31+
2932
/**
3033
* Gradle 'Spring Boot' {@link Plugin}.
3134
*
@@ -41,11 +44,13 @@ class SpringBootPlugin implements Plugin<Project> {
4144
project.getPlugins().apply(ApplicationPlugin)
4245

4346
project.getExtensions().create("springBoot", SpringBootPluginExtension)
47+
project.getConfigurations().create(VersionManagedDependencies.CONFIGURATION);
4448

4549
new AgentPluginFeatures().apply(project)
4650
new ResolvePluginFeatures().apply(project)
4751
new RepackagePluginFeatures().apply(project)
4852
new RunPluginFeatures().apply(project)
53+
new ExcludePluginFeatures().apply(project)
4954

5055
useUtf8Encoding(project)
5156
}

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPluginExtension.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,9 @@ public class SpringBootPluginExtension {
108108
* Flag to indicate that the agent requires -noverify (and the plugin will refuse to start if it is not set)
109109
*/
110110
Boolean noverify;
111+
112+
/**
113+
* If exclude rules should be applied to dependencies based on the spring-dependencies-bom
114+
*/
115+
boolean applyExcludeRules = true;
111116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.gradle;
18+
19+
import java.io.File;
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import java.util.List;
25+
import java.util.Set;
26+
27+
import org.gradle.api.Project;
28+
import org.gradle.api.artifacts.Configuration;
29+
import org.springframework.boot.dependency.tools.Dependencies;
30+
import org.springframework.boot.dependency.tools.ManagedDependencies;
31+
import org.springframework.boot.dependency.tools.PropertiesFileDependencies;
32+
33+
/**
34+
* Utility to provide access to {@link ManagedDependencies} with support for version
35+
* file overrides.
36+
*
37+
* @author Phillip Webb
38+
*/
39+
public class VersionManagedDependencies {
40+
41+
public static final String CONFIGURATION = "versionManagement";
42+
43+
private Configuration versionManagementConfiguration;
44+
45+
private Collection<Dependencies> versionManagedDependencies;
46+
47+
private ManagedDependencies managedDependencies;
48+
49+
public VersionManagedDependencies(Project project) {
50+
this.versionManagementConfiguration = project.getConfigurations().getByName(
51+
CONFIGURATION);
52+
}
53+
54+
public ManagedDependencies getManagedDependencies() {
55+
if (this.managedDependencies == null) {
56+
this.managedDependencies = ManagedDependencies
57+
.get(getVersionManagedDependencies());
58+
}
59+
return this.managedDependencies;
60+
}
61+
62+
private Collection<Dependencies> getVersionManagedDependencies() {
63+
if (versionManagedDependencies == null) {
64+
Set<File> files = versionManagementConfiguration.resolve();
65+
List<Dependencies> dependencies = new ArrayList<Dependencies>(files.size());
66+
for (File file : files) {
67+
dependencies.add(getPropertiesFileManagedDependencies(file));
68+
}
69+
this.versionManagedDependencies = dependencies;
70+
}
71+
return versionManagedDependencies;
72+
}
73+
74+
private Dependencies getPropertiesFileManagedDependencies(File file) {
75+
if (!file.getName().toLowerCase().endsWith(".properties")) {
76+
throw new IllegalStateException(file + " is not a version property file");
77+
}
78+
try {
79+
return new PropertiesFileDependencies(new FileInputStream(file));
80+
}
81+
catch (IOException ex) {
82+
throw new IllegalStateException(ex);
83+
}
84+
}
85+
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.gradle.exclude;
18+
19+
import org.gradle.api.Action;
20+
import org.gradle.api.Project;
21+
import org.gradle.api.artifacts.Configuration;
22+
import org.gradle.api.artifacts.Dependency;
23+
import org.gradle.api.artifacts.ModuleDependency;
24+
import org.gradle.api.internal.artifacts.DefaultExcludeRule;
25+
import org.gradle.api.logging.Logger;
26+
import org.springframework.boot.dependency.tools.Dependency.Exclusion;
27+
import org.springframework.boot.dependency.tools.ManagedDependencies;
28+
import org.springframework.boot.gradle.VersionManagedDependencies;
29+
30+
/**
31+
* {@link Action} to apply exclude rules.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
public class ApplyExcludeRules implements Action<Configuration> {
36+
37+
private final Logger logger;
38+
39+
private final VersionManagedDependencies versionManagedDependencies;
40+
41+
public ApplyExcludeRules(Project project) {
42+
this.logger = project.getLogger();
43+
this.versionManagedDependencies = new VersionManagedDependencies(project);
44+
}
45+
46+
@Override
47+
public void execute(Configuration configuration) {
48+
configuration.getDependencies().all(new Action<Dependency>() {
49+
@Override
50+
public void execute(Dependency dependency) {
51+
applyExcludeRules(dependency);
52+
}
53+
});
54+
}
55+
56+
private void applyExcludeRules(Dependency dependency) {
57+
if (dependency instanceof ModuleDependency) {
58+
applyExcludeRules((ModuleDependency) dependency);
59+
}
60+
}
61+
62+
private void applyExcludeRules(ModuleDependency dependency) {
63+
ManagedDependencies managedDependencies = versionManagedDependencies
64+
.getManagedDependencies();
65+
org.springframework.boot.dependency.tools.Dependency managedDependency = managedDependencies
66+
.find(dependency.getGroup(), dependency.getName());
67+
if (managedDependency != null) {
68+
if (managedDependency.getExclusions().isEmpty()) {
69+
logger.debug("No exclusions rules applied for managed dependency "
70+
+ dependency);
71+
}
72+
for (Exclusion exclusion : managedDependency.getExclusions()) {
73+
addExcludeRule(dependency, exclusion);
74+
}
75+
}
76+
else {
77+
logger.debug("No exclusions rules applied for non-managed dependency "
78+
+ dependency);
79+
}
80+
}
81+
82+
private void addExcludeRule(ModuleDependency dependency, Exclusion exclusion) {
83+
logger.info("Adding managed exclusion rule " + exclusion + " to " + dependency);
84+
DefaultExcludeRule rule = new DefaultExcludeRule(exclusion.getGroupId(),
85+
exclusion.getArtifactId());
86+
dependency.getExcludeRules().add(rule);
87+
}
88+
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.gradle.exclude;
18+
19+
import org.gradle.api.Project;
20+
import org.springframework.boot.gradle.PluginFeatures;
21+
import org.springframework.boot.gradle.SpringBootPluginExtension;
22+
23+
/**
24+
* {@link PluginFeatures} to apply exclusion rules.
25+
*
26+
* @author Phillip Webb
27+
*/
28+
public class ExcludePluginFeatures implements PluginFeatures {
29+
30+
@Override
31+
public void apply(Project project) {
32+
SpringBootPluginExtension extension = project.getExtensions().getByType(
33+
SpringBootPluginExtension.class);
34+
if (extension.isApplyExcludeRules()) {
35+
project.getConfigurations().all(new ApplyExcludeRules(project));
36+
}
37+
}
38+
39+
}

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/resolve/ResolvePluginFeatures.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public class ResolvePluginFeatures implements PluginFeatures {
3030

3131
@Override
3232
public void apply(final Project project) {
33-
project.getConfigurations().create(
34-
SpringBootResolutionStrategy.VERSION_MANAGEMENT_CONFIGURATION);
3533
project.getConfigurations().all(new Action<Configuration>() {
3634
@Override
3735
public void execute(Configuration configuration) {

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/resolve/SpringBootResolutionStrategy.java

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,14 @@
1616

1717
package org.springframework.boot.gradle.resolve;
1818

19-
import java.io.File;
20-
import java.io.FileInputStream;
21-
import java.io.IOException;
22-
import java.util.ArrayList;
23-
import java.util.Collection;
24-
import java.util.List;
25-
import java.util.Set;
26-
2719
import org.gradle.api.Action;
2820
import org.gradle.api.Project;
2921
import org.gradle.api.artifacts.Configuration;
3022
import org.gradle.api.artifacts.DependencyResolveDetails;
3123
import org.gradle.api.artifacts.ModuleVersionSelector;
32-
import org.springframework.boot.dependency.tools.Dependencies;
3324
import org.springframework.boot.dependency.tools.Dependency;
3425
import org.springframework.boot.dependency.tools.ManagedDependencies;
35-
import org.springframework.boot.dependency.tools.PropertiesFileDependencies;
26+
import org.springframework.boot.gradle.VersionManagedDependencies;
3627

3728
/**
3829
* A resolution strategy to resolve missing version numbers using the
@@ -42,12 +33,11 @@
4233
*/
4334
public class SpringBootResolutionStrategy {
4435

45-
public static final String VERSION_MANAGEMENT_CONFIGURATION = "versionManagement";
46-
4736
private static final String SPRING_BOOT_GROUP = "org.springframework.boot";
4837

49-
public static void applyToConfiguration(final Project project, Configuration configuration) {
50-
if (VERSION_MANAGEMENT_CONFIGURATION.equals(configuration.getName())) {
38+
public static void applyToConfiguration(final Project project,
39+
Configuration configuration) {
40+
if (VersionManagedDependencies.CONFIGURATION.equals(configuration.getName())) {
5141
return;
5242
}
5343
VersionResolver versionResolver = new VersionResolver(project);
@@ -56,13 +46,10 @@ public static void applyToConfiguration(final Project project, Configuration con
5646

5747
private static class VersionResolver implements Action<DependencyResolveDetails> {
5848

59-
private Configuration versionManagementConfiguration;
60-
61-
private Collection<Dependencies> versionManagedDependencies;
49+
private final VersionManagedDependencies versionManagedDependencies;
6250

6351
public VersionResolver(Project project) {
64-
this.versionManagementConfiguration = project.getConfigurations().getByName(
65-
VERSION_MANAGEMENT_CONFIGURATION);
52+
this.versionManagedDependencies = new VersionManagedDependencies(project);
6653
}
6754

6855
@Override
@@ -74,41 +61,19 @@ public void execute(DependencyResolveDetails resolveDetails) {
7461
}
7562

7663
private void resolve(DependencyResolveDetails resolveDetails) {
77-
ManagedDependencies dependencies = ManagedDependencies.get(
78-
getVersionManagedDependencies());
64+
ManagedDependencies dependencies = this.versionManagedDependencies
65+
.getManagedDependencies();
7966
ModuleVersionSelector target = resolveDetails.getTarget();
8067
if (SPRING_BOOT_GROUP.equals(target.getGroup())) {
8168
resolveDetails.useVersion(dependencies.getSpringBootVersion());
8269
return;
8370
}
84-
Dependency dependency = dependencies.find(target.getGroup(), target.getName());
71+
Dependency dependency = dependencies
72+
.find(target.getGroup(), target.getName());
8573
if (dependency != null) {
8674
resolveDetails.useVersion(dependency.getVersion());
8775
}
8876
}
8977

90-
private Collection<Dependencies> getVersionManagedDependencies() {
91-
if (versionManagedDependencies == null) {
92-
Set<File> files = versionManagementConfiguration.resolve();
93-
List<Dependencies> dependencies = new ArrayList<Dependencies>(
94-
files.size());
95-
for (File file : files) {
96-
dependencies.add(getPropertiesFileManagedDependencies(file));
97-
}
98-
this.versionManagedDependencies = dependencies;
99-
}
100-
return versionManagedDependencies;
101-
}
102-
103-
private Dependencies getPropertiesFileManagedDependencies(File file) {
104-
if (!file.getName().toLowerCase().endsWith(".properties")) {
105-
throw new IllegalStateException(file + " is not a version property file");
106-
}
107-
try {
108-
return new PropertiesFileDependencies(new FileInputStream(file));
109-
} catch (IOException ex) {
110-
throw new IllegalStateException(ex);
111-
}
112-
}
11378
}
11479
}

0 commit comments

Comments
 (0)