1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.basepom.mojo.duplicatefinder.classpath;
15
16 import java.util.Collection;
17 import java.util.List;
18
19 import com.google.common.base.Predicate;
20 import com.google.common.collect.ImmutableList;
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
23 import org.basepom.mojo.duplicatefinder.artifact.MavenCoordinates;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import static java.lang.String.format;
28
29 import static com.google.common.base.Preconditions.checkNotNull;
30
31 public class MatchArtifactPredicate implements Predicate<Artifact> {
32
33 private static final Logger LOG = LoggerFactory.getLogger(MatchArtifactPredicate.class);
34
35 private final List<MavenCoordinates> mavenCoordinates;
36
37 MatchArtifactPredicate(final Collection<MavenCoordinates> dependencies) {
38 this.mavenCoordinates = ImmutableList.copyOf(checkNotNull(dependencies, "dependencies is null"));
39 }
40
41 @Override
42 public boolean apply(final Artifact artifact) {
43 if (artifact != null) {
44 for (final MavenCoordinates mavenCoordinate : mavenCoordinates) {
45 try {
46 if (mavenCoordinate.matches(artifact)) {
47 LOG.debug(format("Ignoring artifact '%s' (matches %s)", artifact, mavenCoordinate));
48 return true;
49 }
50 } catch (final OverConstrainedVersionException e) {
51 LOG.warn(format("Caught '%s' while comparing '%s' to '%s'", e.getMessage(), mavenCoordinate, artifact));
52 }
53 }
54 }
55 return false;
56 }
57 }