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.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.maven.model.Model;
26 import org.apache.maven.resolver.internal.ant.AntRepoSys;
27 import org.apache.maven.resolver.internal.ant.ProjectWorkspaceReader;
28 import org.apache.maven.resolver.internal.ant.tasks.RefTask;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.PropertyHelper;
31 import org.apache.tools.ant.Task;
32 import org.apache.tools.ant.types.Reference;
33
34
35
36 public class Pom extends RefTask {
37
38 private Model model;
39
40 private String id;
41
42 private File file;
43
44 private String groupId;
45
46 private String artifactId;
47
48 private String version;
49
50 private String packaging = "jar";
51
52 private RemoteRepositories remoteRepositories;
53
54 private String coords;
55
56 protected Pom getRef() {
57 return (Pom) getCheckedRef();
58 }
59
60 public void validate() {
61 if (isReference()) {
62 getRef().validate();
63 } else {
64 if (file == null) {
65 if (groupId == null) {
66 throw new BuildException("You must specify the 'groupId' for the POM");
67 }
68 if (artifactId == null) {
69 throw new BuildException("You must specify the 'artifactId' for the POM");
70 }
71 if (version == null) {
72 throw new BuildException("You must specify the 'version' for the POM");
73 }
74 }
75 }
76 }
77
78 public void setRefid(Reference ref) {
79 if (id != null || file != null || groupId != null || artifactId != null || version != null) {
80 throw tooManyAttributes();
81 }
82 if (remoteRepositories != null) {
83 throw noChildrenAllowed();
84 }
85 super.setRefid(ref);
86 }
87
88 public void setId(String id) {
89 checkAttributesAllowed();
90 this.id = id;
91 }
92
93 public File getFile() {
94 if (isReference()) {
95 return getRef().getFile();
96 }
97 return file;
98 }
99
100 public void setFile(File file) {
101 checkAttributesAllowed();
102 if (groupId != null || artifactId != null || version != null) {
103 throw ambiguousSource();
104 }
105
106 this.file = file;
107 }
108
109 public String getGroupId() {
110 if (isReference()) {
111 return getRef().getGroupId();
112 }
113 return groupId;
114 }
115
116 public void setGroupId(String groupId) {
117 checkAttributesAllowed();
118 if (this.groupId != null) {
119 throw ambiguousCoords();
120 }
121 if (file != null) {
122 throw ambiguousSource();
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 if (file != null) {
140 throw ambiguousSource();
141 }
142 this.artifactId = artifactId;
143 }
144
145 public String getVersion() {
146 if (isReference()) {
147 return getRef().getVersion();
148 }
149 return version;
150 }
151
152 public void setVersion(String version) {
153 checkAttributesAllowed();
154 if (this.version != null) {
155 throw ambiguousCoords();
156 }
157 if (file != null) {
158 throw ambiguousSource();
159 }
160 this.version = version;
161 }
162
163 public String getCoords() {
164 if (isReference()) {
165 return getRef().getCoords();
166 }
167 return coords;
168 }
169
170 public void setCoords(String coords) {
171 checkAttributesAllowed();
172 if (file != null) {
173 throw ambiguousSource();
174 }
175 if (groupId != null || artifactId != null || version != null) {
176 throw ambiguousCoords();
177 }
178 Pattern p = Pattern.compile("([^: ]+):([^: ]+):([^: ]+)");
179 Matcher m = p.matcher(coords);
180 if (!m.matches()) {
181 throw new BuildException("Bad POM coordinates, expected format is <groupId>:<artifactId>:<version>");
182 }
183 groupId = m.group(1);
184 artifactId = m.group(2);
185 version = m.group(3);
186 }
187
188 private BuildException ambiguousCoords() {
189 return new BuildException("You must not specify both 'coords' and ('groupId', 'artifactId', 'version')");
190 }
191
192 private BuildException ambiguousSource() {
193 return new BuildException(
194 "You must not specify both 'file' and " + "('coords', 'groupId', 'artifactId', 'version')");
195 }
196
197 public String getPackaging() {
198 if (isReference()) {
199 return getRef().getPackaging();
200 }
201 return packaging;
202 }
203
204 public void setPackaging(String packaging) {
205 checkAttributesAllowed();
206 if (file != null) {
207 throw ambiguousSource();
208 }
209 this.packaging = packaging;
210 }
211
212 private RemoteRepositories getRemoteRepos() {
213 if (remoteRepositories == null) {
214 remoteRepositories = new RemoteRepositories();
215 remoteRepositories.setProject(getProject());
216 }
217 return remoteRepositories;
218 }
219
220 public void addRemoteRepo(RemoteRepository repository) {
221 getRemoteRepos().addRemoterepo(repository);
222 }
223
224 public void addRemoteRepos(RemoteRepositories repositories) {
225 getRemoteRepos().addRemoterepos(repositories);
226 }
227
228 public void setRemoteReposRef(Reference ref) {
229 RemoteRepositories repos = new RemoteRepositories();
230 repos.setProject(getProject());
231 repos.setRefid(ref);
232 getRemoteRepos().addRemoterepos(repos);
233 }
234
235 public Model getModel(Task task) {
236 if (isReference()) {
237 return getRef().getModel(task);
238 }
239 synchronized (this) {
240 if (model == null) {
241 if (file != null) {
242 model = AntRepoSys.getInstance(getProject()).loadModel(task, file, true, remoteRepositories);
243 }
244 }
245 return model;
246 }
247 }
248
249 @Override
250 public void execute() {
251 validate();
252
253 if (file != null && (id == null || AntRepoSys.getInstance(getProject()).getDefaultPom() == null)) {
254 AntRepoSys.getInstance(getProject()).setDefaultPom(this);
255 }
256
257 ProjectWorkspaceReader.getInstance().addPom(this);
258
259 Model model = getModel(this);
260
261 if (model == null) {
262 coords = getGroupId() + ":" + getArtifactId() + ":" + getVersion();
263 return;
264 }
265
266 coords = model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion();
267
268 ModelValueExtractor extractor = new ModelValueExtractor(id, model, getProject());
269
270 PropertyHelper propHelper = PropertyHelper.getPropertyHelper(getProject());
271
272 try {
273
274 PomPropertyEvaluator.register(extractor, propHelper);
275 } catch (LinkageError e) {
276
277 PomPropertyHelper.register(extractor, propHelper);
278 }
279 }
280
281 public String toString() {
282 return coords + " (" + super.toString() + ")";
283 }
284 }