forked from mojohaus/versions
/
MaxDependencyUpdates.java
375 lines (345 loc) · 15 KB
/
MaxDependencyUpdates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package org.apache.maven.plugins.enforcer;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.enforcer.rule.api.EnforcerLevel;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRule2;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.codehaus.mojo.versions.model.RuleSet;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL;
import static org.codehaus.mojo.versions.api.Segment.MINOR;
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;
import static org.codehaus.mojo.versions.filtering.DependencyFilter.filterDependencies;
import static org.codehaus.mojo.versions.filtering.WildcardMatcher.WILDCARD;
import static org.codehaus.mojo.versions.utils.MavenProjectUtils.extractDependenciesFromDependencyManagement;
import static org.codehaus.mojo.versions.utils.MavenProjectUtils.extractDependenciesFromPlugins;
import static org.codehaus.mojo.versions.utils.MavenProjectUtils.extractPluginDependenciesFromPluginsInPluginManagement;
public class MaxDependencyUpdates implements EnforcerRule2 {
/**
* Maximum allowed number of updates.
*
* @since 2.14.0
*/
protected int maxUpdates = 0;
/**
* Whether to process the dependencies section of the project.
*
* @since 2.14.0
*/
protected boolean processDependencies = true;
/**
* Whether to process the dependencyManagement section of the project.
*
* @since 1.2
*/
protected boolean processDependencyManagement = true;
/**
* Whether to process the dependencyManagement part transitive or not.
* In case of type {@code pom} and scope {@code import}, this means
* by default to report also the imported dependencies.
* If the parameter is set to {@code false}, the report will only show
* updates of the imported pom itself.
*
* @since 2.14.0
*/
protected boolean processDependencyManagementTransitive = true;
/**
* Whether to process the dependencies sections of plugins.
*
* @since 2.14.0
*/
protected boolean processPluginDependencies = true;
/**
* Whether to process the dependencies sections of plugins which are defined in pluginManagement.
*
* @since 2.14.0
*/
protected boolean processPluginDependenciesInPluginManagement = true;
/**
* Whether minor updates should be ignored. Default {@code false}.
*
* <p><b>Note: </b> when {@code true}, will also assume that {@link #ignoreIncrementalUpdates}
* and {@link #ignoreSubIncrementalUpdates} are {@code true}.</p>
*
* @since 2.14.0
*/
protected boolean ignoreMinorUpdates = false;
/**
* Whether incremental updates should be ignored. Default {@code false}.
*
* <p><b>Note: </b> when {@code true}, will also assume that
* {@link #ignoreSubIncrementalUpdates} is {@code true}.</p>
*
* @since 2.14.0
*/
protected boolean ignoreIncrementalUpdates = false;
/**
* Whether sub-incremental updates should be ignored. Default {@code false}.
*
* @since 2.14.0
*/
protected boolean ignoreSubIncrementalUpdates = false;
/**
* List of dependency inclusion patterns.
* Only dependencies matching all the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> dependencyIncludes = singletonList(WILDCARD);
/**
* List of dependency exclusion patterns.
* Only dependencies matching none of the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> dependencyExcludes = emptyList();
/**
* List of dependency management inclusion patterns.
* Only dependencies matching all the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> dependencyManagementIncludes = singletonList(WILDCARD);
/**
* List of dependency management exclusion patterns.
* Only dependencies matching none of the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> dependencyManagementExcludes = emptyList();
/**
* List of plugin dependency inclusion patterns.
* Only dependencies matching all the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> pluginDependencyIncludes = singletonList(WILDCARD);
/**
* List of plugin dependency exclusion patterns.
* Only dependencies matching none of the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> pluginDependencyExcludes = emptyList();
/**
* List of plugin management dependency inclusion patterns.
* Only dependencies matching all the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> pluginManagementDependencyIncludes = singletonList(WILDCARD);
/**
* List of plugin dependency management exclusion patterns.
* Only dependencies matching none of the patterns will be considered.
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
*
* @since 2.14.0
*/
protected List<String> pluginManagementDependencyExcludes = emptyList();
/**
* settings.xml's server id for the URL. This is used when wagon needs extra authentication information.
*
* @since 2.14.0
*/
private String serverId;
/**
* URI of a ruleSet file containing the rules that control how to compare
* version numbers. The URI could be either a Wagon URI or a classpath URI
* (e.g. <code>classpath:///package/sub/package/rules.xml</code>).
*
* 2.14.0
*/
private String rulesUri;
/**
* <p>Allows specifying the {@linkplain RuleSet} object describing rules
* on artifact versions to ignore when considering updates.</p>
*
* @see <a href="https://www.mojohaus.org/versions-maven-plugin/version-rules.html#Using_the_ruleSet_element_in_the_POM">
* Using the ruleSet element in the POM</a>
*
* @since 2.14.0
*/
protected RuleSet ruleSet;
/**
* Retrieves the maven project from metadata
* @param ruleHelper EnforcerRuleHelper object
* @return maven project
*/
private static MavenProject getMavenProject(EnforcerRuleHelper ruleHelper) {
try {
return (MavenProject) ruleHelper.evaluate("${project}");
} catch (ExpressionEvaluationException e) {
throw new RuntimeException("Cannot evaluate project metadata", e);
}
}
/**
* Creates the VersionsHelper object
* @param ruleHelper EnforcerRuleHelper object
* @return VersionsHelper object
*/
@SuppressWarnings("unchecked")
private static VersionsHelper createVersionsHelper(
EnforcerRuleHelper ruleHelper, String serverId, String rulesUri, RuleSet ruleSet) {
try {
return new DefaultVersionsHelper.Builder()
.withRepositorySystem(ruleHelper.getComponent(RepositorySystem.class))
.withAetherRepositorySystem(ruleHelper.getComponent(org.eclipse.aether.RepositorySystem.class))
.withWagonMap(ruleHelper.getComponentMap(Wagon.class.getName()).entrySet().stream()
.filter(e -> e.getValue() instanceof Wagon)
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), (Wagon) e.getValue()), HashMap::putAll))
.withServerId(serverId)
.withRulesUri(rulesUri)
.withRuleSet(ruleSet)
.withIgnoredVersions(null)
.withLog(ruleHelper.getLog())
.withMavenSession((MavenSession) ruleHelper.evaluate("${session}"))
.withMojoExecution((MojoExecution) ruleHelper.evaluate("${mojoExecution}"))
.build();
} catch (ExpressionEvaluationException e) {
throw new RuntimeException("Cannot evaluate project metadata", e);
} catch (ComponentLookupException | MojoExecutionException e) {
throw new RuntimeException("Cannot resolve dependency", e);
}
}
@Override
public boolean isCacheable() {
return false;
}
@Override
public boolean isResultValid(EnforcerRule enforcerRule) {
return false;
}
@Override
public String getCacheId() {
return "Does not matter as not cacheable";
}
@Override
public void execute(EnforcerRuleHelper ruleHelper) throws EnforcerRuleException {
VersionsHelper versionsHelper =
createVersionsHelper(ruleHelper, serverId != null ? serverId : "serverId", rulesUri, ruleSet);
MavenProject project = getMavenProject(ruleHelper);
Set<Dependency> dependencies = new TreeSet<>(DependencyComparator.INSTANCE);
if (processDependencyManagement) {
try {
dependencies.addAll(filterDependencies(
extractDependenciesFromDependencyManagement(
project, processDependencyManagementTransitive, ruleHelper.getLog()),
dependencyManagementIncludes,
dependencyManagementExcludes,
"Dependency Management",
ruleHelper.getLog()));
} catch (VersionRetrievalException e) {
throw new EnforcerRuleException(e.getMessage());
}
}
if (processPluginDependencies) {
dependencies.addAll(filterDependencies(
extractDependenciesFromPlugins(project),
pluginDependencyIncludes,
pluginDependencyExcludes,
"Plugin Dependencies",
ruleHelper.getLog()));
}
if (processPluginDependenciesInPluginManagement) {
dependencies.addAll(filterDependencies(
extractPluginDependenciesFromPluginsInPluginManagement(project),
pluginManagementDependencyIncludes,
pluginManagementDependencyExcludes,
"Plugin Management Dependencies",
ruleHelper.getLog()));
}
if (processDependencies) {
dependencies.addAll(filterDependencies(
project.getDependencies(),
dependencyIncludes,
dependencyExcludes,
"Dependencies",
ruleHelper.getLog()));
}
try {
Optional<Segment> ignoredSegment = ignoreSubIncrementalUpdates
? of(SUBINCREMENTAL)
: ignoreIncrementalUpdates ? of(INCREMENTAL) : ignoreMinorUpdates ? of(MINOR) : empty();
List<ArtifactVersions> upgradable =
versionsHelper.lookupDependenciesUpdates(dependencies, false).values().stream()
.filter(v -> v.getVersions(v.restrictionForIgnoreScope(ignoredSegment), true).length > 0)
.collect(Collectors.toList());
if (upgradable.size() > maxUpdates) {
throw new EnforcerRuleException("More than " + maxUpdates + " upgradable artifacts detected: "
+ upgradable.stream()
.map(av -> av.getArtifact() + " -> ["
+ Arrays.stream(av.getVersions())
.map(ArtifactVersion::toString)
.collect(Collectors.joining(", "))
+ "]")
.collect(Collectors.joining(", ")));
}
} catch (VersionRetrievalException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
public EnforcerLevel getLevel() {
// all reported items should be treated as errors
return EnforcerLevel.ERROR;
}
}