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.model.Dependency;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.project.DefaultProjectBuildingRequest;
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      /**
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         final Collection<Dependency> dependencies = getProject().getDependencies();
103         final Set<DependableCoordinate> dependableCoordinates = new HashSet<>();
104         final ProjectBuildingRequest buildingRequest =
105                 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
106 
107         for ( Dependency dependency : dependencies )
108         {
109             dependableCoordinates.add( createDependendableCoordinateFromDependency( dependency ) );
110         }
111 
112         return resolveDependableCoordinate( buildingRequest, dependableCoordinates );
113     }
114 
115     private Set<Artifact> resolveDependableCoordinate( final ProjectBuildingRequest buildingRequest,
116                                                         final Collection<DependableCoordinate> dependableCoordinates )
117             throws DependencyResolverException
118     {
119         final TransformableFilter filter = getTransformableFilter();
120 
121         final Set<Artifact> results = new HashSet<>();
122 
123         for ( DependableCoordinate dependableCoordinate : dependableCoordinates )
124         {
125             final Iterable<ArtifactResult> artifactResults = getDependencyResolver().resolveDependencies(
126                     buildingRequest, dependableCoordinate, filter );
127 
128             for ( final ArtifactResult artifactResult : artifactResults )
129             {
130                 results.add( artifactResult.getArtifact() );
131             }
132         }
133 
134         return results;
135     }
136 
137     private TransformableFilter getTransformableFilter()
138     {
139         if ( this.excludeReactor )
140         {
141             return new ExcludeReactorProjectsDependencyFilter( this.reactorProjects, getLog() );
142         }
143         else
144         {
145             return null;
146         }
147     }
148 
149     /**
150      * This method resolves the plugin artifacts from the project.
151      *
152      * @return set of resolved plugin artifacts.
153      * @throws DependencyResolverException in case of an error while resolving the artifacts.
154      */
155     protected Set<Artifact> resolvePluginArtifacts()
156             throws DependencyResolverException
157     {
158         final Set<DependableCoordinate> dependableCoordinates = new HashSet<>();
159 
160         final Set<Artifact> plugins = getProject().getPluginArtifacts();
161         final Set<Artifact> reports = getProject().getReportArtifacts();
162 
163         final Set<Artifact> artifacts = new LinkedHashSet<>();
164         artifacts.addAll( reports );
165         artifacts.addAll( plugins );
166 
167         final ProjectBuildingRequest buildingRequest =
168                 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
169 
170         for ( Artifact artifact : artifacts )
171         {
172             dependableCoordinates.add( createDependendableCoordinateFromArtifact( artifact ) );
173         }
174 
175         return resolveDependableCoordinate( buildingRequest, dependableCoordinates );
176     }
177 
178     private DependableCoordinate createDependendableCoordinateFromArtifact( final Artifact artifact )
179     {
180         final DefaultDependableCoordinate result = new DefaultDependableCoordinate();
181         result.setGroupId( artifact.getGroupId() );
182         result.setArtifactId( artifact.getArtifactId() );
183         result.setVersion( artifact.getVersion() );
184         result.setType( artifact.getType() );
185 
186         return result;
187     }
188 
189     private DependableCoordinate createDependendableCoordinateFromDependency( final Dependency dependency )
190     {
191         final DefaultDependableCoordinate result = new DefaultDependableCoordinate();
192         result.setGroupId( dependency.getGroupId() );
193         result.setArtifactId( dependency.getArtifactId() );
194         result.setVersion( dependency.getVersion() );
195         result.setType( dependency.getType() );
196 
197         return result;
198     }
199 
200     @Override
201     protected ArtifactsFilter getMarkedArtifactFilter()
202     {
203         return null;
204     }
205 }