1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.nuxeo.build.maven;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.apache.maven.artifact.Artifact;
23 import org.eclipse.aether.artifact.DefaultArtifact;
24 import org.eclipse.aether.graph.Dependency;
25 import org.eclipse.aether.util.artifact.JavaScopes;
26
27 import org.nuxeo.build.maven.graph.DependencyUtils;
28
29
30
31
32
33 public class ArtifactDescriptor {
34
35 public static final Pattern PATTERN = Pattern.compile("(?<groupId>[^: ]+):(?<artifactId>[^: ]+)"
36 + "(?::(?<version>[^: ]*)(?::(?<type>[^: ]*)(?::(?<classifier>[^: ]*)(?::(?<scope>[^: ]*))?)?)?)?");
37
38 protected String groupId = null;
39
40 protected String artifactId = null;
41
42 protected String version = null;
43
44 protected String type = null;
45
46 protected String classifier = null;
47
48 protected String scope = null;
49
50 public ArtifactDescriptor() {
51 }
52
53 public ArtifactDescriptor(String groupId, String artifactId, String version, String type, String classifier) {
54 this.groupId = groupId;
55 this.artifactId = artifactId;
56 this.version = version;
57 this.type = type;
58 this.classifier = classifier;
59 scope = JavaScopes.COMPILE;
60 }
61
62
63
64
65
66
67
68 public ArtifactDescriptor(String key) {
69 Matcher m = PATTERN.matcher(key);
70 if (!m.matches()) {
71 throw new IllegalArgumentException(String.format("Invalid key '%s', expected format is '%s'", key,
72 "<groupId>:<artifactId>[:<version>[:<type>[:<classifier>[:<scope>]]]]"));
73 }
74 groupId = m.group("groupId");
75 artifactId = m.group("artifactId");
76 if (m.group("version") != null && !m.group("version").isEmpty()) {
77 version = m.group("version");
78 }
79 if (m.group("type") != null && !m.group("type").isEmpty()) {
80 type = m.group("type");
81 } else {
82 type = "jar";
83 }
84 if (m.group("classifier") != null && !m.group("classifier").isEmpty()) {
85 classifier = m.group("classifier");
86 }
87 if (m.group("scope") != null && !m.group("scope").isEmpty()) {
88 scope = m.group("scope");
89 } else {
90 scope = JavaScopes.COMPILE;
91 }
92 }
93
94 public String getNodeKeyPattern() {
95 if (groupId != null) {
96 StringBuilder buf = new StringBuilder();
97 buf.append(groupId);
98 if (artifactId != null) {
99 buf.append(':').append(artifactId);
100 if (version != null) {
101 buf.append(':').append(version);
102 if (type != null) {
103 buf.append(':').append(type);
104 }
105 }
106 }
107 return buf.toString();
108 }
109 return null;
110 }
111
112 @Override
113 public String toString() {
114 StringBuilder buf = new StringBuilder();
115 buf.append(groupId).append(':').append(artifactId);
116 buf.append(':').append(version);
117 buf.append(':').append(type);
118 buf.append(':');
119 if (classifier != null) {
120 buf.append(classifier);
121 }
122 buf.append(':').append(scope);
123 return buf.toString();
124 }
125
126
127
128
129
130 public Artifact getMavenArtifact() {
131 org.eclipse.aether.artifact.Artifact aetherArtifact = getAetherArtifact();
132 return DependencyUtils.aetherToMaven(aetherArtifact, scope);
133 }
134
135
136
137
138
139
140 public org.eclipse.aether.artifact.Artifact getAetherArtifact() {
141 return getDependency().getArtifact();
142 }
143
144
145
146
147 public Dependency getDependency() {
148 return new Dependency(new DefaultArtifact(groupId, artifactId, classifier, type, version), scope);
149 }
150
151
152
153
154
155
156 public static ArtifactDescriptor parseQuietly(String key) {
157 try {
158 return new ArtifactDescriptor(key);
159 } catch (IllegalArgumentException | IllegalStateException e) {
160
161 return new ArtifactDescriptor();
162 }
163 }
164
165 public String getGroupId() {
166 return groupId;
167 }
168
169 public void setGroupId(String groupId) {
170 this.groupId = groupId;
171 }
172
173 public String getArtifactId() {
174 return artifactId;
175 }
176
177 public void setArtifactId(String artifactId) {
178 this.artifactId = artifactId;
179 }
180
181 public String getVersion() {
182 return version;
183 }
184
185 public void setVersion(String version) {
186 this.version = version;
187 }
188
189 public String getType() {
190 return type;
191 }
192
193 public void setType(String type) {
194 this.type = type;
195 }
196
197 public String getClassifier() {
198 return classifier;
199 }
200
201 public void setClassifier(String classifier) {
202 this.classifier = classifier;
203 }
204
205 public String getScope() {
206 return scope;
207 }
208
209 public void setScope(String scope) {
210 this.scope = scope;
211 }
212
213
214
215
216 public boolean isEmpty() {
217 return groupId == null && artifactId == null && groupId == null && scope == null && type == null
218 && classifier == null;
219 }
220 }