View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.shade.mojo;
20  
21  import java.util.Collection;
22  import java.util.HashSet;
23  
24  import org.apache.maven.artifact.Artifact;
25  
26  /**
27   * @author Benjamin Bentmann
28   */
29  class ArtifactSelector {
30  
31      private Collection<ArtifactId> includes;
32  
33      private Collection<ArtifactId> excludes;
34  
35      ArtifactSelector(Artifact projectArtifact, ArtifactSet artifactSet, String groupPrefix) {
36          this(
37                  (artifactSet != null) ? artifactSet.getIncludes() : null,
38                  (artifactSet != null) ? artifactSet.getExcludes() : null,
39                  groupPrefix);
40  
41          if (projectArtifact != null && !this.includes.isEmpty()) {
42              this.includes.add(new ArtifactId(projectArtifact));
43          }
44      }
45  
46      ArtifactSelector(Collection<String> includes, Collection<String> excludes, String groupPrefix) {
47          this.includes = toIds(includes);
48          this.excludes = toIds(excludes);
49  
50          if (groupPrefix != null && groupPrefix.length() > 0) {
51              this.includes.add(new ArtifactId(groupPrefix + "*", "*", "*", "*"));
52          }
53      }
54  
55      private static Collection<ArtifactId> toIds(Collection<String> patterns) {
56          Collection<ArtifactId> result = new HashSet<>();
57  
58          if (patterns != null) {
59              for (String pattern : patterns) {
60                  result.add(new ArtifactId(pattern));
61              }
62          }
63  
64          return result;
65      }
66  
67      public boolean isSelected(Artifact artifact) {
68          return artifact != null && isSelected(new ArtifactId(artifact));
69      }
70  
71      boolean isSelected(ArtifactId id) {
72          return (includes.isEmpty() || matches(includes, id)) && !matches(excludes, id);
73      }
74  
75      private boolean matches(Collection<ArtifactId> patterns, ArtifactId id) {
76          for (ArtifactId pattern : patterns) {
77              if (id.matches(pattern)) {
78                  return true;
79              }
80          }
81          return false;
82      }
83  }