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  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  }