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