1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.resolvers;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.Set;
33 import java.util.jar.JarFile;
34 import java.util.jar.Manifest;
35
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
38 import org.apache.maven.execution.MavenSession;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.plugins.annotations.LifecyclePhase;
41 import org.apache.maven.plugins.annotations.Mojo;
42 import org.apache.maven.plugins.annotations.Parameter;
43 import org.apache.maven.plugins.annotations.ResolutionScope;
44 import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
45 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
46 import org.apache.maven.plugins.dependency.utils.ResolverUtil;
47 import org.apache.maven.plugins.dependency.utils.filters.ResolveFileFilter;
48 import org.apache.maven.plugins.dependency.utils.markers.SourcesFileMarkerHandler;
49 import org.apache.maven.project.MavenProject;
50 import org.apache.maven.project.ProjectBuilder;
51 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
52 import org.apache.maven.shared.utils.logging.MessageBuilder;
53 import org.apache.maven.shared.utils.logging.MessageUtils;
54 import org.sonatype.plexus.build.incremental.BuildContext;
55
56
57
58
59
60
61
62
63 @Mojo(
64 name = "resolve",
65 requiresDependencyResolution = ResolutionScope.TEST,
66 defaultPhase = LifecyclePhase.GENERATE_SOURCES,
67 threadSafe = true)
68 public class ResolveDependenciesMojo extends AbstractResolveMojo {
69
70 @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}")
71 private String outputEncoding;
72
73
74
75
76
77
78 @Parameter(property = "mdep.outputScope", defaultValue = "true")
79 protected boolean outputScope;
80
81
82
83
84
85
86 @Parameter(property = "outputAbsoluteArtifactFilename", defaultValue = "false")
87 private boolean outputAbsoluteArtifactFilename;
88
89
90
91
92 DependencyStatusSets results;
93
94
95
96
97
98
99 @Parameter(property = "sort", defaultValue = "false")
100 boolean sort;
101
102
103
104
105
106
107 @Parameter(property = "includeParents", defaultValue = "false")
108 boolean includeParents;
109
110 @Inject
111
112 public ResolveDependenciesMojo(
113 MavenSession session,
114 BuildContext buildContext,
115 MavenProject project,
116 ResolverUtil resolverUtil,
117 ProjectBuilder projectBuilder,
118 ArtifactHandlerManager artifactHandlerManager) {
119 super(session, buildContext, project, resolverUtil, projectBuilder, artifactHandlerManager);
120 }
121
122
123
124
125
126
127
128 @Override
129 protected void doExecute() throws MojoExecutionException {
130
131 results = this.getDependencySets(false, includeParents);
132
133 String output = getOutput(outputAbsoluteArtifactFilename, outputScope, sort);
134 try {
135 if (outputFile == null) {
136 DependencyUtil.log(output, getLog());
137 } else {
138 String encoding = Objects.toString(outputEncoding, "UTF-8");
139 DependencyUtil.write(output, outputFile, appendOutput, encoding);
140 }
141 } catch (IOException e) {
142 throw new MojoExecutionException(e.getMessage(), e);
143 }
144 }
145
146
147
148
149 public DependencyStatusSets getResults() {
150 return this.results;
151 }
152
153 @Override
154 protected ArtifactsFilter getMarkedArtifactFilter() {
155 return new ResolveFileFilter(new SourcesFileMarkerHandler(this.markersDirectory));
156 }
157
158
159
160
161
162
163
164 public String getOutput(boolean outputAbsoluteArtifactFilename, boolean theOutputScope, boolean theSort) {
165 StringBuilder sb = new StringBuilder();
166 if (outputFile == null) {
167 sb.append(System.lineSeparator());
168 }
169 sb.append("The following files have been resolved:");
170 sb.append(System.lineSeparator());
171 if (results.getResolvedDependencies() == null
172 || results.getResolvedDependencies().isEmpty()) {
173 sb.append(" none");
174 sb.append(System.lineSeparator());
175 } else {
176 sb.append(buildArtifactListOutput(
177 results.getResolvedDependencies(), outputAbsoluteArtifactFilename, theOutputScope, theSort));
178 }
179
180 if (results.getSkippedDependencies() != null
181 && !results.getSkippedDependencies().isEmpty()) {
182 sb.append(System.lineSeparator());
183 sb.append("The following files were skipped:");
184 sb.append(System.lineSeparator());
185 Set<Artifact> skippedDependencies = new LinkedHashSet<>(results.getSkippedDependencies());
186 sb.append(buildArtifactListOutput(
187 skippedDependencies, outputAbsoluteArtifactFilename, theOutputScope, theSort));
188 }
189
190 if (results.getUnResolvedDependencies() != null
191 && !results.getUnResolvedDependencies().isEmpty()) {
192 sb.append(System.lineSeparator());
193 sb.append("The following files have NOT been resolved:");
194 sb.append(System.lineSeparator());
195 Set<Artifact> unResolvedDependencies = new LinkedHashSet<>(results.getUnResolvedDependencies());
196 sb.append(buildArtifactListOutput(
197 unResolvedDependencies, outputAbsoluteArtifactFilename, theOutputScope, theSort));
198 }
199 sb.append(System.lineSeparator());
200
201 return sb.toString();
202 }
203
204 private StringBuilder buildArtifactListOutput(
205 Set<Artifact> artifacts, boolean outputAbsoluteArtifactFilename, boolean theOutputScope, boolean theSort) {
206 StringBuilder sb = new StringBuilder();
207 List<String> artifactStringList = new ArrayList<>();
208
209
210
211
212
213 for (Artifact artifact : artifacts) {
214 MessageBuilder messageBuilder = MessageUtils.buffer();
215 messageBuilder.a(" ");
216
217 if (theOutputScope) {
218 messageBuilder.a(artifact.toString());
219 } else {
220 messageBuilder.a(artifact.getId());
221 }
222
223 if (outputAbsoluteArtifactFilename) {
224 try {
225
226 String artifactFilename =
227 artifact.getFile().getAbsoluteFile().getPath();
228
229 messageBuilder.a(':').a(artifactFilename);
230 } catch (NullPointerException e) {
231
232 }
233 }
234
235 if (theOutputScope && artifact.isOptional()) {
236 messageBuilder.a(" (optional)");
237 }
238
239
240 if (artifact.getFile() != null) {
241 ModuleDescriptor moduleDescriptor = getModuleDescriptor(artifact.getFile());
242 if (moduleDescriptor != null) {
243 messageBuilder.project(" -- module " + moduleDescriptor.name);
244
245 if (moduleDescriptor.automatic) {
246 if ("MANIFEST".equals(moduleDescriptor.moduleNameSource)) {
247 messageBuilder.strong(" [auto]");
248 } else {
249 messageBuilder.warning(" (auto)");
250 }
251 }
252 }
253 }
254 artifactStringList.add(messageBuilder.build() + System.lineSeparator());
255 }
256 if (theSort) {
257 Collections.sort(artifactStringList);
258 }
259 for (String artifactString : artifactStringList) {
260 sb.append(artifactString);
261 }
262 return sb;
263 }
264
265 private ModuleDescriptor getModuleDescriptor(File artifactFile) {
266 ModuleDescriptor moduleDescriptor = null;
267 try {
268
269 Class<?> moduleFinderClass = Class.forName("java.lang.module.ModuleFinder");
270
271 java.nio.file.Path path = artifactFile.toPath();
272
273 Method ofMethod = moduleFinderClass.getMethod("of", java.nio.file.Path[].class);
274 Object moduleFinderInstance = ofMethod.invoke(null, new Object[] {new java.nio.file.Path[] {path}});
275
276 Method findAllMethod = moduleFinderClass.getMethod("findAll");
277 Set<Object> moduleReferences = (Set<Object>) findAllMethod.invoke(moduleFinderInstance);
278
279
280 if (!moduleReferences.isEmpty()) {
281 Object moduleReference = moduleReferences.iterator().next();
282 Method descriptorMethod = moduleReference.getClass().getMethod("descriptor");
283 Object moduleDescriptorInstance = descriptorMethod.invoke(moduleReference);
284
285 Method nameMethod = moduleDescriptorInstance.getClass().getMethod("name");
286 String name = (String) nameMethod.invoke(moduleDescriptorInstance);
287
288 moduleDescriptor = new ModuleDescriptor();
289 moduleDescriptor.name = name;
290
291 Method isAutomaticMethod = moduleDescriptorInstance.getClass().getMethod("isAutomatic");
292 moduleDescriptor.automatic = (Boolean) isAutomaticMethod.invoke(moduleDescriptorInstance);
293
294 if (moduleDescriptor.automatic) {
295 if (artifactFile.isFile()) {
296 try (JarFile jarFile = new JarFile(artifactFile)) {
297 Manifest manifest = jarFile.getManifest();
298
299 if (manifest != null
300 && manifest.getMainAttributes().getValue("Automatic-Module-Name") != null) {
301 moduleDescriptor.moduleNameSource = "MANIFEST";
302 } else {
303 moduleDescriptor.moduleNameSource = "FILENAME";
304 }
305 } catch (IOException e) {
306
307 }
308 }
309 }
310 }
311 } catch (ClassNotFoundException | SecurityException | IllegalAccessException | IllegalArgumentException e) {
312
313 } catch (NoSuchMethodException e) {
314 getLog().warn(e);
315 } catch (InvocationTargetException e) {
316 Throwable cause = e.getCause();
317 while (cause.getCause() != null) {
318 cause = cause.getCause();
319 }
320 getLog().info("Can't extract module name from " + artifactFile.getName() + ": " + cause.getMessage());
321 }
322 return moduleDescriptor;
323 }
324
325 private static class ModuleDescriptor {
326 String name;
327
328 boolean automatic = true;
329
330 String moduleNameSource;
331 }
332 }