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
41
42
43
44
45
46
47
48 @Mojo( name = "go-offline", threadSafe = true )
49 public class GoOfflineMojo
50 extends AbstractResolveMojo
51 {
52
53
54
55
56
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
94
95
96
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
157
158
159
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 }