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