View Javadoc
1   package org.apache.maven.plugins.dependency.resolvers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugins.annotations.LifecyclePhase;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
34  import org.apache.maven.project.DefaultProjectBuildingRequest;
35  import org.apache.maven.project.ProjectBuildingRequest;
36  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
37  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
38  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
39  import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
40  import org.apache.maven.shared.dependencies.DefaultDependableCoordinate;
41  import org.apache.maven.shared.dependencies.resolve.DependencyResolverException;
42  
43  /**
44   * Goal that resolves all project plugins and reports and their dependencies.
45   *
46   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
47   * @version $Id: ResolvePluginsMojo.java 1807877 2017-09-09 10:35:59Z khmarbaise $
48   * @since 2.0
49   */
50  @Mojo( name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
51  public class ResolvePluginsMojo
52      extends AbstractResolveMojo
53  {
54  
55      /**
56       * Remote repositories which will be searched for plugins.
57       */
58      @Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true )
59      private List<ArtifactRepository> remotePluginRepositories;
60  
61      /**
62       * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
63       *
64       * @throws MojoExecutionException with a message if an error occurs.
65       */
66      @Override
67      protected void doExecute()
68          throws MojoExecutionException
69      {
70          try
71          {
72              // ideally this should either be DependencyCoordinates or DependencyNode
73              final Set<Artifact> plugins = resolvePluginArtifacts();
74  
75              StringBuilder sb = new StringBuilder();
76              sb.append( "\n" );
77              sb.append( "The following plugins have been resolved:\n" );
78              if ( plugins == null || plugins.isEmpty() )
79              {
80                  sb.append( "   none\n" );
81              }
82              else
83              {
84                  for ( Artifact plugin : plugins )
85                  {
86                      String artifactFilename = null;
87                      if ( outputAbsoluteArtifactFilename )
88                      {
89                          try
90                          {
91                              // we want to print the absolute file name here
92                              artifactFilename = plugin.getFile().getAbsoluteFile().getPath();
93                          }
94                          catch ( NullPointerException e )
95                          {
96                              // ignore the null pointer, we'll output a null string
97                              artifactFilename = null;
98                          }
99                      }
100 
101                     String id = plugin.toString();
102                     sb.append( "   " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) + "\n" );
103 
104                     if ( !excludeTransitive )
105                     {
106                         DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
107                         pluginCoordinate.setGroupId( plugin.getGroupId() );
108                         pluginCoordinate.setArtifactId( plugin.getArtifactId() );
109                         pluginCoordinate.setVersion( plugin.getVersion() );
110 
111                         for ( final Artifact artifact : resolveArtifactDependencies( pluginCoordinate ) )
112                         {
113                             artifactFilename = null;
114                             if ( outputAbsoluteArtifactFilename )
115                             {
116                                 try
117                                 {
118                                     // we want to print the absolute file name here
119                                     artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
120                                 }
121                                 catch ( NullPointerException e )
122                                 {
123                                     // ignore the null pointer, we'll output a null string
124                                     artifactFilename = null;
125                                 }
126                             }
127 
128                             id = artifact.toString();
129                             sb.append( "      " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" )
130                                 + "\n" );
131                         }
132                     }
133                 }
134                 sb.append( "\n" );
135 
136                 String output = sb.toString();
137                 if ( outputFile == null )
138                 {
139                     DependencyUtil.log( output, getLog() );
140                 }
141                 else
142                 {
143                     DependencyUtil.write( output, outputFile, appendOutput, getLog() );
144                 }
145             }
146         }
147         catch ( final IOException e )
148         {
149             throw new MojoExecutionException( e.getMessage(), e );
150         }
151         catch ( final ArtifactFilterException e )
152         {
153             throw new MojoExecutionException( e.getMessage(), e );
154         }
155         catch ( ArtifactResolverException e )
156         {
157             throw new MojoExecutionException( e.getMessage(), e );
158         }
159         catch ( DependencyResolverException e )
160         {
161             throw new MojoExecutionException( e.getMessage(), e );
162         }
163     }
164 
165     /**
166      * This method resolves the plugin artifacts from the project.
167      *
168      * @return set of resolved plugin artifacts.
169      * @throws ArtifactFilterException in case of an error.
170      * @throws ArtifactResolverException in case of an error.
171      */
172     protected Set<Artifact> resolvePluginArtifacts()
173         throws ArtifactFilterException, ArtifactResolverException
174     {
175         final Set<Artifact> plugins = getProject().getPluginArtifacts();
176         final Set<Artifact> reports = getProject().getReportArtifacts();
177 
178         Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
179         artifacts.addAll( reports );
180         artifacts.addAll( plugins );
181 
182         final FilterArtifacts filter = getPluginArtifactsFilter();
183         artifacts = filter.filter( artifacts );
184 
185         Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>( artifacts.size() );
186         // final ArtifactFilter filter = getPluginFilter();
187         for ( final Artifact artifact : new LinkedHashSet<Artifact>( artifacts ) )
188         {
189             // if ( !filter.include( artifact ) )
190             // {
191             // final String logStr =
192             // String.format( " Plugin SKIPPED: %s", DependencyUtil.getFormattedFileName( artifact, false ) );
193             //
194             // if ( !silent )
195             // {
196             // this.getLog().info( logStr );
197             // }
198             //
199             // artifacts.remove( artifact );
200             // continue;
201             // }
202 
203             ProjectBuildingRequest buildingRequest =
204                 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
205 
206             buildingRequest.setRemoteRepositories( this.remotePluginRepositories );
207 
208             // resolve the new artifact
209             resolvedArtifacts.add( getArtifactResolver().resolveArtifact( buildingRequest, artifact ).getArtifact() );
210         }
211         return artifacts;
212     }
213 
214     @Override
215     protected ArtifactsFilter getMarkedArtifactFilter()
216     {
217         return null;
218     }
219 }