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.Artifact;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.model.Dependency;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.project.ProjectBuildingRequest;
29  import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
30  import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
31  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
32  import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
33  import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
34  import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
35  
36  import java.util.Collection;
37  import java.util.HashSet;
38  import java.util.LinkedHashSet;
39  import java.util.Set;
40  import java.util.stream.Collectors;
41  
42  /**
43   * Goal that resolves all project dependencies, including plugins and reports and their dependencies.
44   *
45   * <a href="mailto:brianf@apache.org">Brian Fox</a>
46   * @author Maarten Mulders
47   * @since 2.0
48   */
49  @Mojo( name = "go-offline", threadSafe = true )
50  public class GoOfflineMojo
51      extends AbstractResolveMojo
52  {
53      /**
54       * Main entry into mojo. Gets the list of dependencies, resolves all that are not in the Reactor, and iterates
55       * through displaying the resolved versions.
56       *
57       * @throws MojoExecutionException with a message if an error occurs.
58       */
59      @Override
60      protected void doExecute()
61          throws MojoExecutionException
62      {
63  
64          try
65          {
66              final Set<Artifact> plugins = resolvePluginArtifacts();
67  
68              final Set<Artifact> dependencies = resolveDependencyArtifacts();
69  
70              if ( !isSilent() )
71              {
72                  for ( Artifact artifact : plugins )
73                  {
74                      this.getLog().info( "Resolved plugin: "
75                              + DependencyUtil.getFormattedFileName( artifact, false ) );
76                  }
77  
78                  for ( Artifact artifact : dependencies )
79                  {
80                      this.getLog().info( "Resolved dependency: "
81                              + DependencyUtil.getFormattedFileName( artifact, false ) );
82                  }
83              }
84  
85          }
86          catch ( DependencyResolverException e )
87          {
88              throw new MojoExecutionException( e.getMessage(), e );
89          }
90  
91      }
92  
93      /**
94       * This method resolves the dependency artifacts from the project.
95       *
96       * @return set of resolved dependency artifacts.
97       * @throws DependencyResolverException in case of an error while resolving the artifacts.
98       */
99      protected Set<Artifact> resolveDependencyArtifacts()
100             throws DependencyResolverException
101     {
102         Collection<Dependency> dependencies = getProject().getDependencies();
103 
104         Set<DependableCoordinate> dependableCoordinates = dependencies.stream()
105                 .map( this::createDependendableCoordinateFromDependency )
106                 .collect( Collectors.toSet() );
107 
108         ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
109 
110         return resolveDependableCoordinate( buildingRequest, dependableCoordinates, "dependencies" );
111     }
112 
113     private Set<Artifact> resolveDependableCoordinate( final ProjectBuildingRequest buildingRequest,
114                                                         final Collection<DependableCoordinate> dependableCoordinates,
115                                                        final String type )
116             throws DependencyResolverException
117     {
118         final TransformableFilter filter = getTransformableFilter();
119 
120         this.getLog().debug( "Resolving '" + type + "' with following repositories:" );
121         for ( ArtifactRepository repo : buildingRequest.getRemoteRepositories() )
122         {
123             getLog().debug( repo.getId() + " (" + repo.getUrl() + ")" );
124         }
125 
126         final Set<Artifact> results = new HashSet<>();
127 
128         for ( DependableCoordinate dependableCoordinate : dependableCoordinates )
129         {
130             final Iterable<ArtifactResult> artifactResults = getDependencyResolver().resolveDependencies(
131                     buildingRequest, dependableCoordinate, filter );
132 
133             for ( final ArtifactResult artifactResult : artifactResults )
134             {
135                 results.add( artifactResult.getArtifact() );
136             }
137         }
138 
139         return results;
140     }
141 
142     private TransformableFilter getTransformableFilter()
143     {
144         if ( this.excludeReactor )
145         {
146             return new ExcludeReactorProjectsDependencyFilter( this.reactorProjects, getLog() );
147         }
148         else
149         {
150             return null;
151         }
152     }
153 
154     /**
155      * This method resolves the plugin artifacts from the project.
156      *
157      * @return set of resolved plugin artifacts.
158      * @throws DependencyResolverException in case of an error while resolving the artifacts.
159      */
160     protected Set<Artifact> resolvePluginArtifacts()
161             throws DependencyResolverException
162     {
163 
164         Set<Artifact> plugins = getProject().getPluginArtifacts();
165         Set<Artifact> reports = getProject().getReportArtifacts();
166 
167         Set<Artifact> artifacts = new LinkedHashSet<>();
168         artifacts.addAll( reports );
169         artifacts.addAll( plugins );
170 
171         Set<DependableCoordinate> dependableCoordinates = artifacts.stream()
172                 .map( this::createDependendableCoordinateFromArtifact )
173                 .collect( Collectors.toSet() );
174 
175 
176         ProjectBuildingRequest buildingRequest = newResolvePluginProjectBuildingRequest();
177 
178         return resolveDependableCoordinate( buildingRequest, dependableCoordinates, "plugins" );
179     }
180 
181     private DependableCoordinate createDependendableCoordinateFromArtifact( final Artifact artifact )
182     {
183         final DefaultDependableCoordinate result = new DefaultDependableCoordinate();
184         result.setGroupId( artifact.getGroupId() );
185         result.setArtifactId( artifact.getArtifactId() );
186         result.setVersion( artifact.getVersion() );
187         result.setType( artifact.getType() );
188         result.setClassifier( artifact.getClassifier() );
189 
190         return result;
191     }
192 
193     private DependableCoordinate createDependendableCoordinateFromDependency( final Dependency dependency )
194     {
195         final DefaultDependableCoordinate result = new DefaultDependableCoordinate();
196         result.setGroupId( dependency.getGroupId() );
197         result.setArtifactId( dependency.getArtifactId() );
198         result.setVersion( dependency.getVersion() );
199         result.setType( dependency.getType() );
200         result.setClassifier( dependency.getClassifier() );
201 
202         return result;
203     }
204 
205     @Override
206     protected ArtifactsFilter getMarkedArtifactFilter()
207     {
208         return null;
209     }
210 }