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