View Javadoc

1   package org.apache.maven.plugin.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.FileWriter;
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
32  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.dependency.AbstractResolveMojo;
35  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
36  import org.apache.maven.plugins.annotations.LifecyclePhase;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  import org.apache.maven.project.ProjectBuildingException;
40  import org.apache.maven.project.artifact.InvalidDependencyVersionException;
41  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
42  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
43  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
44  import org.codehaus.plexus.util.IOUtil;
45  
46  /**
47   * Goal that resolves all project plugins and reports and their dependencies.
48   *
49   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
50   * @version $Id: ResolvePluginsMojo.java 1451088 2013-02-28 04:22:41Z brianf $
51   * @since 2.0
52   */
53  @Mojo( name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
54  public class ResolvePluginsMojo
55      extends AbstractResolveMojo
56  {
57  
58      /**
59       * Remote repositories which will be searched for plugins.
60       */
61      @Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true )
62      private List<ArtifactRepository> remotePluginRepositories;
63  
64      /**
65       * If we should exclude transitive dependencies
66       */
67      @Parameter( property = "excludeTransitive", defaultValue = "false" )
68      private boolean excludeTransitive;
69  
70      /**
71       * Main entry into mojo. Gets the list of dependencies and iterates through
72       * displaying the resolved version.
73       *
74       * @throws MojoExecutionException with a message if an error occurs.
75       */
76      protected void doExecute()
77          throws MojoExecutionException
78      {
79          Writer outputWriter = null;
80  
81          try
82          {
83              final Set<Artifact> plugins = resolvePluginArtifacts();
84  
85              if ( this.outputFile != null )
86              {
87                  outputFile.getParentFile()
88                            .mkdirs();
89  
90                  outputWriter = new FileWriter( outputFile );
91              }
92  
93              for ( final Artifact plugin : plugins )
94              {
95                  String logStr = "Plugin Resolved: " + DependencyUtil.getFormattedFileName( plugin, false );
96                  if ( !silent )
97                  {
98                      this.getLog()
99                          .info( logStr );
100                 }
101 
102                 if ( outputWriter != null )
103                 {
104                     outputWriter.write( logStr );
105                     outputWriter.write( "\n" );
106                 }
107 
108                 if ( !excludeTransitive )
109                 {
110                     for ( final Artifact artifact : resolveArtifactDependencies( plugin ) )
111                     {
112                         logStr =
113                             "    Plugin Dependency Resolved: " + DependencyUtil.getFormattedFileName( artifact, false );
114 
115                         if ( !silent )
116                         {
117                             this.getLog()
118                                 .info( logStr );
119                         }
120 
121                         if ( outputWriter != null )
122                         {
123                             outputWriter.write( logStr );
124                             outputWriter.write( "\n" );
125                         }
126                     }
127                 }
128             }
129         }
130         catch ( final IOException e )
131         {
132             throw new MojoExecutionException( "Nested:", e );
133         }
134         catch ( final ArtifactResolutionException e )
135         {
136             throw new MojoExecutionException( "Nested:", e );
137         }
138         catch ( final ArtifactNotFoundException e )
139         {
140             throw new MojoExecutionException( "Nested:", e );
141         }
142         catch ( final ProjectBuildingException e )
143         {
144             throw new MojoExecutionException( "Nested:", e );
145         }
146         catch ( final InvalidDependencyVersionException e )
147         {
148             throw new MojoExecutionException( "Nested:", e );
149         }
150         catch ( final ArtifactFilterException e )
151         {
152             throw new MojoExecutionException( "Nested:", e );
153         }
154         finally
155         {
156             IOUtil.close( outputWriter );
157         }
158 
159     }
160 
161     /**
162      * This method resolves the plugin artifacts from the project.
163      *
164      * @return set of resolved plugin artifacts.
165      * @throws ArtifactResolutionException
166      * @throws ArtifactNotFoundException
167      * @throws ArtifactFilterException 
168      */
169     @SuppressWarnings( "unchecked" )
170     protected Set<Artifact> resolvePluginArtifacts()
171         throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactFilterException
172     {
173         final Set<Artifact> plugins = project.getPluginArtifacts();
174         final Set<Artifact> reports = project.getReportArtifacts();
175 
176         Set<Artifact> artifacts = new HashSet<Artifact>();
177         artifacts.addAll( reports );
178         artifacts.addAll( plugins );
179 
180         final FilterArtifacts filter = getPluginArtifactsFilter();
181         artifacts = filter.filter( artifacts );
182 
183         //        final ArtifactFilter filter = getPluginFilter();
184         for ( final Artifact artifact : new HashSet<Artifact>( artifacts ) )
185         {
186             //            if ( !filter.include( artifact ) )
187             //            {
188             //                final String logStr =
189             //                    String.format( "    Plugin SKIPPED: %s", DependencyUtil.getFormattedFileName( artifact, false ) );
190             //
191             //                if ( !silent )
192             //                {
193             //                    this.getLog()
194             //                        .info( logStr );
195             //                }
196             //
197             //                artifacts.remove( artifact );
198             //                continue;
199             //            }
200 
201             // resolve the new artifact
202             this.resolver.resolve( artifact, this.remotePluginRepositories, this.getLocal() );
203         }
204         return artifacts;
205     }
206 
207     @Override
208     protected ArtifactsFilter getMarkedArtifactFilter()
209     {
210         return null;
211     }
212 }