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 for (Artifact artifact : artifacts) {
209 MessageBuilder messageBuilder = MessageUtils.buffer();
210 messageBuilder.a(" ");
211
212 if (theOutputScope) {
213 messageBuilder.a(artifact.toString());
214 } else {
215 messageBuilder.a(artifact.getId());
216 }
217
218 if (outputAbsoluteArtifactFilename) {
219 try {
220
221 String artifactFilename =
222 artifact.getFile().getAbsoluteFile().getPath();
223
224 messageBuilder.a(':').a(artifactFilename);
225 } catch (NullPointerException e) {
226
227 }
228 }
229
230 if (theOutputScope && artifact.isOptional()) {
231 messageBuilder.a(" (optional)");
232 }
233
234
235 if (artifact.getFile() != null) {
236 ModuleDescriptor moduleDescriptor = getModuleDescriptor(artifact.getFile());
237 if (moduleDescriptor != null) {
238 messageBuilder.project(" -- module " + moduleDescriptor.name);
239
240 if (moduleDescriptor.automatic) {
241 if ("MANIFEST".equals(moduleDescriptor.moduleNameSource)) {
242 messageBuilder.strong(" [auto]");
243 } else {
244 messageBuilder.warning(" (auto)");
245 }
246 }
247 }
248 }
249 String message = messageBuilder.build();
250 if (outputFile != null) {
251 message = MessageUtils.stripAnsiCodes(message);
252 }
253 artifactStringList.add(message + System.lineSeparator());
254 }
255 if (theSort) {
256 Collections.sort(artifactStringList);
257 }
258 for (String artifactString : artifactStringList) {
259 sb.append(artifactString);
260 }
261 return sb;
262 }
263
264 private ModuleDescriptor getModuleDescriptor(File artifactFile) {
265 ModuleDescriptor moduleDescriptor = null;
266 try {
267
268 Class<?> moduleFinderClass = Class.forName("java.lang.module.ModuleFinder");
269
270 java.nio.file.Path path = artifactFile.toPath();
271
272 Method ofMethod = moduleFinderClass.getMethod("of", java.nio.file.Path[].class);
273 Object moduleFinderInstance = ofMethod.invoke(null, new Object[] {new java.nio.file.Path[] {path}});
274
275 Method findAllMethod = moduleFinderClass.getMethod("findAll");
276 Set<Object> moduleReferences = (Set<Object>) findAllMethod.invoke(moduleFinderInstance);
277
278
279 if (!moduleReferences.isEmpty()) {
280 Object moduleReference = moduleReferences.iterator().next();
281 Method descriptorMethod = moduleReference.getClass().getMethod("descriptor");
282 Object moduleDescriptorInstance = descriptorMethod.invoke(moduleReference);
283
284 Method nameMethod = moduleDescriptorInstance.getClass().getMethod("name");
285 String name = (String) nameMethod.invoke(moduleDescriptorInstance);
286
287 moduleDescriptor = new ModuleDescriptor();
288 moduleDescriptor.name = name;
289
290 Method isAutomaticMethod = moduleDescriptorInstance.getClass().getMethod("isAutomatic");
291 moduleDescriptor.automatic = (Boolean) isAutomaticMethod.invoke(moduleDescriptorInstance);
292
293 if (moduleDescriptor.automatic) {
294 if (artifactFile.isFile()) {
295 try (JarFile jarFile = new JarFile(artifactFile)) {
296 Manifest manifest = jarFile.getManifest();
297
298 if (manifest != null
299 && manifest.getMainAttributes().getValue("Automatic-Module-Name") != null) {
300 moduleDescriptor.moduleNameSource = "MANIFEST";
301 } else {
302 moduleDescriptor.moduleNameSource = "FILENAME";
303 }
304 } catch (IOException e) {
305
306 }
307 }
308 }
309 }
310 } catch (ClassNotFoundException | SecurityException | IllegalAccessException | IllegalArgumentException e) {
311
312 } catch (NoSuchMethodException e) {
313 getLog().warn(e);
314 } catch (InvocationTargetException e) {
315 Throwable cause = e.getCause();
316 while (cause.getCause() != null) {
317 cause = cause.getCause();
318 }
319 getLog().info("Can't extract module name from " + artifactFile.getName() + ": " + cause.getMessage());
320 }
321 return moduleDescriptor;
322 }
323
324 private static class ModuleDescriptor {
325 String name;
326
327 boolean automatic = true;
328
329 String moduleNameSource;
330 }
331 }