2525import java .util .stream .Collectors ;
2626
2727import org .apache .maven .artifact .Artifact ;
28+ import org .apache .maven .artifact .DefaultArtifact ;
29+ import org .apache .maven .artifact .handler .DefaultArtifactHandler ;
2830import org .apache .maven .artifact .repository .ArtifactRepository ;
2931import org .apache .maven .model .Dependency ;
3032import org .apache .maven .plugin .MojoExecutionException ;
3133import org .apache .maven .plugins .annotations .Mojo ;
3234import org .apache .maven .plugins .dependency .utils .DependencyUtil ;
3335import org .apache .maven .project .ProjectBuildingRequest ;
36+ import org .apache .maven .shared .artifact .filter .collection .ArtifactFilterException ;
37+ import org .apache .maven .shared .artifact .filter .collection .ArtifactIdFilter ;
3438import org .apache .maven .shared .artifact .filter .collection .ArtifactsFilter ;
39+ import org .apache .maven .shared .artifact .filter .collection .ClassifierFilter ;
40+ import org .apache .maven .shared .artifact .filter .collection .FilterArtifacts ;
41+ import org .apache .maven .shared .artifact .filter .collection .GroupIdFilter ;
42+ import org .apache .maven .shared .artifact .filter .collection .ScopeFilter ;
43+ import org .apache .maven .shared .artifact .filter .collection .TypeFilter ;
3544import org .apache .maven .shared .artifact .filter .resolve .TransformableFilter ;
3645import org .apache .maven .shared .transfer .artifact .resolve .ArtifactResult ;
3746import org .apache .maven .shared .transfer .dependencies .DefaultDependableCoordinate ;
4352 *
4453 * @author <a href="mailto:[email protected] ">Brian Fox</a> 4554 * @author Maarten Mulders
55+ * @author Lisa Hardy
4656 * @since 2.0
4757 */
4858@ Mojo (name = "go-offline" , threadSafe = true )
@@ -71,7 +81,7 @@ protected void doExecute() throws MojoExecutionException {
7181 }
7282 }
7383
74- } catch (DependencyResolverException e ) {
84+ } catch (DependencyResolverException | ArtifactFilterException e ) {
7585 throw new MojoExecutionException (e .getMessage (), e );
7686 }
7787 }
@@ -81,10 +91,13 @@ protected void doExecute() throws MojoExecutionException {
8191 *
8292 * @return set of resolved dependency artifacts.
8393 * @throws DependencyResolverException in case of an error while resolving the artifacts.
94+ * @throws ArtifactFilterException
8495 */
85- protected Set <Artifact > resolveDependencyArtifacts () throws DependencyResolverException {
96+ protected Set <Artifact > resolveDependencyArtifacts () throws DependencyResolverException , ArtifactFilterException {
8697 Collection <Dependency > dependencies = getProject ().getDependencies ();
8798
99+ dependencies = filterDependencies (dependencies );
100+
88101 Set <DependableCoordinate > dependableCoordinates = dependencies .stream ()
89102 .map (this ::createDependendableCoordinateFromDependency )
90103 .collect (Collectors .toSet ());
@@ -133,8 +146,9 @@ private TransformableFilter getTransformableFilter() {
133146 *
134147 * @return set of resolved plugin artifacts.
135148 * @throws DependencyResolverException in case of an error while resolving the artifacts.
149+ * @throws ArtifactFilterException
136150 */
137- protected Set <Artifact > resolvePluginArtifacts () throws DependencyResolverException {
151+ protected Set <Artifact > resolvePluginArtifacts () throws DependencyResolverException , ArtifactFilterException {
138152
139153 Set <Artifact > plugins = getProject ().getPluginArtifacts ();
140154 Set <Artifact > reports = getProject ().getReportArtifacts ();
@@ -143,6 +157,9 @@ protected Set<Artifact> resolvePluginArtifacts() throws DependencyResolverExcept
143157 artifacts .addAll (reports );
144158 artifacts .addAll (plugins );
145159
160+ final FilterArtifacts filter = getArtifactsFilter ();
161+ artifacts = filter .filter (artifacts );
162+
146163 Set <DependableCoordinate > dependableCoordinates = artifacts .stream ()
147164 .map (this ::createDependendableCoordinateFromArtifact )
148165 .collect (Collectors .toSet ());
@@ -152,6 +169,46 @@ protected Set<Artifact> resolvePluginArtifacts() throws DependencyResolverExcept
152169 return resolveDependableCoordinate (buildingRequest , dependableCoordinates , "plugins" );
153170 }
154171
172+ protected FilterArtifacts getArtifactsFilter () {
173+ final FilterArtifacts filter = new FilterArtifacts ();
174+
175+ if (excludeReactor ) {
176+ filter .addFilter (new ExcludeReactorProjectsArtifactFilter (reactorProjects , getLog ()));
177+ }
178+
179+ filter .addFilter (new ScopeFilter (
180+ DependencyUtil .cleanToBeTokenizedString (this .includeScope ),
181+ DependencyUtil .cleanToBeTokenizedString (this .excludeScope )));
182+
183+ filter .addFilter (new TypeFilter (
184+ DependencyUtil .cleanToBeTokenizedString (this .includeTypes ),
185+ DependencyUtil .cleanToBeTokenizedString (this .excludeTypes )));
186+
187+ filter .addFilter (new ClassifierFilter (
188+ DependencyUtil .cleanToBeTokenizedString (this .includeClassifiers ),
189+ DependencyUtil .cleanToBeTokenizedString (this .excludeClassifiers )));
190+
191+ filter .addFilter (new GroupIdFilter (
192+ DependencyUtil .cleanToBeTokenizedString (this .includeGroupIds ),
193+ DependencyUtil .cleanToBeTokenizedString (this .excludeGroupIds )));
194+
195+ filter .addFilter (new ArtifactIdFilter (
196+ DependencyUtil .cleanToBeTokenizedString (this .includeArtifactIds ),
197+ DependencyUtil .cleanToBeTokenizedString (this .excludeArtifactIds )));
198+
199+ return filter ;
200+ }
201+
202+ private Collection <Dependency > filterDependencies (Collection <Dependency > deps ) throws ArtifactFilterException {
203+
204+ Set <Artifact > artifacts = createArtifactSetFromDependencies (deps );
205+
206+ final FilterArtifacts filter = getArtifactsFilter ();
207+ artifacts = filter .filter (artifacts );
208+
209+ return createDependencySetFromArtifacts (artifacts );
210+ }
211+
155212 private DependableCoordinate createDependendableCoordinateFromArtifact (final Artifact artifact ) {
156213 final DefaultDependableCoordinate result = new DefaultDependableCoordinate ();
157214 result .setGroupId (artifact .getGroupId ());
@@ -174,6 +231,39 @@ private DependableCoordinate createDependendableCoordinateFromDependency(final D
174231 return result ;
175232 }
176233
234+ private Set <Artifact > createArtifactSetFromDependencies (Collection <Dependency > deps ) {
235+ Set <Artifact > artifacts = new HashSet <>();
236+ for (Dependency dep : deps ) {
237+ DefaultArtifactHandler handler = new DefaultArtifactHandler (dep .getType ());
238+ artifacts .add (new DefaultArtifact (
239+ dep .getGroupId (),
240+ dep .getArtifactId (),
241+ dep .getVersion (),
242+ dep .getScope (),
243+ dep .getType (),
244+ dep .getClassifier (),
245+ handler ));
246+ }
247+ return artifacts ;
248+ }
249+
250+ private Collection <Dependency > createDependencySetFromArtifacts (Set <Artifact > artifacts ) {
251+ Set <Dependency > dependencies = new HashSet <>();
252+
253+ for (Artifact artifact : artifacts ) {
254+ Dependency d = new Dependency ();
255+ d .setGroupId (artifact .getGroupId ());
256+ d .setArtifactId (artifact .getArtifactId ());
257+ d .setVersion (artifact .getVersion ());
258+ d .setType (artifact .getType ());
259+ d .setClassifier (artifact .getClassifier ());
260+ d .setScope (artifact .getScope ());
261+ dependencies .add (d );
262+ }
263+
264+ return dependencies ;
265+ }
266+
177267 @ Override
178268 protected ArtifactsFilter getMarkedArtifactFilter () {
179269 return null ;
0 commit comments