1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.dvc;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18 import static com.google.common.base.Preconditions.checkState;
19
20 import org.basepom.mojo.dvc.dependency.DependencyMap;
21 import org.basepom.mojo.dvc.dependency.DependencyMapBuilder;
22 import org.basepom.mojo.dvc.dependency.DependencyTreeResolver;
23 import org.basepom.mojo.dvc.model.ResolverDefinition;
24 import org.basepom.mojo.dvc.model.VersionCheckExcludes;
25 import org.basepom.mojo.dvc.strategy.StrategyProvider;
26 import org.basepom.mojo.dvc.version.VersionResolutionCollection;
27
28 import java.util.Arrays;
29 import java.util.List;
30
31 import com.google.common.base.Strings;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.ImmutableSet;
34 import com.google.common.collect.ImmutableSetMultimap;
35 import org.apache.maven.execution.MavenSession;
36 import org.apache.maven.plugin.AbstractMojo;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.plugins.annotations.Component;
40 import org.apache.maven.plugins.annotations.Parameter;
41 import org.apache.maven.project.DefaultProjectBuildingRequest;
42 import org.apache.maven.project.MavenProject;
43 import org.apache.maven.project.ProjectBuilder;
44 import org.apache.maven.project.ProjectBuildingRequest;
45 import org.apache.maven.project.ProjectDependenciesResolver;
46 import org.eclipse.aether.DefaultRepositorySystemSession;
47 import org.eclipse.aether.RepositorySystem;
48 import org.eclipse.aether.RepositorySystemSession;
49 import org.eclipse.aether.artifact.Artifact;
50 import org.eclipse.aether.resolution.VersionRangeRequest;
51 import org.eclipse.aether.util.artifact.JavaScopes;
52 import org.eclipse.aether.util.graph.version.SnapshotVersionFilter;
53
54
55
56
57 public abstract class AbstractDependencyVersionsMojo
58 extends AbstractMojo
59 implements Context {
60
61 private static final ImmutableSet<String> VALID_SCOPES = ImmutableSet.of(
62 ScopeLimitingFilter.COMPILE_PLUS_RUNTIME,
63 JavaScopes.COMPILE,
64 JavaScopes.RUNTIME,
65 JavaScopes.TEST);
66
67 protected final PluginLog log = new PluginLog(this.getClass());
68
69 @Parameter(defaultValue = "${project}", readonly = true)
70 public MavenProject project;
71
72 @Parameter(defaultValue = "${session}", readonly = true)
73 public MavenSession mavenSession;
74
75 @Parameter(defaultValue = "${reactorProjects}", readonly = true, required = true)
76 public List<MavenProject> reactorProjects;
77
78 @Component
79 public ProjectBuilder mavenProjectBuilder;
80
81 @Component
82 public ProjectDependenciesResolver projectDependenciesResolver;
83
84 @Component
85 public RepositorySystem repositorySystem;
86
87
88
89
90 @Component
91 public StrategyProvider strategyProvider;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 @Parameter(alias = "exceptions")
111 public VersionCheckExcludes[] exclusions = new VersionCheckExcludes[0];
112
113
114
115
116 @Parameter(defaultValue = "false", property = "dvc.skip")
117 public boolean skip = false;
118
119
120
121
122
123
124
125 @Parameter(defaultValue = "false", property = "dvc.include-pom-projects")
126 public boolean includePomProjects = false;
127
128
129
130
131
132
133 @Parameter(defaultValue = "false", property = "dvc.quiet")
134 public boolean quiet = false;
135
136
137
138
139
140
141
142 @Parameter(defaultValue = "test", property = "scope")
143 public String scope = JavaScopes.TEST;
144
145
146
147
148
149
150
151 @Parameter(defaultValue = "false", property = "dvc.deep-scan")
152 public boolean deepScan = false;
153
154
155
156
157
158
159 @Parameter(defaultValue = "false", property = "dvc.direct-only")
160 public boolean directOnly = false;
161
162
163
164
165
166
167 @Parameter(defaultValue = "false", property = "dvc.managed-only")
168 public boolean managedOnly = false;
169
170
171
172
173
174
175
176 @Parameter(defaultValue = "true", property = "dvc.fast-resolution")
177 public boolean fastResolution = true;
178
179
180
181
182
183
184
185
186
187
188 @Parameter(defaultValue = "false", property = "dvc.unresolved-system-artifacts-fail-build")
189 protected boolean unresolvedSystemArtifactsFailBuild = false;
190
191
192
193
194
195
196
197 @Parameter(defaultValue = "false", property = "dvc.optional-dependencies-must-exist")
198 protected boolean optionalDependenciesMustExist = false;
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 @Parameter
222 public ResolverDefinition[] resolvers = new ResolverDefinition[0];
223
224
225
226
227
228
229
230
231
232 @Parameter(defaultValue = "default", property = "dvc.default-strategy")
233 public String defaultStrategy = "default";
234
235 protected StrategyCache strategyCache;
236 protected RepositorySystemSession snapshotFilteredSession;
237
238 @Override
239 @SuppressWarnings("PMD.AvoidRethrowingException")
240 public void execute()
241 throws MojoExecutionException, MojoFailureException {
242 try {
243 for (VersionCheckExcludes exclusion : exclusions) {
244 checkState(exclusion.isValid(), "Invalid exclusion specification: '%s'", exclusion);
245 }
246
247 checkState(!Strings.nullToEmpty(scope).trim().isEmpty() && VALID_SCOPES.contains(scope), "Scope '%s' is invalid", scope);
248
249 if (skip) {
250 log.report(quiet, "Skipping plugin execution");
251 return;
252 }
253
254 if (!includePomProjects && "pom".equals(project.getPackaging())) {
255 log.report(quiet, "Ignoring POM project");
256 return;
257 }
258
259 log.debug("Starting %s mojo run!", this.getClass().getSimpleName());
260
261 this.strategyCache = new StrategyCache(strategyProvider, resolvers, defaultStrategy);
262 this.snapshotFilteredSession = new DefaultRepositorySystemSession(mavenSession.getRepositorySession())
263 .setVersionFilter(new SnapshotVersionFilter());
264
265 final ScopeLimitingFilter scopeFilter = createScopeFilter();
266 final DependencyMap rootDependencyMap = new DependencyMapBuilder(this).mapProject(project, scopeFilter);
267
268 try (DependencyTreeResolver dependencyTreeResolver = new DependencyTreeResolver(this, rootDependencyMap)) {
269 final ImmutableSetMultimap<QualifiedName, VersionResolutionCollection> resolutionMap = dependencyTreeResolver.computeResolutionMap(project,
270 scopeFilter);
271 doExecute(resolutionMap, rootDependencyMap);
272 }
273 } catch (MojoExecutionException | MojoFailureException e) {
274 throw e;
275 } catch (Exception e) {
276 throw new MojoExecutionException("While running mojo: ", e);
277 } finally {
278 log.debug("Ended %s mojo run!", this.getClass().getSimpleName());
279 }
280 }
281
282
283
284
285
286
287
288
289 protected abstract void doExecute(ImmutableSetMultimap<QualifiedName, VersionResolutionCollection> resolutionMap, DependencyMap rootDependencyMap)
290 throws Exception;
291
292
293
294
295
296
297
298 protected ScopeLimitingFilter createScopeFilter() {
299 return ScopeLimitingFilter.computeDependencyScope(scope);
300 }
301
302 @Override
303 public boolean useFastResolution() {
304 return fastResolution;
305 }
306
307 @Override
308 public boolean useDeepScan() {
309 return deepScan;
310 }
311
312 @Override
313 public boolean isOptionalDependenciesMustExist() {
314 return optionalDependenciesMustExist;
315 }
316
317 @Override
318 public StrategyCache getStrategyCache() {
319 return strategyCache;
320 }
321
322 @Override
323 public ProjectBuilder getProjectBuilder() {
324 return mavenProjectBuilder;
325 }
326
327 @Override
328 public ProjectDependenciesResolver getProjectDependenciesResolver() {
329 return projectDependenciesResolver;
330 }
331
332 @Override
333 public MavenProject getRootProject() {
334 return project;
335 }
336
337 @Override
338 public List<MavenProject> getReactorProjects() {
339 return ImmutableList.copyOf(reactorProjects);
340 }
341
342 @Override
343 public RepositorySystemSession getRepositorySystemSession() {
344 return snapshotFilteredSession;
345 }
346
347 @Override
348 public RepositorySystem getRepositorySystem() {
349 return repositorySystem;
350 }
351
352 @Override
353 public ProjectBuildingRequest createProjectBuildingRequest() {
354 DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(mavenSession.getProjectBuildingRequest());
355 buildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories());
356 return buildingRequest;
357 }
358
359 @Override
360 public VersionRangeRequest createVersionRangeRequest(Artifact artifact) {
361 checkNotNull(artifact, "artifact is null");
362 return new VersionRangeRequest(artifact, project.getRemoteProjectRepositories(), "");
363 }
364
365 @Override
366 public List<VersionCheckExcludes> getExclusions() {
367 return Arrays.asList(exclusions);
368 }
369
370 @Override
371 public boolean isUnresolvedSystemArtifactsFailBuild() {
372 return unresolvedSystemArtifactsFailBuild;
373 }
374 }