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