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