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