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