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.ArrayList;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
29  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
30  import org.apache.maven.artifact.resolver.ResolutionNode;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.dependency.AbstractDependencyMojo;
33  import org.apache.maven.shared.artifact.filter.ScopeArtifactFilter;
34  
35  /**
36   * Goal that resolves all project dependencies and then lists the repositories
37   * used by the build and by the transitive dependencies
38   *
39   * @goal list-repositories
40   * @requiresDependencyResolution test
41   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
42   * @version $Id: GoOfflineMojo.java 728546 2008-12-21 22:56:51Z bentmann $
43   * @since 2.2
44   */
45  public class ListRepositoriesMojo
46      extends AbstractDependencyMojo
47  {
48  	/**
49       * Displays a list of the repositories used by this build.
50  	 * @throws MojoExecutionException
51  	 *             with a message if an error occurs.
52  	 *
53  	 */
54      public void execute()
55          throws MojoExecutionException
56  	{
57  		try
58  		{
59              ArtifactResolutionResult result =
60                  this.artifactCollector.collect( project.getArtifacts(), project.getArtifact(), this.getLocal(),
61                                                  this.remoteRepos, this.artifactMetadataSource,
62                                                  new ScopeArtifactFilter( Artifact.SCOPE_TEST ), new ArrayList() );
63  			Set repos = new HashSet();
64  			Set<ResolutionNode> nodes = result.getArtifactResolutionNodes();
65              for ( ResolutionNode node : nodes )
66  			{
67                  repos.addAll( node.getRemoteRepositories() );
68  			}
69  
70              this.getLog().info( "Repositories Used by this build:" );
71              for ( Iterator i = repos.iterator(); i.hasNext(); )
72              {
73                  this.getLog().info( i.next().toString() );
74              }
75          }
76          catch ( ArtifactResolutionException e )
77          {
78              throw new MojoExecutionException( "Unable to resolve artifacts", e );
79          }
80  	}
81  }