View Javadoc
1   package org.apache.maven.plugins.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.repository.ArtifactRepository;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
25  import org.apache.maven.plugins.annotations.Component;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  import org.apache.maven.shared.dependencies.collect.CollectorResult;
29  import org.apache.maven.shared.dependencies.collect.DependencyCollector;
30  import org.apache.maven.shared.dependencies.collect.DependencyCollectorException;
31  
32  /**
33   * Goal that resolves all project dependencies and then lists the repositories used by the build and by the transitive
34   * dependencies
35   *
36   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
37   * @since 2.2
38   */
39  @Mojo( name = "list-repositories", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
40  public class ListRepositoriesMojo
41      extends AbstractDependencyMojo
42  {
43      /**
44       * Dependency collector, needed to resolve dependencies.
45       */
46      @Component( role = DependencyCollector.class )
47      private DependencyCollector dependencyCollector;
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      @Override
55      protected void doExecute()
56          throws MojoExecutionException
57      {
58          try
59          {
60              CollectorResult collectResult =
61                  dependencyCollector.collectDependencies( session.getProjectBuildingRequest(), getProject().getModel() );
62  
63              this.getLog().info( "Repositories used by this build:" );
64  
65              for ( ArtifactRepository repo : collectResult.getRemoteRepositories() )
66              {
67                  this.getLog().info( repo.toString() );
68              }
69          }
70          catch ( DependencyCollectorException e )
71          {
72              throw new MojoExecutionException( "Unable to resolve artifacts", e );
73          }
74      }
75  
76  }