View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.resolvers;
20  
21  import java.util.HashSet;
22  import java.util.LinkedHashSet;
23  import java.util.List;
24  import java.util.Set;
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.ArtifactUtils;
27  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
28  import org.apache.maven.plugin.logging.Log;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;
31  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
32  
33  /**
34   * {@link ArtifactFilter} implementation that excludes artifacts found in the Reactor.
35   *
36   * @author Maarten Mulders
37   */
38  public class ExcludeReactorProjectsArtifactFilter extends AbstractArtifactsFilter {
39      private final Log log;
40      private final Set<String> reactorArtifactKeys;
41  
42      public ExcludeReactorProjectsArtifactFilter(final List<MavenProject> reactorProjects, final Log log) {
43          this.log = log;
44          this.reactorArtifactKeys = new HashSet<>(reactorProjects.size());
45          for (final MavenProject project : reactorProjects) {
46              this.reactorArtifactKeys.add(ArtifactUtils.key(project.getArtifact()));
47          }
48      }
49  
50      @Override
51      public Set<Artifact> filter(final Set<Artifact> artifacts) throws ArtifactFilterException {
52          final Set<Artifact> results = new LinkedHashSet<>(artifacts.size());
53  
54          for (final Artifact artifact : artifacts) {
55              if (!isArtifactInReactor(artifact)) {
56                  results.add(artifact);
57              } else {
58                  if (log.isDebugEnabled()) {
59                      log.debug("Skipped artifact "
60                              + ArtifactUtils.key(artifact)
61                              + " because it is present in the reactor");
62                  }
63              }
64          }
65  
66          return results;
67      }
68  
69      private boolean isArtifactInReactor(final Artifact artifact) {
70          for (final String reactorArtifactKey : this.reactorArtifactKeys) {
71              final String artifactKey = ArtifactUtils.key(artifact);
72  
73              // This check only includes GAV. Should we take a look at the types, too?
74              if (reactorArtifactKey.equals(artifactKey)) {
75                  return true;
76              }
77          }
78          return false;
79      }
80  }