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         /* if (outputFile != null) {
209             MessageUtils.setColorEnabled(false);
210         } else {
211             MessageUtils.setColorEnabled(true);
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                     // we want to print the absolute file name here
226                     String artifactFilename =
227                             artifact.getFile().getAbsoluteFile().getPath();
228 
229                     messageBuilder.a(':').a(artifactFilename);
230                 } catch (NullPointerException e) {
231                     // ignore the null pointer, we'll output a null string
232                 }
233             }
234 
235             if (theOutputScope && artifact.isOptional()) {
236                 messageBuilder.a(" (optional)");
237             }
238 
239             // dependencies:collect won't download jars
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             // Use Java9 code to get moduleName, don't try to do it better with own implementation
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             // moduleReferences can be empty when referring to target/classes without module-info.class
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                             // noop
307                         }
308                     }
309                 }
310             }
311         } catch (ClassNotFoundException | SecurityException | IllegalAccessException | IllegalArgumentException e) {
312             // do nothing
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 }