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.transfer.artifact.resolve.ArtifactResolverException;
40  import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
41  import org.apache.maven.shared.transfer.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( System.lineSeparator() );
76              sb.append( "The following plugins have been resolved:" );
77              sb.append( System.lineSeparator() );
78              if ( plugins == null || plugins.isEmpty() )
79              {
80                  sb.append( "   none" );
81                  sb.append( System.lineSeparator() );
82              }
83              else
84              {
85                  for ( Artifact plugin : plugins )
86                  {
87                      String artifactFilename = null;
88                      if ( outputAbsoluteArtifactFilename )
89                      {
90                          try
91                          {
92                              // we want to print the absolute file name here
93                              artifactFilename = plugin.getFile().getAbsoluteFile().getPath();
94                          }
95                          catch ( NullPointerException e )
96                          {
97                              // ignore the null pointer, we'll output a null string
98                              artifactFilename = null;
99                          }
100                     }
101 
102                     String id = plugin.toString();
103                     sb.append( "   " )
104                             .append( id )
105                             .append( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" )
106                             .append( System.lineSeparator() );
107 
108                     if ( !excludeTransitive )
109                     {
110                         DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
111                         pluginCoordinate.setGroupId( plugin.getGroupId() );
112                         pluginCoordinate.setArtifactId( plugin.getArtifactId() );
113                         pluginCoordinate.setVersion( plugin.getVersion() );
114 
115                         for ( final Artifact artifact : resolveArtifactDependencies( pluginCoordinate ) )
116                         {
117                             artifactFilename = null;
118                             if ( outputAbsoluteArtifactFilename )
119                             {
120                                 try
121                                 {
122                                     // we want to print the absolute file name here
123                                     artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
124                                 }
125                                 catch ( NullPointerException e )
126                                 {
127                                     // ignore the null pointer, we'll output a null string
128                                     artifactFilename = null;
129                                 }
130                             }
131 
132                             id = artifact.toString();
133                             sb.append( "      " )
134                                     .append( id )
135                                     .append( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" )
136                                     .append( System.lineSeparator() );
137                         }
138                     }
139                 }
140                 sb.append( System.lineSeparator() );
141 
142                 String output = sb.toString();
143                 if ( outputFile == null )
144                 {
145                     DependencyUtil.log( output, getLog() );
146                 }
147                 else
148                 {
149                     DependencyUtil.write( output, outputFile, appendOutput, getLog() );
150                 }
151             }
152         }
153         catch ( IOException | ArtifactFilterException | ArtifactResolverException | DependencyResolverException e )
154         {
155             throw new MojoExecutionException( e.getMessage(), e );
156         }
157     }
158 
159     /**
160      * This method resolves the plugin artifacts from the project.
161      *
162      * @return set of resolved plugin artifacts.
163      * @throws ArtifactFilterException in case of an error.
164      * @throws ArtifactResolverException in case of an error.
165      */
166     protected Set<Artifact> resolvePluginArtifacts()
167         throws ArtifactFilterException, ArtifactResolverException
168     {
169         final Set<Artifact> plugins = getProject().getPluginArtifacts();
170         final Set<Artifact> reports = getProject().getReportArtifacts();
171 
172         Set<Artifact> artifacts = new LinkedHashSet<>();
173         artifacts.addAll( reports );
174         artifacts.addAll( plugins );
175 
176         final FilterArtifacts filter = getArtifactsFilter();
177         artifacts = filter.filter( artifacts );
178 
179         Set<Artifact> resolvedArtifacts = new LinkedHashSet<>( artifacts.size() );
180         // final ArtifactFilter filter = getPluginFilter();
181         for ( final Artifact artifact : new LinkedHashSet<>( artifacts ) )
182         {
183             // if ( !filter.include( artifact ) )
184             // {
185             // final String logStr =
186             // String.format( " Plugin SKIPPED: %s", DependencyUtil.getFormattedFileName( artifact, false ) );
187             //
188             // if ( !silent )
189             // {
190             // this.getLog().info( logStr );
191             // }
192             //
193             // artifacts.remove( artifact );
194             // continue;
195             // }
196 
197             ProjectBuildingRequest buildingRequest =
198                 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
199 
200             buildingRequest.setRemoteRepositories( this.remotePluginRepositories );
201 
202             // resolve the new artifact
203             resolvedArtifacts.add( getArtifactResolver().resolveArtifact( buildingRequest, artifact ).getArtifact() );
204         }
205         return artifacts;
206     }
207 
208     @Override
209     protected ArtifactsFilter getMarkedArtifactFilter()
210     {
211         return null;
212     }
213 }