DependencyDefinitionFilter.java

  1. /*
  2.  * Licensed under the Apache License, Version 2.0 (the "License");
  3.  * you may not use this file except in compliance with the License.
  4.  * You may obtain a copy of the License at
  5.  *
  6.  * http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  * Unless required by applicable law or agreed to in writing, software
  9.  * distributed under the License is distributed on an "AS IS" BASIS,
  10.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  * See the License for the specific language governing permissions and
  12.  * limitations under the License.
  13.  */

  14. package org.basepom.mojo.repack;

  15. import static com.google.common.base.Preconditions.checkNotNull;

  16. import java.util.Set;

  17. import com.google.common.collect.ImmutableSet;
  18. import org.apache.maven.artifact.Artifact;
  19. import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;

  20. final class DependencyDefinitionFilter extends AbstractArtifactsFilter {

  21.     private final Set<DependencyDefinition> dependencies;
  22.     private final boolean include;

  23.     DependencyDefinitionFilter(Set<DependencyDefinition> dependencies, boolean include) {
  24.         this.dependencies = checkNotNull(dependencies, "dependencies is null");
  25.         this.include = include;
  26.     }

  27.     @Override
  28.     public Set<Artifact> filter(Set<Artifact> artifacts) {
  29.         ImmutableSet.Builder<Artifact> builder = ImmutableSet.builder();

  30.         for (Artifact artifact : artifacts) {
  31.             for (DependencyDefinition dependency : dependencies) {
  32.                 // inclusion filter and match -> included
  33.                 // exclusion filter and no match -> included
  34.                 if (include == dependency.matches(artifact)) {
  35.                     builder.add(artifact);
  36.                     break; // only add it once.
  37.                 } else {
  38.                     Reporter.addExcluded(artifact, include ? "included" : "excluded");
  39.                 }
  40.             }
  41.         }
  42.         return builder.build();
  43.     }
  44. }