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.tasks;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.nio.file.Files;
25 import java.util.ArrayList;
26 import java.util.Map;
27
28 import org.apache.maven.model.RepositoryPolicy;
29 import org.apache.maven.resolver.internal.ant.types.Dependencies;
30 import org.apache.maven.resolver.internal.ant.types.Dependency;
31 import org.apache.maven.resolver.internal.ant.types.DependencyManagement;
32 import org.apache.maven.resolver.internal.ant.types.Pom;
33 import org.apache.maven.resolver.internal.ant.types.model.Developers;
34 import org.apache.maven.resolver.internal.ant.types.model.License;
35 import org.apache.maven.resolver.internal.ant.types.model.Licenses;
36 import org.apache.maven.resolver.internal.ant.types.model.MavenProject;
37 import org.apache.maven.resolver.internal.ant.types.model.Repositories;
38 import org.apache.maven.resolver.internal.ant.types.model.Scm;
39 import org.apache.tools.ant.BuildException;
40 import org.apache.tools.ant.Project;
41 import org.apache.tools.ant.Task;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public class CreatePom extends Task {
106 private String dependenciesIdReference;
107 private String dependencyManagementIdReference;
108 private File pomFile;
109 private String groupId;
110 private String artifactId;
111 private String version;
112 private String name;
113 private String description;
114 private Licenses licenses;
115 private Repositories repositories;
116 private Developers developers;
117 private Scm scm;
118 private boolean skipPomRegistration;
119
120
121
122
123 public CreatePom() {
124
125 }
126
127
128
129
130
131
132 public void setPomTarget(String pomTarget) {
133 pomFile = new File(getProject().replaceProperties(pomTarget));
134 }
135
136
137
138
139
140
141 public void setPomTarget(File pomTarget) {
142 pomFile = pomTarget;
143 }
144
145
146
147
148
149
150
151
152 public void setDependencyManagementRef(String dependencyManagementRefId) {
153 this.dependencyManagementIdReference = getProject().replaceProperties(dependencyManagementRefId);
154 }
155
156
157
158
159
160
161
162
163 public void setDependenciesRef(String dependenciesIdReference) {
164 this.dependenciesIdReference = getProject().replaceProperties(dependenciesIdReference);
165 }
166
167
168
169
170
171
172
173 public void setGroupId(String groupId) {
174 this.groupId = getProject().replaceProperties(groupId);
175 }
176
177
178
179
180
181
182
183 public void setArtifactId(String artifactId) {
184 this.artifactId = getProject().replaceProperties(artifactId);
185 }
186
187
188
189
190
191
192
193 public void setVersion(String version) {
194 this.version = getProject().replaceProperties(version);
195 }
196
197
198
199
200
201
202
203 public void setName(String name) {
204 this.name = getProject().replaceProperties(name);
205 }
206
207
208
209
210
211
212 public String getName() {
213 return name;
214 }
215
216
217
218
219
220
221
222 public void setDescription(String description) {
223 this.description = getProject().replaceProperties(description);
224 }
225
226
227
228
229
230
231
232
233
234
235
236 public void setSkipPomRegistration(boolean skipPomRegistration) {
237 this.skipPomRegistration = skipPomRegistration;
238 }
239
240
241
242
243
244
245
246 public String getGroupId() {
247 if (groupId == null) {
248 return getProject().getProperty("groupId");
249 }
250 return groupId;
251 }
252
253
254
255
256
257
258
259 public String getArtifactId() {
260 if (artifactId == null) {
261 return getProject().getProperty("artifactId");
262 }
263 return artifactId;
264 }
265
266
267
268
269
270
271
272 public String getVersion() {
273 if (version == null) {
274 return getProject().getProperty("version");
275 }
276 return version;
277 }
278
279
280
281
282
283
284
285 public String getDescription() {
286 return description == null ? super.getDescription() : description;
287 }
288
289
290
291
292
293
294
295 public void addLicenses(Licenses licenses) {
296 this.licenses = licenses;
297 }
298
299
300
301
302
303
304
305
306 public void addRepositories(Repositories repositories) {
307 this.repositories = repositories;
308 }
309
310
311
312
313
314
315 public void addDevelopers(Developers developers) {
316 this.developers = developers;
317 }
318
319
320
321
322
323
324 public void addScm(Scm scm) {
325 this.scm = scm;
326 }
327
328
329
330
331 @Override
332 public void execute() {
333 if (!pomFile.getParentFile().exists()) {
334 pomFile.getParentFile().mkdirs();
335 }
336 MavenProject pom = new MavenProject();
337 pom.setGroupId(getGroupId());
338 pom.setArtifactId(getArtifactId());
339 pom.setVersion(getVersion());
340 pom.setName(getName());
341 pom.setDescription(getDescription());
342
343 if (dependencyManagementIdReference != null) {
344 DependencyManagement dependencyManagement =
345 DependencyManagement.get(getProject(), dependencyManagementIdReference);
346 appendManagedDependencies(dependencyManagement.getDependencies(), pom);
347 }
348 if (dependenciesIdReference != null) {
349 Dependencies dependencies = new Dependencies();
350 dependencies.setProject(getProject());
351 org.apache.tools.ant.types.Reference ref =
352 new org.apache.tools.ant.types.Reference(getProject(), dependenciesIdReference);
353 dependencies.setRefid(ref);
354
355 appendDependencies(dependencies, pom);
356 }
357
358 if (licenses != null) {
359 for (License l : licenses.getLicenses()) {
360 pom.addLicense(l);
361 }
362 }
363
364 if (repositories != null) {
365 repositories.getRepositories().forEach(repository -> {
366 org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository();
367 repo.setId(repository.getId());
368 repo.setName(repository.getName());
369 repo.setUrl(repository.getUrl());
370 repo.setLayout(repository.getLayout());
371 if (repository.getReleases() != null) {
372 RepositoryPolicy policy = new RepositoryPolicy();
373 policy.setEnabled(repository.getReleases().isEnabled());
374 repo.setReleases(policy);
375 }
376 if (repository.getSnapshots() != null) {
377 RepositoryPolicy policy = new RepositoryPolicy();
378 policy.setEnabled(repository.getSnapshots().isEnabled());
379 repo.setSnapshots(policy);
380 }
381 pom.getModel().getRepositories().add(repo);
382 });
383 }
384
385 if (developers != null) {
386 developers.getDevelopers().forEach(developer -> {
387 org.apache.maven.model.Developer dev = new org.apache.maven.model.Developer();
388 dev.setId(developer.getIdText());
389 dev.setName(developer.getNameText());
390 dev.setEmail(developer.getEmailText());
391 dev.setUrl(developer.getUrlText());
392 dev.setOrganization(developer.getOrganizationText());
393 dev.setOrganizationUrl(developer.getOrganizationUrlText());
394 if (!developer.getRoles().isEmpty()) {
395 dev.setRoles(new ArrayList<>());
396 developer.getRoles().forEach(role -> {
397 dev.getRoles().add(role.getText());
398 });
399 }
400 dev.setTimezone(developer.getTimezoneText());
401 pom.getModel().getDevelopers().add(dev);
402 });
403 }
404
405 if (scm != null) {
406 org.apache.maven.model.Scm mavenScm = new org.apache.maven.model.Scm();
407 mavenScm.setConnection(scm.getConnectionText());
408 mavenScm.setDeveloperConnection(scm.getDeveloperConnectionText());
409 mavenScm.setUrl(scm.getUrlText());
410 pom.getModel().setScm(mavenScm);
411 }
412
413 try (OutputStream pomOutputStream = Files.newOutputStream(pomFile.toPath())) {
414 pom.toPom(pomOutputStream);
415 log("Created the POM file " + pomFile.getAbsolutePath(), Project.MSG_VERBOSE);
416 } catch (IOException e) {
417 throw new BuildException("Failed to create POM file", e);
418 }
419
420 if (!skipPomRegistration) {
421 registerPom();
422 log("Registered the POM file", Project.MSG_VERBOSE);
423 }
424 }
425
426 private void registerPom() {
427 Map<String, Class<?>> taskDefs = getProject().getTaskDefinitions();
428 if (!taskDefs.containsKey("pom")) {
429 getProject().addTaskDefinition("pom", Pom.class);
430 }
431 Pom pom = (Pom) getProject().createTask("pom");
432 pom.setProject(getProject());
433 pom.setFile(pomFile);
434 pom.execute();
435 }
436
437 private static void appendDependencies(Dependencies dependencies, MavenProject pom) {
438
439 dependencies.getDependencyContainers().forEach(container -> {
440 if (container instanceof Dependency) {
441 Dependency dep = (Dependency) container;
442 pom.addDependency(dep);
443 }
444 });
445 }
446
447 private static void appendManagedDependencies(Dependencies dependencies, MavenProject mavenProject) {
448 dependencies.getDependencyContainers().forEach(it -> {
449 if (it instanceof Dependency) {
450 Dependency dep = (Dependency) it;
451 mavenProject.addToDependencyManagement(dep);
452 }
453 });
454 }
455 }