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.resolver.ArtifactResolutionException;
24  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
25  import org.apache.maven.artifact.resolver.ResolutionNode;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.dependency.AbstractDependencyMojo;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.ResolutionScope;
30  import org.apache.maven.shared.artifact.filter.ScopeArtifactFilter;
31  
32  import java.util.ArrayList;
33  import java.util.HashSet;
34  import java.util.Iterator;
35  import java.util.Set;
36  
37  /**
38   * Goal that resolves all project dependencies and then lists the repositories
39   * used by the build and by the transitive dependencies
40   *
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  @Mojo( name = "list-repositories", requiresDependencyResolution = ResolutionScope.TEST )
46  public class ListRepositoriesMojo
47      extends AbstractDependencyMojo
48  {
49      /**
50       * Displays a list of the repositories used by this build.
51       *
52       * @throws MojoExecutionException with a message if an error occurs.
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  }