16
16
* specific language governing permissions and limitations
17
17
* under the License.
18
18
*/
19
- package org .apache .maven .enforcer .rules ;
19
+ package org .apache .maven .enforcer .rules . dependency ;
20
20
21
21
import javax .inject .Inject ;
22
22
import javax .inject .Named ;
32
32
import java .util .function .Predicate ;
33
33
import java .util .stream .Collectors ;
34
34
35
- import org .apache .maven .RepositoryUtils ;
36
35
import org .apache .maven .enforcer .rule .api .EnforcerRuleException ;
36
+ import org .apache .maven .enforcer .rules .AbstractStandardEnforcerRule ;
37
37
import org .apache .maven .enforcer .rules .utils .ArtifactMatcher ;
38
38
import org .apache .maven .enforcer .rules .utils .ArtifactUtils ;
39
39
import org .apache .maven .execution .MavenSession ;
40
40
import org .apache .maven .project .MavenProject ;
41
- import org .eclipse .aether .DefaultRepositorySystemSession ;
42
41
import org .eclipse .aether .RepositorySystem ;
43
- import org .eclipse .aether .RepositorySystemSession ;
44
- import org .eclipse .aether .collection .CollectRequest ;
45
- import org .eclipse .aether .collection .CollectResult ;
46
42
import org .eclipse .aether .collection .DependencyCollectionException ;
47
- import org .eclipse .aether .collection .DependencySelector ;
48
- import org .eclipse .aether .graph .Dependency ;
49
43
import org .eclipse .aether .graph .DependencyNode ;
50
44
import org .eclipse .aether .graph .DependencyVisitor ;
51
- import org .eclipse .aether .util .graph .selector .AndDependencySelector ;
52
- import org .eclipse .aether .util .graph .selector .OptionalDependencySelector ;
53
- import org .eclipse .aether .util .graph .selector .ScopeDependencySelector ;
54
45
import org .eclipse .aether .util .graph .visitor .TreeDependencyVisitor ;
55
46
import org .eclipse .aether .version .VersionConstraint ;
56
47
@@ -108,7 +99,7 @@ public final class BanDynamicVersions extends AbstractStandardEnforcerRule {
108
99
/**
109
100
* the scopes of dependencies which should be excluded from this rule
110
101
*/
111
- private String [] excludedScopes ;
102
+ private List < String > excludedScopes ;
112
103
113
104
/**
114
105
* Specify the ignored dependencies. This can be a list of artifacts in the format
@@ -119,17 +110,12 @@ public final class BanDynamicVersions extends AbstractStandardEnforcerRule {
119
110
*/
120
111
private List <String > ignores = null ;
121
112
122
- private final MavenProject project ;
123
-
124
- private final RepositorySystem repoSystem ;
125
-
126
- private final MavenSession mavenSession ;
113
+ private final ResolverUtil resolverUtil ;
127
114
128
115
@ Inject
129
- public BanDynamicVersions (MavenProject project , RepositorySystem repoSystem , MavenSession mavenSession ) {
130
- this .project = Objects .requireNonNull (project );
131
- this .repoSystem = Objects .requireNonNull (repoSystem );
132
- this .mavenSession = Objects .requireNonNull (mavenSession );
116
+ public BanDynamicVersions (
117
+ MavenProject project , RepositorySystem repoSystem , MavenSession mavenSession , ResolverUtil resolverUtil ) {
118
+ this .resolverUtil = Objects .requireNonNull (resolverUtil );
133
119
}
134
120
135
121
private final class BannedDynamicVersionCollector implements DependencyVisitor {
@@ -138,19 +124,19 @@ private final class BannedDynamicVersionCollector implements DependencyVisitor {
138
124
139
125
private boolean isRoot = true ;
140
126
141
- private int numViolations ;
127
+ private List < String > violations ;
142
128
143
129
private final Predicate <DependencyNode > predicate ;
144
130
145
- public int getNumViolations () {
146
- return numViolations ;
131
+ public List < String > getViolations () {
132
+ return violations ;
147
133
}
148
134
149
135
BannedDynamicVersionCollector (Predicate <DependencyNode > predicate ) {
150
- nodeStack = new ArrayDeque <>();
136
+ this . nodeStack = new ArrayDeque <>();
151
137
this .predicate = predicate ;
152
138
this .isRoot = true ;
153
- numViolations = 0 ;
139
+ this . violations = new ArrayList <>() ;
154
140
}
155
141
156
142
private boolean isBannedDynamicVersion (VersionConstraint versionConstraint ) {
@@ -183,13 +169,11 @@ public boolean visitEnter(DependencyNode node) {
183
169
} else {
184
170
getLog ().debug ("Found node " + node + " with version constraint " + node .getVersionConstraint ());
185
171
if (predicate .test (node ) && isBannedDynamicVersion (node .getVersionConstraint ())) {
186
- getLog ().warnOrError (() -> new StringBuilder ()
187
- .append ("Dependency " )
188
- .append (node .getDependency ())
189
- .append (dumpIntermediatePath (nodeStack ))
190
- .append (" is referenced with a banned dynamic version " )
191
- .append (node .getVersionConstraint ()));
192
- numViolations ++;
172
+ violations .add ("Dependency "
173
+ + node .getDependency ()
174
+ + dumpIntermediatePath (nodeStack )
175
+ + " is referenced with a banned dynamic version "
176
+ + node .getVersionConstraint ());
193
177
return false ;
194
178
}
195
179
nodeStack .addLast (node );
@@ -209,26 +193,17 @@ public boolean visitLeave(DependencyNode node) {
209
193
@ Override
210
194
public void execute () throws EnforcerRuleException {
211
195
212
- // get a new session to be able to tweak the dependency selector
213
- DefaultRepositorySystemSession newRepoSession =
214
- new DefaultRepositorySystemSession (mavenSession .getRepositorySession ());
215
-
216
- Collection <DependencySelector > depSelectors = new ArrayList <>();
217
- depSelectors .add (new ScopeDependencySelector (excludedScopes ));
218
- if (excludeOptionals ) {
219
- depSelectors .add (new OptionalDependencySelector ());
220
- }
221
- newRepoSession .setDependencySelector (new AndDependencySelector (depSelectors ));
222
-
223
- Dependency rootDependency = RepositoryUtils .toDependency (project .getArtifact (), null );
224
196
try {
225
- // use root dependency with unresolved direct dependencies
226
- int numViolations = emitDependenciesWithBannedDynamicVersions (rootDependency , newRepoSession );
227
- if (numViolations > 0 ) {
197
+ DependencyNode rootDependency =
198
+ resolverUtil .resolveTransitiveDependencies (excludeOptionals , excludedScopes );
199
+
200
+ List <String > violations = collectDependenciesWithBannedDynamicVersions (rootDependency );
201
+ if (!violations .isEmpty ()) {
228
202
ChoiceFormat dependenciesFormat = new ChoiceFormat ("1#dependency|1<dependencies" );
229
- throw new EnforcerRuleException ("Found " + numViolations + " "
230
- + dependenciesFormat .format (numViolations )
231
- + " with dynamic versions. Look at the warnings emitted above for the details." );
203
+ throw new EnforcerRuleException ("Found " + violations .size () + " "
204
+ + dependenciesFormat .format (violations .size ())
205
+ + " with dynamic versions." + System .lineSeparator ()
206
+ + String .join (System .lineSeparator (), violations ));
232
207
}
233
208
} catch (DependencyCollectionException e ) {
234
209
throw new EnforcerRuleException ("Could not retrieve dependency metadata for project" , e );
@@ -256,10 +231,8 @@ public boolean test(DependencyNode depNode) {
256
231
}
257
232
}
258
233
259
- private int emitDependenciesWithBannedDynamicVersions (
260
- Dependency rootDependency , RepositorySystemSession repoSession ) throws DependencyCollectionException {
261
- CollectRequest collectRequest = new CollectRequest (rootDependency , project .getRemoteProjectRepositories ());
262
- CollectResult collectResult = repoSystem .collectDependencies (repoSession , collectRequest );
234
+ private List <String > collectDependenciesWithBannedDynamicVersions (DependencyNode rootDependency )
235
+ throws DependencyCollectionException {
263
236
Predicate <DependencyNode > predicate ;
264
237
if (ignores != null && !ignores .isEmpty ()) {
265
238
predicate = new ExcludeArtifactPatternsPredicate (ignores );
@@ -268,8 +241,8 @@ private int emitDependenciesWithBannedDynamicVersions(
268
241
}
269
242
BannedDynamicVersionCollector bannedDynamicVersionCollector = new BannedDynamicVersionCollector (predicate );
270
243
DependencyVisitor depVisitor = new TreeDependencyVisitor (bannedDynamicVersionCollector );
271
- collectResult . getRoot () .accept (depVisitor );
272
- return bannedDynamicVersionCollector .getNumViolations ();
244
+ rootDependency .accept (depVisitor );
245
+ return bannedDynamicVersionCollector .getViolations ();
273
246
}
274
247
275
248
@ Override
0 commit comments