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