1 package org.apache.maven.plugins.dependency.resolvers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
44
45
46
47
48
49 @Mojo( name = "go-offline", threadSafe = true )
50 public class GoOfflineMojo
51 extends AbstractResolveMojo
52 {
53
54
55
56
57
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
95
96
97
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
156
157
158
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 }