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