1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant.types;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Project;
29 import org.apache.tools.ant.Task;
30 import org.apache.tools.ant.types.DataType;
31 import org.apache.tools.ant.types.Reference;
32
33
34
35 public class Dependency extends DataType implements DependencyContainer {
36
37 private String groupId;
38
39 private String artifactId;
40
41 private String version;
42
43 private String classifier;
44
45 private String type;
46
47 private String scope;
48
49 private File systemPath;
50
51 private List<Exclusion> exclusions = new ArrayList<>();
52
53 protected Dependency getRef() {
54 return (Dependency) getCheckedRef();
55 }
56
57 public void validate(Task task) {
58 if (isReference()) {
59 getRef().validate(task);
60 } else {
61 if (groupId == null || groupId.length() <= 0) {
62 throw new BuildException("You must specify the 'groupId' for a dependency");
63 }
64 if (artifactId == null || artifactId.length() <= 0) {
65 throw new BuildException("You must specify the 'artifactId' for a dependency");
66 }
67 if (version == null || version.length() <= 0) {
68 throw new BuildException("You must specify the 'version' for a dependency");
69 }
70
71 if ("system".equals(scope)) {
72 if (systemPath == null) {
73 throw new BuildException("You must specify 'systemPath' for dependencies with scope=system");
74 }
75 } else if (systemPath != null) {
76 throw new BuildException("You may only specify 'systemPath' for dependencies with scope=system");
77 }
78
79 if (scope != null
80 && !"compile".equals(scope)
81 && !"provided".equals(scope)
82 && !"system".equals(scope)
83 && !"runtime".equals(scope)
84 && !"test".equals(scope)) {
85 task.log("Unknown scope '" + scope + "' for dependency", Project.MSG_WARN);
86 }
87
88 for (Exclusion exclusion : exclusions) {
89 exclusion.validate(task);
90 }
91 }
92 }
93
94 public void setRefid(Reference ref) {
95 if (groupId != null
96 || artifactId != null
97 || type != null
98 || classifier != null
99 || version != null
100 || scope != null
101 || systemPath != null) {
102 throw tooManyAttributes();
103 }
104 if (!exclusions.isEmpty()) {
105 throw noChildrenAllowed();
106 }
107 super.setRefid(ref);
108 }
109
110 public String getGroupId() {
111 if (isReference()) {
112 return getRef().getGroupId();
113 }
114 return groupId;
115 }
116
117 public void setGroupId(String groupId) {
118 checkAttributesAllowed();
119 if (this.groupId != null) {
120 throw ambiguousCoords();
121 }
122 this.groupId = groupId;
123 }
124
125 public String getArtifactId() {
126 if (isReference()) {
127 return getRef().getArtifactId();
128 }
129 return artifactId;
130 }
131
132 public void setArtifactId(String artifactId) {
133 checkAttributesAllowed();
134 if (this.artifactId != null) {
135 throw ambiguousCoords();
136 }
137 this.artifactId = artifactId;
138 }
139
140 public String getVersion() {
141 if (isReference()) {
142 return getRef().getVersion();
143 }
144 return version;
145 }
146
147 public void setVersion(String version) {
148 checkAttributesAllowed();
149 if (this.version != null) {
150 throw ambiguousCoords();
151 }
152 this.version = version;
153 }
154
155 public String getClassifier() {
156 if (isReference()) {
157 return getRef().getClassifier();
158 }
159 return classifier;
160 }
161
162 public void setClassifier(String classifier) {
163 checkAttributesAllowed();
164 if (this.classifier != null) {
165 throw ambiguousCoords();
166 }
167 this.classifier = classifier;
168 }
169
170 public String getType() {
171 if (isReference()) {
172 return getRef().getType();
173 }
174 return (type != null) ? type : "jar";
175 }
176
177 public void setType(String type) {
178 checkAttributesAllowed();
179 if (this.type != null) {
180 throw ambiguousCoords();
181 }
182 this.type = type;
183 }
184
185 public String getScope() {
186 if (isReference()) {
187 return getRef().getScope();
188 }
189 return (scope != null) ? scope : "compile";
190 }
191
192 public void setScope(String scope) {
193 checkAttributesAllowed();
194 if (this.scope != null) {
195 throw ambiguousCoords();
196 }
197 this.scope = scope;
198 }
199
200 public void setCoords(String coords) {
201 checkAttributesAllowed();
202 if (groupId != null
203 || artifactId != null
204 || version != null
205 || type != null
206 || classifier != null
207 || scope != null) {
208 throw ambiguousCoords();
209 }
210 Pattern p = Pattern.compile("([^: ]+):([^: ]+):([^: ]+)((:([^: ]+)(:([^: ]+))?)?:([^: ]+))?");
211 Matcher m = p.matcher(coords);
212 if (!m.matches()) {
213 throw new BuildException("Bad dependency coordinates '" + coords
214 + "', expected format is <groupId>:<artifactId>:<version>[[:<type>[:<classifier>]]:<scope>]");
215 }
216 groupId = m.group(1);
217 artifactId = m.group(2);
218 version = m.group(3);
219 type = m.group(6);
220 if (type == null || type.length() <= 0) {
221 type = "jar";
222 }
223 classifier = m.group(8);
224 if (classifier == null) {
225 classifier = "";
226 }
227 scope = m.group(9);
228 }
229
230 public void setSystemPath(File systemPath) {
231 checkAttributesAllowed();
232 this.systemPath = systemPath;
233 }
234
235 public File getSystemPath() {
236 if (isReference()) {
237 return getRef().getSystemPath();
238 }
239 return systemPath;
240 }
241
242 public String getVersionlessKey() {
243 if (isReference()) {
244 return getRef().getVersionlessKey();
245 }
246 StringBuilder key = new StringBuilder(128);
247 if (groupId != null) {
248 key.append(groupId);
249 }
250 key.append(':');
251 if (artifactId != null) {
252 key.append(artifactId);
253 }
254 key.append(':');
255 key.append((type != null) ? type : "jar");
256 if (classifier != null && classifier.length() > 0) {
257 key.append(':');
258 key.append(classifier);
259 }
260 return key.toString();
261 }
262
263 public void addExclusion(Exclusion exclusion) {
264 checkChildrenAllowed();
265 this.exclusions.add(exclusion);
266 }
267
268 public List<Exclusion> getExclusions() {
269 if (isReference()) {
270 return getRef().getExclusions();
271 }
272 return exclusions;
273 }
274
275 private BuildException ambiguousCoords() {
276 return new BuildException("You must not specify both 'coords' and "
277 + "('groupId', 'artifactId', 'version', 'extension', 'classifier', 'scope')");
278 }
279 }