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   * @since 2.0
48   */
49  @Mojo( name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
50  public class ResolvePluginsMojo
51      extends AbstractResolveMojo
52  {
53  
54      /**
55       * Remote repositories which will be searched for plugins.
56       */
57      @Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true )
58      private List<ArtifactRepository> remotePluginRepositories;
59  
60      /**
61       * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
62       *
63       * @throws MojoExecutionException with a message if an error occurs.
64       */
65      @Override
66      protected void doExecute()
67          throws MojoExecutionException
68      {
69          try
70          {
71              // ideally this should either be DependencyCoordinates or DependencyNode
72              final Set<Artifact> plugins = resolvePluginArtifacts();
73  
74              StringBuilder sb = new StringBuilder();
75              sb.append( "\n" );
76              sb.append( "The following plugins have been resolved:\n" );
77              if ( plugins == null || plugins.isEmpty() )
78              {
79                  sb.append( "   none\n" );
80              }
81              else
82              {
83                  for ( Artifact plugin : plugins )
84                  {
85                      String artifactFilename = null;
86                      if ( outputAbsoluteArtifactFilename )
87                      {
88                          try
89                          {
90                              // we want to print the absolute file name here
91                              artifactFilename = plugin.getFile().getAbsoluteFile().getPath();
92                          }
93                          catch ( NullPointerException e )
94                          {
95                              // ignore the null pointer, we'll output a null string
96                              artifactFilename = null;
97                          }
98                      }
99  
100                     String id = plugin.toString();
101                     sb.append( "   " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) + "\n" );
102 
103                     if ( !excludeTransitive )
104                     {
105                         DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
106                         pluginCoordinate.setGroupId( plugin.getGroupId() );
107                         pluginCoordinate.setArtifactId( plugin.getArtifactId() );
108                         pluginCoordinate.setVersion( plugin.getVersion() );
109 
110                         for ( final Artifact artifact : resolveArtifactDependencies( pluginCoordinate ) )
111                         {
112                             artifactFilename = null;
113                             if ( outputAbsoluteArtifactFilename )
114                             {
115                                 try
116                                 {
117                                     // we want to print the absolute file name here
118                                     artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
119                                 }
120                                 catch ( NullPointerException e )
121                                 {
122                                     // ignore the null pointer, we'll output a null string
123                                     artifactFilename = null;
124                                 }
125                             }
126 
127                             id = artifact.toString();
128                             sb.append( "      " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" )
129                                 + "\n" );
130                         }
131                     }
132                 }
133                 sb.append( "\n" );
134 
135                 String output = sb.toString();
136                 if ( outputFile == null )
137                 {
138                     DependencyUtil.log( output, getLog() );
139                 }
140                 else
141                 {
142                     DependencyUtil.write( output, outputFile, appendOutput, getLog() );
143                 }
144             }
145         }
146         catch ( final IOException e )
147         {
148             throw new MojoExecutionException( e.getMessage(), e );
149         }
150         catch ( final ArtifactFilterException e )
151         {
152             throw new MojoExecutionException( e.getMessage(), e );
153         }
154         catch ( ArtifactResolverException e )
155         {
156             throw new MojoExecutionException( e.getMessage(), e );
157         }
158         catch ( DependencyResolverException e )
159         {
160             throw new MojoExecutionException( e.getMessage(), e );
161         }
162     }
163 
164     /**
165      * This method resolves the plugin artifacts from the project.
166      *
167      * @return set of resolved plugin artifacts.
168      * @throws ArtifactFilterException in case of an error.
169      * @throws ArtifactResolverException in case of an error.
170      */
171     protected Set<Artifact> resolvePluginArtifacts()
172         throws ArtifactFilterException, ArtifactResolverException
173     {
174         final Set<Artifact> plugins = getProject().getPluginArtifacts();
175         final Set<Artifact> reports = getProject().getReportArtifacts();
176 
177         Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
178         artifacts.addAll( reports );
179         artifacts.addAll( plugins );
180 
181         final FilterArtifacts filter = getPluginArtifactsFilter();
182         artifacts = filter.filter( artifacts );
183 
184         Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>( artifacts.size() );
185         // final ArtifactFilter filter = getPluginFilter();
186         for ( final Artifact artifact : new LinkedHashSet<Artifact>( artifacts ) )
187         {
188             // if ( !filter.include( artifact ) )
189             // {
190             // final String logStr =
191             // String.format( " Plugin SKIPPED: %s", DependencyUtil.getFormattedFileName( artifact, false ) );
192             //
193             // if ( !silent )
194             // {
195             // this.getLog().info( logStr );
196             // }
197             //
198             // artifacts.remove( artifact );
199             // continue;
200             // }
201 
202             ProjectBuildingRequest buildingRequest =
203                 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
204 
205             buildingRequest.setRemoteRepositories( this.remotePluginRepositories );
206 
207             // resolve the new artifact
208             resolvedArtifacts.add( getArtifactResolver().resolveArtifact( buildingRequest, artifact ).getArtifact() );
209         }
210         return artifacts;
211     }
212 
213     @Override
214     protected ArtifactsFilter getMarkedArtifactFilter()
215     {
216         return null;
217     }
218 }