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.repack;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static com.google.common.base.Preconditions.checkState;
19  import static org.basepom.mojo.repack.Wildcard.wildcardMatch;
20  
21  import java.util.List;
22  import java.util.Objects;
23  import java.util.Optional;
24  import javax.annotation.CheckForNull;
25  
26  import com.google.common.base.Joiner;
27  import com.google.common.base.MoreObjects;
28  import com.google.common.base.Splitter;
29  import com.google.common.collect.ImmutableList;
30  import org.apache.maven.artifact.Artifact;
31  
32  final class DependencyDefinition {
33  
34      private final String artifactId;
35      private final String groupId;
36      private final String type;
37      @CheckForNull
38      private final String classifier;
39  
40      DependencyDefinition(final String value) {
41          checkNotNull(value, "value is null");
42  
43          List<String> elements = Splitter.on(':').trimResults().splitToList(value);
44          checkState(!elements.isEmpty(), "Dependency reference requires at least a group id!");
45  
46          String groupId = elements.get(0);
47  
48          this.groupId = groupId.isEmpty() ? "*" : groupId;
49  
50          this.artifactId = elements.size() > 1 && !elements.get(1).isEmpty() ? elements.get(1) : "*";
51          String type = elements.size() > 2 && !elements.get(2).isEmpty() ? elements.get(2) : "jar";
52          String classifier = elements.size() > 3 && !elements.get(3).isEmpty() ? elements.get(3) : null;
53  
54          if ("test-jar".equals(type)) {
55              this.type = "jar";
56              this.classifier = MoreObjects.firstNonNull(classifier, "tests");
57          } else {
58              this.type = type;
59              this.classifier = classifier;
60          }
61      }
62  
63      DependencyDefinition(final Artifact artifact) {
64          checkNotNull(artifact, "artifact is null");
65  
66          this.artifactId = checkNotNull(artifact.getArtifactId(), "artifactId for artifact '%s' is null", artifact);
67          this.groupId = checkNotNull(artifact.getGroupId(), "groupId for artifact '%s' is null", artifact);
68  
69          final String type = artifact.getType();
70          final String classifier = artifact.getClassifier();
71          if ("test-jar".equals(type)) {
72              this.classifier = MoreObjects.firstNonNull(classifier, "tests");
73              this.type = "jar";
74          } else {
75              this.type = MoreObjects.firstNonNull(type, "jar");
76              this.classifier = classifier;
77          }
78      }
79  
80      public String getArtifactId() {
81          return artifactId;
82      }
83  
84      public String getGroupId() {
85          return groupId;
86      }
87  
88      public String getType() {
89          return type;
90      }
91  
92      public Optional<String> getClassifier() {
93          return Optional.ofNullable(classifier);
94      }
95  
96      public boolean matches(final Artifact artifact) {
97          return matches(new DependencyDefinition(artifact));
98      }
99  
100     public boolean matches(final DependencyDefinition other) {
101 
102         if (!wildcardMatch(getGroupId(), other.getGroupId())) {
103             return false;
104         }
105 
106         if (!wildcardMatch(getArtifactId(), other.getArtifactId())) {
107             return false;
108         }
109 
110         if (!Objects.equals(getType(), other.getType())) {
111             return false;
112         }
113 
114         // If a classifier is present, try to match the other classifier,
115         // otherwise, if no classifier is present, it matches all classifiers from the other DependencyDefinition.
116         return getClassifier()
117             .map(cl -> Objects.equals(cl, other.getClassifier().orElse(null)))
118             .orElse(true);
119     }
120 
121     @Override
122     public boolean equals(Object o) {
123         if (this == o) {
124             return true;
125         }
126         if (o == null || getClass() != o.getClass()) {
127             return false;
128         }
129         DependencyDefinition that = (DependencyDefinition) o;
130         return artifactId.equals(that.artifactId) && groupId.equals(that.groupId) && type.equals(that.type) && Objects.equals(classifier, that.classifier);
131     }
132 
133     @Override
134     public int hashCode() {
135         return Objects.hash(artifactId, groupId, type, classifier);
136     }
137 
138     @Override
139     public String toString() {
140         final ImmutableList.Builder<String> builder = ImmutableList.builder();
141 
142         builder.add(getGroupId());
143         builder.add(getArtifactId());
144 
145         builder.add(getType());
146         builder.add(getClassifier().orElse("<any>"));
147         return Joiner.on(':').join(builder.build());
148     }
149 }