38
38
import org .gradle .api .artifacts .ModuleDependency ;
39
39
import org .gradle .api .artifacts .component .ModuleComponentIdentifier ;
40
40
import org .gradle .api .artifacts .dsl .DependencyHandler ;
41
+ import org .gradle .api .artifacts .result .ResolvedArtifactResult ;
41
42
import org .gradle .api .tasks .Input ;
42
43
import org .gradle .api .tasks .TaskAction ;
43
44
48
49
*/
49
50
public class CheckClasspathForUnnecessaryExclusions extends DefaultTask {
50
51
52
+ private static final Map <String , String > SPRING_BOOT_DEPENDENCIES_PROJECT = Collections .singletonMap ("path" ,
53
+ ":spring-boot-project:spring-boot-dependencies" );
54
+
51
55
private final Map <String , Set <String >> exclusionsByDependencyId = new TreeMap <>();
52
56
53
57
private final Map <String , Dependency > dependencyById = new HashMap <>();
@@ -63,27 +67,31 @@ public CheckClasspathForUnnecessaryExclusions(DependencyHandler dependencyHandle
63
67
ConfigurationContainer configurations ) {
64
68
this .dependencyHandler = getProject ().getDependencies ();
65
69
this .configurations = getProject ().getConfigurations ();
66
- this .platform = this .dependencyHandler .create (this . dependencyHandler . platform ( this . dependencyHandler
67
- . project ( Collections . singletonMap ( "path" , ":spring-boot- project:spring-boot-dependencies" ) )));
70
+ this .platform = this .dependencyHandler .create (
71
+ this . dependencyHandler . platform ( this . dependencyHandler . project ( SPRING_BOOT_DEPENDENCIES_PROJECT )));
68
72
getOutputs ().upToDateWhen ((task ) -> true );
69
73
}
70
74
71
75
public void setClasspath (Configuration classpath ) {
72
76
this .exclusionsByDependencyId .clear ();
73
77
this .dependencyById .clear ();
74
- classpath .getAllDependencies ().all ((dependency ) -> {
75
- if (dependency instanceof ModuleDependency ) {
76
- String dependencyId = dependency .getGroup () + ":" + dependency .getName ();
77
- Set <ExcludeRule > excludeRules = ((ModuleDependency ) dependency ).getExcludeRules ();
78
- TreeSet <String > exclusions = excludeRules .stream ()
79
- .map ((rule ) -> rule .getGroup () + ":" + rule .getModule ())
80
- .collect (Collectors .toCollection (TreeSet ::new ));
81
- this .exclusionsByDependencyId .put (dependencyId , exclusions );
82
- if (!exclusions .isEmpty ()) {
83
- this .dependencyById .put (dependencyId , getProject ().getDependencies ().create (dependencyId ));
84
- }
85
- }
86
- });
78
+ classpath .getAllDependencies ().all (this ::processDependency );
79
+ }
80
+
81
+ private void processDependency (Dependency dependency ) {
82
+ if (dependency instanceof ModuleDependency ) {
83
+ processDependency ((ModuleDependency ) dependency );
84
+ }
85
+ }
86
+
87
+ private void processDependency (ModuleDependency dependency ) {
88
+ String dependencyId = getId (dependency );
89
+ TreeSet <String > exclusions = dependency .getExcludeRules ().stream ().map (this ::getId )
90
+ .collect (Collectors .toCollection (TreeSet ::new ));
91
+ this .exclusionsByDependencyId .put (dependencyId , exclusions );
92
+ if (!exclusions .isEmpty ()) {
93
+ this .dependencyById .put (dependencyId , getProject ().getDependencies ().create (dependencyId ));
94
+ }
87
95
}
88
96
89
97
@ Input
@@ -94,33 +102,48 @@ Map<String, Set<String>> getExclusionsByDependencyId() {
94
102
@ TaskAction
95
103
public void checkForUnnecessaryExclusions () {
96
104
Map <String , Set <String >> unnecessaryExclusions = new HashMap <>();
97
- for (Entry <String , Set <String >> entry : this .exclusionsByDependencyId .entrySet ()) {
98
- String dependencyId = entry .getKey ();
99
- Set <String > exclusions = entry .getValue ();
105
+ this .exclusionsByDependencyId .forEach ((dependencyId , exclusions ) -> {
100
106
if (!exclusions .isEmpty ()) {
101
107
Dependency toCheck = this .dependencyById .get (dependencyId );
102
108
List <String > dependencies = this .configurations .detachedConfiguration (toCheck , this .platform )
103
- .getIncoming ().getArtifacts ().getArtifacts ().stream ().map ((artifact ) -> {
104
- ModuleComponentIdentifier id = (ModuleComponentIdentifier ) artifact .getId ()
105
- .getComponentIdentifier ();
106
- return id .getGroup () + ":" + id .getModule ();
107
- }).collect (Collectors .toList ());
109
+ .getIncoming ().getArtifacts ().getArtifacts ().stream ().map (this ::getId )
110
+ .collect (Collectors .toList ());
108
111
exclusions .removeAll (dependencies );
109
112
if (!exclusions .isEmpty ()) {
110
113
unnecessaryExclusions .put (dependencyId , exclusions );
111
114
}
112
115
}
113
- }
116
+ });
114
117
if (!unnecessaryExclusions .isEmpty ()) {
115
- StringBuilder message = new StringBuilder ("Unnecessary exclusions detected:" );
116
- for (Entry <String , Set <String >> entry : unnecessaryExclusions .entrySet ()) {
117
- message .append (String .format ("%n %s" , entry .getKey ()));
118
- for (String exclusion : entry .getValue ()) {
119
- message .append (String .format ("%n %s" , exclusion ));
120
- }
118
+ throw new GradleException (getExceptionMessage (unnecessaryExclusions ));
119
+ }
120
+ }
121
+
122
+ private String getExceptionMessage (Map <String , Set <String >> unnecessaryExclusions ) {
123
+ StringBuilder message = new StringBuilder ("Unnecessary exclusions detected:" );
124
+ for (Entry <String , Set <String >> entry : unnecessaryExclusions .entrySet ()) {
125
+ message .append (String .format ("%n %s" , entry .getKey ()));
126
+ for (String exclusion : entry .getValue ()) {
127
+ message .append (String .format ("%n %s" , exclusion ));
121
128
}
122
- throw new GradleException (message .toString ());
123
129
}
130
+ return message .toString ();
131
+ }
132
+
133
+ private String getId (ResolvedArtifactResult artifact ) {
134
+ return getId ((ModuleComponentIdentifier ) artifact .getId ().getComponentIdentifier ());
135
+ }
136
+
137
+ private String getId (ModuleDependency dependency ) {
138
+ return dependency .getGroup () + ":" + dependency .getName ();
139
+ }
140
+
141
+ private String getId (ExcludeRule rule ) {
142
+ return rule .getGroup () + ":" + rule .getModule ();
143
+ }
144
+
145
+ private String getId (ModuleComponentIdentifier identifier ) {
146
+ return identifier .getGroup () + ":" + identifier .getModule ();
124
147
}
125
148
126
149
}
0 commit comments