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.List;
22  import java.util.Set;
23  import java.util.stream.Collectors;
24  
25  import org.apache.maven.artifact.ArtifactUtils;
26  import org.apache.maven.model.Dependency;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.shared.artifact.filter.resolve.AbstractFilter;
30  import org.apache.maven.shared.artifact.filter.resolve.Node;
31  import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
32  
33  /**
34   * {@link TransformableFilter} implementation that excludes artifacts found in the Reactor.
35   *
36   * @author Maarten Mulders
37   */
38  public class ExcludeReactorProjectsDependencyFilter extends AbstractFilter {
39      private final Log log;
40      private final Set<String> reactorArtifactKeys;
41  
42      public ExcludeReactorProjectsDependencyFilter(final List<MavenProject> reactorProjects, final Log log) {
43          this.log = log;
44          this.reactorArtifactKeys = reactorProjects.stream()
45                  .map(project -> ArtifactUtils.key(project.getArtifact()))
46                  .collect(Collectors.toSet());
47      }
48  
49      @Override
50      public boolean accept(final Node node, final List<Node> parents) {
51          final Dependency dependency = node.getDependency();
52          if (dependency != null) {
53              final String dependencyArtifactKey =
54                      ArtifactUtils.key(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
55  
56              final boolean result = isDependencyArtifactInReactor(dependencyArtifactKey);
57  
58              if (log.isDebugEnabled() && result) {
59                  log.debug("Skipped dependency " + dependencyArtifactKey + " because it is present in the reactor");
60              }
61  
62              return !result;
63          }
64          return true;
65      }
66  
67      private boolean isDependencyArtifactInReactor(final String dependencyArtifactKey) {
68          for (final String reactorArtifactKey : this.reactorArtifactKeys) {
69              // This check only includes GAV. Should we take a look at the types, too?
70              if (reactorArtifactKey.equals(dependencyArtifactKey)) {
71                  return true;
72              }
73          }
74          return false;
75      }
76  }