20
20
21
21
import javax .inject .Inject ;
22
22
23
+ import java .util .Arrays ;
23
24
import java .util .Collection ;
24
25
import java .util .HashSet ;
25
- import java .util .LinkedHashSet ;
26
+ import java .util .List ;
26
27
import java .util .Set ;
28
+ import java .util .function .Predicate ;
27
29
import java .util .stream .Collectors ;
28
30
31
+ import org .apache .maven .RepositoryUtils ;
29
32
import org .apache .maven .artifact .Artifact ;
30
33
import org .apache .maven .artifact .DefaultArtifact ;
31
34
import org .apache .maven .artifact .handler .DefaultArtifactHandler ;
32
35
import org .apache .maven .artifact .handler .manager .ArtifactHandlerManager ;
33
36
import org .apache .maven .artifact .repository .ArtifactRepository ;
34
37
import org .apache .maven .execution .MavenSession ;
35
38
import org .apache .maven .model .Dependency ;
39
+ import org .apache .maven .model .Plugin ;
36
40
import org .apache .maven .plugin .MojoExecutionException ;
37
41
import org .apache .maven .plugins .annotations .Mojo ;
42
+ import org .apache .maven .plugins .annotations .Parameter ;
43
+ import org .apache .maven .plugins .dependency .fromDependencies .AbstractDependencyFilterMojo ;
38
44
import org .apache .maven .plugins .dependency .utils .DependencyUtil ;
39
45
import org .apache .maven .plugins .dependency .utils .ResolverUtil ;
46
+ import org .apache .maven .project .DefaultProjectBuildingRequest ;
40
47
import org .apache .maven .project .MavenProject ;
41
48
import org .apache .maven .project .ProjectBuilder ;
42
49
import org .apache .maven .project .ProjectBuildingRequest ;
43
50
import org .apache .maven .shared .artifact .filter .collection .ArtifactFilterException ;
51
+ import org .apache .maven .shared .artifact .filter .collection .ArtifactIdFilter ;
44
52
import org .apache .maven .shared .artifact .filter .collection .ArtifactsFilter ;
53
+ import org .apache .maven .shared .artifact .filter .collection .ClassifierFilter ;
45
54
import org .apache .maven .shared .artifact .filter .collection .FilterArtifacts ;
55
+ import org .apache .maven .shared .artifact .filter .collection .GroupIdFilter ;
56
+ import org .apache .maven .shared .artifact .filter .collection .ScopeFilter ;
57
+ import org .apache .maven .shared .artifact .filter .collection .TypeFilter ;
46
58
import org .apache .maven .shared .artifact .filter .resolve .TransformableFilter ;
47
59
import org .apache .maven .shared .transfer .artifact .resolve .ArtifactResult ;
48
60
import org .apache .maven .shared .transfer .dependencies .DefaultDependableCoordinate ;
49
61
import org .apache .maven .shared .transfer .dependencies .DependableCoordinate ;
50
62
import org .apache .maven .shared .transfer .dependencies .resolve .DependencyResolver ;
51
63
import org .apache .maven .shared .transfer .dependencies .resolve .DependencyResolverException ;
64
+ import org .eclipse .aether .resolution .ArtifactResolutionException ;
65
+ import org .eclipse .aether .resolution .DependencyResolutionException ;
52
66
import org .sonatype .plexus .build .incremental .BuildContext ;
53
67
54
68
/**
60
74
* @since 2.0
61
75
*/
62
76
@ Mojo (name = "go-offline" , threadSafe = true )
63
- public class GoOfflineMojo extends AbstractResolveMojo {
77
+ public class GoOfflineMojo extends AbstractDependencyFilterMojo {
78
+
79
+ /**
80
+ * Remote repositories which will be searched for artifacts.
81
+ */
82
+ @ Parameter (defaultValue = "${project.remoteArtifactRepositories}" , readonly = true , required = true )
83
+ private List <ArtifactRepository > remoteRepositories ;
84
+
85
+ /**
86
+ * Don't resolve plugins and artifacts that are in the current reactor.
87
+ *
88
+ * @since 2.7
89
+ */
90
+ @ Parameter (property = "excludeReactor" , defaultValue = "true" )
91
+ protected boolean excludeReactor ;
64
92
65
93
private final DependencyResolver dependencyResolver ;
66
94
@@ -89,25 +117,47 @@ public GoOfflineMojo(
89
117
protected void doExecute () throws MojoExecutionException {
90
118
91
119
try {
92
- final Set <Artifact > plugins = resolvePluginArtifacts ();
120
+ final Set <Plugin > plugins = getProjectPlugins ();
121
+
122
+ for (Plugin plugin : plugins ) {
123
+ org .eclipse .aether .artifact .Artifact artifact =
124
+ getResolverUtil ().resolvePlugin (plugin );
125
+
126
+ logMessage ("Resolved plugin: "
127
+ + DependencyUtil .getFormattedFileName (RepositoryUtils .toArtifact (artifact ), false ));
128
+ if (!excludeTransitive ) {
129
+ logMessage ("Resolved plugin dependency:" );
130
+ List <org .eclipse .aether .artifact .Artifact > artifacts =
131
+ getResolverUtil ().resolveDependencies (plugin );
132
+ for (org .eclipse .aether .artifact .Artifact a : artifacts ) {
133
+ logMessage (
134
+ " " + DependencyUtil .getFormattedFileName (RepositoryUtils .toArtifact (a ), false ));
135
+ }
136
+ }
137
+ }
93
138
94
139
final Set <Artifact > dependencies = resolveDependencyArtifacts ();
95
140
96
- if (!isSilent ()) {
97
- for (Artifact artifact : plugins ) {
98
- this .getLog ().info ("Resolved plugin: " + DependencyUtil .getFormattedFileName (artifact , false ));
99
- }
100
-
101
- for (Artifact artifact : dependencies ) {
102
- this .getLog ().info ("Resolved dependency: " + DependencyUtil .getFormattedFileName (artifact , false ));
103
- }
141
+ for (Artifact artifact : dependencies ) {
142
+ logMessage ("Resolved dependency: " + DependencyUtil .getFormattedFileName (artifact , false ));
104
143
}
105
144
106
- } catch (DependencyResolverException | ArtifactFilterException e ) {
145
+ } catch (DependencyResolverException
146
+ | ArtifactFilterException
147
+ | ArtifactResolutionException
148
+ | DependencyResolutionException e ) {
107
149
throw new MojoExecutionException (e .getMessage (), e );
108
150
}
109
151
}
110
152
153
+ private void logMessage (String message ) {
154
+ if (isSilent ()) {
155
+ getLog ().debug (message );
156
+ } else {
157
+ getLog ().info (message );
158
+ }
159
+ }
160
+
111
161
/**
112
162
* This method resolves the dependency artifacts from the project.
113
163
*
@@ -164,31 +214,30 @@ private TransformableFilter getTransformableFilter() {
164
214
}
165
215
166
216
/**
167
- * This method resolves the plugin artifacts from the project.
217
+ * This method retrieve plugins list from the project.
168
218
*
169
- * @return set of resolved plugin artifacts
170
- * @throws ArtifactFilterException
171
- * @throws DependencyResolverException in case of an error while resolving the artifacts
219
+ * @return set of plugin used in project
172
220
*/
173
- protected Set <Artifact > resolvePluginArtifacts () throws DependencyResolverException , ArtifactFilterException {
174
-
175
- Set < Artifact > plugins = getProject (). getPluginArtifacts ();
176
- Set < Artifact > reports = getProject (). getReportArtifacts ();
177
-
178
- Set < Artifact > artifacts = new LinkedHashSet <>( );
179
- artifacts . addAll ( reports );
180
- artifacts . addAll ( plugins ) ;
181
-
182
- final FilterArtifacts filter = getArtifactsFilter ( );
183
- artifacts = filter . filter ( artifacts );
221
+ private Set <Plugin > getProjectPlugins () {
222
+ Predicate < Plugin > pluginsFilter = new PluginsIncludeExcludeFilter (
223
+ toList ( includeGroupIds ),
224
+ toList ( excludeGroupIds ),
225
+ toList ( includeArtifactIds ),
226
+ toList ( excludeArtifactIds ) );
227
+
228
+ Predicate < Plugin > reactorExclusionFilter = plugin -> true ;
229
+ if ( excludeReactor ) {
230
+ reactorExclusionFilter = new PluginsReactorExcludeFilter ( session . getProjects () );
231
+ }
184
232
185
- Set <DependableCoordinate > dependableCoordinates = artifacts .stream ()
186
- .map (this ::createDependendableCoordinateFromArtifact )
233
+ return getResolverUtil ().getProjectPlugins (getProject ()).stream ()
234
+ .filter (reactorExclusionFilter )
235
+ .filter (pluginsFilter )
187
236
.collect (Collectors .toSet ());
237
+ }
188
238
189
- ProjectBuildingRequest buildingRequest = newResolvePluginProjectBuildingRequest ();
190
-
191
- return resolveDependableCoordinate (buildingRequest , dependableCoordinates , "plugins" );
239
+ private List <String > toList (String list ) {
240
+ return Arrays .asList (DependencyUtil .cleanToBeTokenizedString (list ).split ("," ));
192
241
}
193
242
194
243
private Collection <Dependency > filterDependencies (Collection <Dependency > deps ) throws ArtifactFilterException {
@@ -201,17 +250,6 @@ private Collection<Dependency> filterDependencies(Collection<Dependency> deps) t
201
250
return createDependencySetFromArtifacts (artifacts );
202
251
}
203
252
204
- private DependableCoordinate createDependendableCoordinateFromArtifact (final Artifact artifact ) {
205
- final DefaultDependableCoordinate result = new DefaultDependableCoordinate ();
206
- result .setGroupId (artifact .getGroupId ());
207
- result .setArtifactId (artifact .getArtifactId ());
208
- result .setVersion (artifact .getVersion ());
209
- result .setType (artifact .getType ());
210
- result .setClassifier (artifact .getClassifier ());
211
-
212
- return result ;
213
- }
214
-
215
253
private DependableCoordinate createDependendableCoordinateFromDependency (final Dependency dependency ) {
216
254
final DefaultDependableCoordinate result = new DefaultDependableCoordinate ();
217
255
result .setGroupId (dependency .getGroupId ());
@@ -256,6 +294,55 @@ private Collection<Dependency> createDependencySetFromArtifacts(Set<Artifact> ar
256
294
return dependencies ;
257
295
}
258
296
297
+ /**
298
+ * @return {@link FilterArtifacts}
299
+ */
300
+ protected FilterArtifacts getArtifactsFilter () {
301
+ final FilterArtifacts filter = new FilterArtifacts ();
302
+
303
+ if (excludeReactor ) {
304
+ filter .addFilter (new ExcludeReactorProjectsArtifactFilter (reactorProjects , getLog ()));
305
+ }
306
+
307
+ filter .addFilter (new ScopeFilter (
308
+ DependencyUtil .cleanToBeTokenizedString (this .includeScope ),
309
+ DependencyUtil .cleanToBeTokenizedString (this .excludeScope )));
310
+
311
+ filter .addFilter (new TypeFilter (
312
+ DependencyUtil .cleanToBeTokenizedString (this .includeTypes ),
313
+ DependencyUtil .cleanToBeTokenizedString (this .excludeTypes )));
314
+
315
+ filter .addFilter (new ClassifierFilter (
316
+ DependencyUtil .cleanToBeTokenizedString (this .includeClassifiers ),
317
+ DependencyUtil .cleanToBeTokenizedString (this .excludeClassifiers )));
318
+
319
+ filter .addFilter (new GroupIdFilter (
320
+ DependencyUtil .cleanToBeTokenizedString (this .includeGroupIds ),
321
+ DependencyUtil .cleanToBeTokenizedString (this .excludeGroupIds )));
322
+
323
+ filter .addFilter (new ArtifactIdFilter (
324
+ DependencyUtil .cleanToBeTokenizedString (this .includeArtifactIds ),
325
+ DependencyUtil .cleanToBeTokenizedString (this .excludeArtifactIds )));
326
+
327
+ return filter ;
328
+ }
329
+
330
+ /**
331
+ * @return returns a new ProjectBuildingRequest populated from the current session and the current project remote
332
+ * repositories, used to resolve artifacts
333
+ */
334
+ public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest () {
335
+ return newProjectBuildingRequest (remoteRepositories );
336
+ }
337
+
338
+ private ProjectBuildingRequest newProjectBuildingRequest (List <ArtifactRepository > repositories ) {
339
+ ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest (session .getProjectBuildingRequest ());
340
+
341
+ buildingRequest .setRemoteRepositories (repositories );
342
+
343
+ return buildingRequest ;
344
+ }
345
+
259
346
@ Override
260
347
protected ArtifactsFilter getMarkedArtifactFilter () {
261
348
return null ;
0 commit comments