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  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.ArtifactUtils;
28  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;
32  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
33  
34  /**
35   * {@link ArtifactFilter} implementation that excludes artifacts found in the Reactor.
36   *
37   * @author Maarten Mulders
38   */
39  public class ExcludeReactorProjectsArtifactFilter extends AbstractArtifactsFilter {
40      private final Log log;
41      private final Set<String> reactorArtifactKeys;
42  
43      public ExcludeReactorProjectsArtifactFilter(final List<MavenProject> reactorProjects, final Log log) {
44          this.log = log;
45          this.reactorArtifactKeys = new HashSet<>(reactorProjects.size());
46          for (final MavenProject project : reactorProjects) {
47              this.reactorArtifactKeys.add(ArtifactUtils.key(project.getArtifact()));
48          }
49      }
50  
51      @Override
52      public Set<Artifact> filter(final Set<Artifact> artifacts) throws ArtifactFilterException {
53          final Set<Artifact> results = new LinkedHashSet<>(artifacts.size());
54  
55          for (final Artifact artifact : artifacts) {
56              if (!isArtifactInReactor(artifact)) {
57                  results.add(artifact);
58              } else {
59                  if (log.isDebugEnabled()) {
60                      log.debug("Skipped artifact "
61                              + ArtifactUtils.key(artifact)
62                              + " because it is present in the reactor");
63                  }
64              }
65          }
66  
67          return results;
68      }
69  
70      private boolean isArtifactInReactor(final Artifact artifact) {
71          for (final String reactorArtifactKey : this.reactorArtifactKeys) {
72              final String artifactKey = ArtifactUtils.key(artifact);
73  
74              // This check only includes GAV. Should we take a look at the types, too?
75              if (reactorArtifactKey.equals(artifactKey)) {
76                  return true;
77              }
78          }
79          return false;
80      }
81  }