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.HashSet;
31  import java.util.List;
32  import java.util.Set;
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 = new HashSet<>( reactorProjects.size() );
48          for ( final MavenProject project : reactorProjects )
49          {
50              this.reactorArtifactKeys.add( ArtifactUtils.key( project.getArtifact() ) );
51          }
52      }
53  
54      @Override
55      public boolean accept( final Node node, final List<Node> parents )
56      {
57          final Dependency dependency = node.getDependency();
58          if ( dependency != null )
59          {
60              final String dependencyArtifactKey = ArtifactUtils.key(
61                      dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
62  
63              final boolean result = isDependencyArtifactInReactor( dependencyArtifactKey );
64  
65              if ( log.isDebugEnabled() && result )
66              {
67                  log.debug( "Skipped dependency "
68                          + dependencyArtifactKey
69                          + " because it is present in the reactor" );
70              }
71  
72              return !result;
73          }
74          return true;
75      }
76  
77      private boolean isDependencyArtifactInReactor( final String dependencyArtifactKey )
78      {
79          for ( final String reactorArtifactKey : this.reactorArtifactKeys )
80          {
81              // This check only includes GAV. Should we take a look at the types, too?
82              if ( reactorArtifactKey.equals( dependencyArtifactKey ) )
83              {
84                  return true;
85              }
86          }
87          return false;
88      }
89  }