View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Goal that resolves the project dependencies from the repository. When running on Java 9, the
58   * module names will be visible as well.
59   *
60   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
61   * @since 2.0
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       * If we should display the scope when resolving.
75       *
76       * @since 2.0-alpha-2
77       */
78      @Parameter(property = "mdep.outputScope", defaultValue = "true")
79      protected boolean outputScope;
80  
81      /**
82       * Output absolute filename for resolved artifacts.
83       *
84       * @since 2.0
85       */
86      @Parameter(property = "outputAbsoluteArtifactFilename", defaultValue = "false")
87      private boolean outputAbsoluteArtifactFilename;
88  
89      /**
90       * Only used to store results for integration test validation.
91       */
92      DependencyStatusSets results;
93  
94      /**
95       * Sort the output list of resolved artifacts alphabetically. The default ordering matches the classpath order.
96       *
97       * @since 2.8
98       */
99      @Parameter(property = "sort", defaultValue = "false")
100     boolean sort;
101 
102     /**
103      * Include parent poms in the dependency resolution list.
104      *
105      * @since 2.8
106      */
107     @Parameter(property = "includeParents", defaultValue = "false")
108     boolean includeParents;
109 
110     @Inject
111     // CHECKSTYLE_OFF: ParameterNumber
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     // CHECKSTYLE_ON: ParameterNumber
122 
123     /**
124      * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
125      *
126      * @throws MojoExecutionException with a message if an error occurs
127      */
128     @Override
129     protected void doExecute() throws MojoExecutionException {
130         // get sets of dependencies
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      * @return returns the results
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      * @param outputAbsoluteArtifactFilename absolute artifact filename
160      * @param theOutputScope the output scope
161      * @param theSort sort yes/no
162      * @return the output
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                     // we want to print the absolute file name here
221                     String artifactFilename =
222                             artifact.getFile().getAbsoluteFile().getPath();
223 
224                     messageBuilder.a(':').a(artifactFilename);
225                 } catch (NullPointerException e) {
226                     // ignore the null pointer, we'll output a null string
227                 }
228             }
229 
230             if (theOutputScope && artifact.isOptional()) {
231                 messageBuilder.a(" (optional)");
232             }
233 
234             // dependencies:collect won't download jars
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             // Use Java9 code to get moduleName, don't try to do it better with own implementation
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             // moduleReferences can be empty when referring to target/classes without module-info.class
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                             // noop
306                         }
307                     }
308                 }
309             }
310         } catch (ClassNotFoundException | SecurityException | IllegalAccessException | IllegalArgumentException e) {
311             // do nothing
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 }