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 java.io.IOException;
22  import java.util.LinkedHashSet;
23  import java.util.Objects;
24  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
32  import org.apache.maven.project.ProjectBuildingRequest;
33  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
34  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
35  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
36  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
37  import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
38  import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
39  
40  /**
41   * Goal that resolves all project plugins and reports and their dependencies.
42   *
43   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
44   * @since 2.0
45   */
46  @Mojo(name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
47  public class ResolvePluginsMojo extends AbstractResolveMojo {
48  
49      @Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}")
50      private String outputEncoding;
51  
52      /**
53       * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
54       *
55       * @throws MojoExecutionException with a message if an error occurs.
56       */
57      @Override
58      protected void doExecute() throws MojoExecutionException {
59          try {
60              // ideally this should either be DependencyCoordinates or DependencyNode
61              final Set<Artifact> plugins = resolvePluginArtifacts();
62  
63              StringBuilder sb = new StringBuilder();
64              sb.append(System.lineSeparator());
65              sb.append("The following plugins have been resolved:");
66              sb.append(System.lineSeparator());
67              if (plugins == null || plugins.isEmpty()) {
68                  sb.append("   none");
69                  sb.append(System.lineSeparator());
70              } else {
71                  for (Artifact plugin : plugins) {
72                      String artifactFilename = null;
73                      if (outputAbsoluteArtifactFilename) {
74                          try {
75                              // we want to print the absolute file name here
76                              artifactFilename =
77                                      plugin.getFile().getAbsoluteFile().getPath();
78                          } catch (NullPointerException e) {
79                              // ignore the null pointer, we'll output a null string
80                              artifactFilename = null;
81                          }
82                      }
83  
84                      String id = plugin.toString();
85                      sb.append("   ")
86                              .append(id)
87                              .append(outputAbsoluteArtifactFilename ? ":" + artifactFilename : "")
88                              .append(System.lineSeparator());
89  
90                      if (!excludeTransitive) {
91                          DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
92                          pluginCoordinate.setGroupId(plugin.getGroupId());
93                          pluginCoordinate.setArtifactId(plugin.getArtifactId());
94                          pluginCoordinate.setVersion(plugin.getVersion());
95  
96                          for (final Artifact artifact : resolveArtifactDependencies(pluginCoordinate)) {
97                              artifactFilename = null;
98                              if (outputAbsoluteArtifactFilename) {
99                                  try {
100                                     // we want to print the absolute file name here
101                                     artifactFilename =
102                                             artifact.getFile().getAbsoluteFile().getPath();
103                                 } catch (NullPointerException e) {
104                                     // ignore the null pointer, we'll output a null string
105                                     artifactFilename = null;
106                                 }
107                             }
108 
109                             id = artifact.toString();
110                             sb.append("      ")
111                                     .append(id)
112                                     .append(outputAbsoluteArtifactFilename ? ":" + artifactFilename : "")
113                                     .append(System.lineSeparator());
114                         }
115                     }
116                 }
117                 sb.append(System.lineSeparator());
118 
119                 String output = sb.toString();
120                 if (outputFile == null) {
121                     DependencyUtil.log(output, getLog());
122                 } else {
123                     String encoding = Objects.toString(outputEncoding, "UTF-8");
124                     DependencyUtil.write(output, outputFile, appendOutput, encoding);
125                 }
126             }
127         } catch (IOException | ArtifactFilterException | ArtifactResolverException | DependencyResolverException e) {
128             throw new MojoExecutionException(e.getMessage(), e);
129         }
130     }
131 
132     /**
133      * This method resolves the plugin artifacts from the project.
134      *
135      * @return set of resolved plugin artifacts
136      * @throws ArtifactFilterException in case of an error
137      * @throws ArtifactResolverException in case of an error
138      */
139     protected Set<Artifact> resolvePluginArtifacts() throws ArtifactFilterException, ArtifactResolverException {
140         final Set<Artifact> plugins = getProject().getPluginArtifacts();
141         final Set<Artifact> reports = getProject().getReportArtifacts();
142 
143         Set<Artifact> artifacts = new LinkedHashSet<>();
144         artifacts.addAll(reports);
145         artifacts.addAll(plugins);
146 
147         final FilterArtifacts filter = getArtifactsFilter();
148         artifacts = filter.filter(artifacts);
149 
150         // final ArtifactFilter filter = getPluginFilter();
151         for (final Artifact artifact : new LinkedHashSet<>(artifacts)) {
152             // if ( !filter.include( artifact ) )
153             // {
154             // final String logStr =
155             // String.format( " Plugin SKIPPED: %s", DependencyUtil.getFormattedFileName( artifact, false ) );
156             //
157             // if ( !silent )
158             // {
159             // this.getLog().info( logStr );
160             // }
161             //
162             // artifacts.remove( artifact );
163             // continue;
164             // }
165 
166             ProjectBuildingRequest buildingRequest = newResolvePluginProjectBuildingRequest();
167 
168             // resolve the new artifact
169             getArtifactResolver().resolveArtifact(buildingRequest, artifact).getArtifact();
170         }
171         return artifacts;
172     }
173 
174     @Override
175     protected ArtifactsFilter getMarkedArtifactFilter() {
176         return null;
177     }
178 }