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 org.apache.maven.artifact.Artifact;
22  import org.apache.maven.model.Dependency;
23  import org.codehaus.plexus.util.SelectorUtils;
24  
25  /**
26   * @author Benjamin Bentmann
27   */
28  class ArtifactId {
29  
30      private final String groupId;
31  
32      private final String artifactId;
33  
34      private final String type;
35  
36      private final String classifier;
37  
38      ArtifactId(Dependency dependency) {
39          this(dependency.getGroupId(), dependency.getArtifactId(), dependency.getType(), dependency.getClassifier());
40      }
41  
42      ArtifactId(Artifact artifact) {
43          this(artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), artifact.getClassifier());
44      }
45  
46      ArtifactId(String groupId, String artifactId, String type, String classifier) {
47          this.groupId = (groupId != null) ? groupId : "";
48          this.artifactId = (artifactId != null) ? artifactId : "";
49          this.type = (type != null) ? type : "";
50          this.classifier = (classifier != null) ? classifier : "";
51      }
52  
53      ArtifactId(String id) {
54          String[] tokens = new String[0];
55          if (id != null && id.length() > 0) {
56              tokens = id.split(":", -1);
57          }
58          groupId = (tokens.length > 0) ? tokens[0] : "";
59          artifactId = (tokens.length > 1) ? tokens[1] : "*";
60          type = (tokens.length > 3) ? tokens[2] : "*";
61          classifier = (tokens.length > 3) ? tokens[3] : ((tokens.length > 2) ? tokens[2] : "*");
62      }
63  
64      public String getGroupId() {
65          return groupId;
66      }
67  
68      public String getArtifactId() {
69          return artifactId;
70      }
71  
72      public String getType() {
73          return type;
74      }
75  
76      public String getClassifier() {
77          return classifier;
78      }
79  
80      public boolean matches(ArtifactId pattern) {
81          if (pattern == null) {
82              return false;
83          }
84          if (!match(getGroupId(), pattern.getGroupId())) {
85              return false;
86          }
87          if (!match(getArtifactId(), pattern.getArtifactId())) {
88              return false;
89          }
90          if (!match(getType(), pattern.getType())) {
91              return false;
92          }
93          return match(getClassifier(), pattern.getClassifier());
94      }
95  
96      private boolean match(String str, String pattern) {
97          return SelectorUtils.match(pattern, str);
98      }
99  }