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.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
29  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.dependency.AbstractResolveMojo;
32  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
33  import org.apache.maven.project.ProjectBuildingException;
34  import org.apache.maven.project.artifact.InvalidDependencyVersionException;
35  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
36  
37  /**
38   * Goal that resolves all project plugins and reports and their dependencies.
39   * 
40   * @goal resolve-plugins
41   * @phase generate-sources
42   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
43   * @version $Id: ResolvePluginsMojo.java 728546 2008-12-21 22:56:51Z bentmann $
44   * @since 2.0
45   */
46  public class ResolvePluginsMojo
47      extends AbstractResolveMojo
48  {
49  
50      /**
51       * Remote repositories which will be searched for plugins.
52       * 
53       * @parameter expression="${project.pluginArtifactRepositories}"
54       * @required
55       * @readonly
56       */
57      private List remotePluginRepositories;
58  
59      /**
60       * If we should exclude transitive dependencies
61       * 
62       * @parameter expression="${excludeTransitive}" default-value="false"
63       */
64      private boolean excludeTransitive;
65  
66      /**
67       * Main entry into mojo. Gets the list of dependencies and iterates through
68       * displaying the resolved version.
69       * 
70       * @throws MojoExecutionException
71       *             with a message if an error occurs.
72       * 
73       */
74      public void execute()
75          throws MojoExecutionException
76      {
77          try
78          {
79              Set plugins = resolvePluginArtifacts();
80              for ( Iterator i = plugins.iterator(); i.hasNext(); )
81              {
82                  Artifact plugin = (Artifact) i.next();
83                  if ( !silent )
84                  {
85                      this.getLog().info( "Plugin Resolved: " + DependencyUtil.getFormattedFileName( plugin, false ) );
86                  }
87                  if ( !excludeTransitive )
88                  {
89                      Set transitiveDependencies = this.resolveArtifactDependencies( plugin );
90                      if ( !silent )
91                      {
92                          for ( Iterator transIter = transitiveDependencies.iterator(); transIter.hasNext(); )
93                          {
94                              this.getLog().info(
95                                                  "    Plugin Dependency Resolved: "
96                                                      + DependencyUtil.getFormattedFileName( (Artifact) transIter.next(),
97                                                                                             false ) );
98                          }
99                      }
100                 }
101             }
102         }
103         catch ( ArtifactResolutionException e )
104         {
105             throw new MojoExecutionException( "Nested:", e );
106         }
107         catch ( ArtifactNotFoundException e )
108         {
109             throw new MojoExecutionException( "Nested:", e );
110         }
111         catch ( ProjectBuildingException e )
112         {
113             throw new MojoExecutionException( "Nested:", e );
114         }
115         catch ( InvalidDependencyVersionException e )
116         {
117             throw new MojoExecutionException( "Nested:", e );
118         }
119 
120     }
121 
122     /**
123      * This method resolves the plugin artifacts from the project.
124      * 
125      * @param project
126      *            The POM.
127      * @param artifactFactory
128      *            component to build artifact objects.
129      * @param localRepository
130      *            where to resolve artifacts.
131      * @param remotePluginRepositories
132      *            list of remote repositories used to resolve plugins.
133      * @param artifactResolver
134      *            component used to resolve artifacts.
135      * 
136      * @return set of resolved plugin artifacts.
137      * 
138      * @throws ArtifactResolutionException
139      * @throws ArtifactNotFoundException
140      */
141     protected Set resolvePluginArtifacts()
142         throws ArtifactResolutionException, ArtifactNotFoundException
143     {
144         Set plugins = project.getPluginArtifacts();
145         Set reports = project.getReportArtifacts();
146 
147         Set artifacts = new HashSet();
148         artifacts.addAll( reports );
149         artifacts.addAll( plugins );
150 
151         for ( Iterator i = artifacts.iterator(); i.hasNext(); )
152         {
153             Artifact artifact = (Artifact) i.next();
154             // resolve the new artifact
155             this.resolver.resolve( artifact, this.remotePluginRepositories, this.local );
156         }
157         return artifacts;
158     }
159 
160     protected ArtifactsFilter getMarkedArtifactFilter()
161     {
162         // TODO Auto-generated method stub
163         return null;
164     }
165 }