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.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.maven.resolver.internal.ant.AntRepoSys;
33 import org.apache.maven.resolver.internal.ant.Names;
34 import org.apache.maven.resolver.internal.ant.types.Dependencies;
35 import org.apache.maven.resolver.internal.ant.types.Pom;
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.Project;
38 import org.apache.tools.ant.ProjectComponent;
39 import org.apache.tools.ant.types.FileSet;
40 import org.apache.tools.ant.types.Reference;
41 import org.apache.tools.ant.types.resources.FileResource;
42 import org.apache.tools.ant.types.resources.Resources;
43 import org.apache.tools.ant.util.FileUtils;
44 import org.eclipse.aether.RepositorySystem;
45 import org.eclipse.aether.RepositorySystemSession;
46 import org.eclipse.aether.artifact.Artifact;
47 import org.eclipse.aether.graph.DependencyFilter;
48 import org.eclipse.aether.graph.DependencyNode;
49 import org.eclipse.aether.resolution.ArtifactRequest;
50 import org.eclipse.aether.resolution.ArtifactResolutionException;
51 import org.eclipse.aether.resolution.ArtifactResult;
52 import org.eclipse.aether.util.artifact.SubArtifact;
53 import org.eclipse.aether.util.filter.ScopeDependencyFilter;
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 Resolve extends AbstractResolvingTask {
106
107 private final List<ArtifactConsumer> consumers = new ArrayList<>();
108
109 private boolean failOnMissingAttachments;
110
111
112
113
114 public Resolve() {
115
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public void setFailOnMissingAttachments(boolean failOnMissingAttachments) {
134 this.failOnMissingAttachments = failOnMissingAttachments;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public Path createPath() {
151 Path path = new Path();
152 consumers.add(path);
153 return path;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public Files createFiles() {
171 Files files = new Files();
172 consumers.add(files);
173 return files;
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public Props createProperties() {
195 Props props = new Props();
196 consumers.add(props);
197 return props;
198 }
199
200 private void validate() {
201 for (ArtifactConsumer consumer : consumers) {
202 consumer.validate();
203 }
204
205 Pom pom = AntRepoSys.getInstance(getProject()).getDefaultPom();
206 if (dependencies == null && pom != null) {
207 log("Using default pom for dependency resolution (" + pom.toString() + ")", Project.MSG_INFO);
208 dependencies = new Dependencies();
209 dependencies.setProject(getProject());
210 getProject().addReference(Names.ID_DEFAULT_POM, pom);
211 dependencies.setPomRef(new Reference(getProject(), Names.ID_DEFAULT_POM));
212 }
213
214 if (dependencies != null) {
215 dependencies.validate(this);
216 } else {
217 throw new BuildException("No <dependencies> set for resolution");
218 }
219 }
220
221 @Override
222 public void execute() throws BuildException {
223 validate();
224
225 AntRepoSys sys = AntRepoSys.getInstance(getProject());
226
227 RepositorySystemSession session = sys.getSession(this, localRepository);
228 RepositorySystem system = sys.getSystem();
229 log("Using local repository " + session.getLocalRepository(), Project.MSG_VERBOSE);
230
231 DependencyNode root = collectDependencies().getRoot();
232 root.accept(new DependencyGraphLogger(this));
233
234 Map<String, Group> groups = new HashMap<>();
235 for (ArtifactConsumer consumer : consumers) {
236 String classifier = consumer.getClassifier();
237 Group group = groups.get(classifier);
238 if (group == null) {
239 group = new Group(classifier);
240 groups.put(classifier, group);
241 }
242 group.add(consumer);
243 }
244
245 for (Group group : groups.values()) {
246 group.createRequests(root);
247 }
248
249 log("Resolving artifacts", Project.MSG_INFO);
250
251 for (Group group : groups.values()) {
252 List<ArtifactResult> results;
253 try {
254 results = system.resolveArtifacts(session, group.getRequests());
255 } catch (ArtifactResolutionException e) {
256 if (!group.isAttachments() || failOnMissingAttachments) {
257 throw new BuildException("Could not resolve artifacts: " + e.getMessage(), e);
258 }
259 results = e.getResults();
260 for (ArtifactResult result : results) {
261 if (result.isMissing()) {
262 log("Ignoring missing attachment " + result.getRequest().getArtifact(), Project.MSG_VERBOSE);
263 } else if (!result.isResolved()) {
264 throw new BuildException("Could not resolve artifacts: " + e.getMessage(), e);
265 }
266 }
267 }
268
269 group.processResults(results, session);
270 }
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296 public abstract static class ArtifactConsumer extends ProjectComponent {
297
298 private DependencyFilter filter;
299
300
301
302
303
304
305
306 public ArtifactConsumer() {
307
308 }
309
310
311
312
313
314
315
316
317
318 public boolean accept(org.eclipse.aether.graph.DependencyNode node, List<DependencyNode> parents) {
319 return filter == null || filter.accept(node, parents);
320 }
321
322
323
324
325
326
327
328
329
330
331 public String getClassifier() {
332 return null;
333 }
334
335
336
337
338
339
340
341
342
343
344
345 public void validate() {}
346
347
348
349
350
351
352
353
354
355
356
357 public abstract void process(Artifact artifact, RepositorySystemSession session);
358
359
360
361
362
363
364
365
366
367
368
369
370
371 public void setScopes(String scopes) {
372 if (filter != null) {
373 throw new BuildException("You must not specify both 'scopes' and 'classpath'");
374 }
375
376 Collection<String> included = new HashSet<>();
377 Collection<String> excluded = new HashSet<>();
378
379 String[] split = scopes.split("[, ]");
380 for (String scope : split) {
381 scope = scope.trim();
382 Collection<String> dst;
383 if (scope.startsWith("-") || scope.startsWith("!")) {
384 dst = excluded;
385 scope = scope.substring(1);
386 } else {
387 dst = included;
388 }
389 if (!scope.isEmpty()) {
390 dst.add(scope);
391 }
392 }
393
394 filter = new ScopeDependencyFilter(included, excluded);
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 public void setClasspath(String classpath) {
413 if ("compile".equals(classpath)) {
414 setScopes("provided,system,compile");
415 } else if ("runtime".equals(classpath)) {
416 setScopes("compile,runtime");
417 } else if ("test".equals(classpath)) {
418 setScopes("provided,system,compile,runtime,test");
419 } else {
420 throw new BuildException("The classpath '" + classpath + "' is not defined"
421 + ", must be one of 'compile', 'runtime' or 'test'");
422 }
423 }
424 }
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 public static class Path extends ArtifactConsumer {
448
449 private String refid;
450
451 private org.apache.tools.ant.types.Path path;
452
453
454
455
456 public Path() {
457
458 }
459
460
461
462
463
464
465 public void setRefId(String refId) {
466 this.refid = refId;
467 }
468
469
470
471
472
473
474 @Override
475 public void validate() {
476 if (refid == null) {
477 throw new BuildException("You must specify the 'refid' for the path");
478 }
479 }
480
481
482
483
484
485
486
487 @Override
488 public void process(Artifact artifact, RepositorySystemSession session) {
489 if (path == null) {
490 path = new org.apache.tools.ant.types.Path(getProject());
491 getProject().addReference(refid, path);
492 }
493 File file = artifact.getFile();
494 path.add(new FileResource(file.getParentFile(), file.getName()));
495 }
496 }
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 public class Files extends ArtifactConsumer {
521
522 private static final String DEFAULT_LAYOUT = Layout.GID_DIRS + "/" + Layout.AID + "/" + Layout.BVER + "/"
523 + Layout.AID + "-" + Layout.VER + "-" + Layout.CLS + "." + Layout.EXT;
524
525 private String refid;
526
527 private String classifier;
528
529 private File dir;
530
531 private Layout layout;
532
533 private FileSet fileset;
534
535 private Resources resources;
536
537
538
539
540
541
542
543 public Files() {
544
545 }
546
547
548
549
550
551
552 public void setRefId(String refId) {
553 this.refid = refId;
554 }
555
556
557
558
559
560
561 @Override
562 public String getClassifier() {
563 return classifier;
564 }
565
566
567
568
569
570
571
572
573
574
575
576
577 public void setAttachments(String attachments) {
578 if ("sources".equals(attachments)) {
579 classifier = "*-sources";
580 } else if ("javadoc".equals(attachments)) {
581 classifier = "*-javadoc";
582 } else {
583 throw new BuildException("The attachment type '" + attachments
584 + "' is not defined, must be one of 'sources' or 'javadoc'");
585 }
586 }
587
588
589
590
591
592
593
594 public void setDir(File dir) {
595 this.dir = dir;
596 if (dir != null && layout == null) {
597 layout = new Layout(DEFAULT_LAYOUT);
598 }
599 }
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618 public void setLayout(String layout) {
619 this.layout = new Layout(layout);
620 }
621
622
623
624
625
626
627 @Override
628 public void validate() {
629 if (refid == null && dir == null) {
630 throw new BuildException("You must either specify the 'refid' for the resource collection"
631 + " or a 'dir' to copy the files to");
632 }
633 if (dir == null && layout != null) {
634 throw new BuildException("You must not specify a 'layout' unless 'dir' is also specified");
635 }
636 }
637
638
639
640
641
642
643
644
645 @Override
646 public void process(Artifact artifact, RepositorySystemSession session) {
647 if (dir != null) {
648 if (refid != null && fileset == null) {
649 fileset = new FileSet();
650 fileset.setProject(getProject());
651 fileset.setDir(dir);
652 getProject().addReference(refid, fileset);
653 }
654
655 String path = layout.getPath(artifact);
656
657 if (fileset != null) {
658 fileset.createInclude().setName(path);
659 }
660
661 File src = artifact.getFile();
662 File dst = new File(dir, path);
663
664 if (src.lastModified() != dst.lastModified() || src.length() != dst.length()) {
665 try {
666 Resolve.this.log("Copy " + src + " to " + dst, Project.MSG_VERBOSE);
667 FileUtils.getFileUtils().copyFile(src, dst, null, true, true);
668 } catch (IOException e) {
669 throw new BuildException(
670 "Failed to copy artifact file " + src + " to " + dst + ": " + e.getMessage(), e);
671 }
672 } else {
673 Resolve.this.log("Omit to copy " + src + " to " + dst + ", seems unchanged", Project.MSG_VERBOSE);
674 }
675 } else {
676 if (resources == null) {
677 resources = new Resources();
678 resources.setProject(getProject());
679 getProject().addReference(refid, resources);
680 }
681
682 FileResource resource = new FileResource(artifact.getFile());
683 resource.setBaseDir(session.getLocalRepository().getBasedir());
684 resource.setProject(getProject());
685 resources.add(resource);
686 }
687 }
688 }
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720 public static class Props extends ArtifactConsumer {
721
722 private String prefix;
723
724 private String classifier;
725
726
727
728
729 public Props() {
730
731 }
732
733
734
735
736
737
738
739
740 public void setPrefix(String prefix) {
741 this.prefix = prefix;
742 }
743
744
745
746
747
748
749 @Override
750 public String getClassifier() {
751 return classifier;
752 }
753
754
755
756
757
758
759
760
761
762
763
764
765 public void setAttachments(String attachments) {
766 if ("sources".equals(attachments)) {
767 classifier = "*-sources";
768 } else if ("javadoc".equals(attachments)) {
769 classifier = "*-javadoc";
770 } else {
771 throw new BuildException("The attachment type '" + attachments
772 + "' is not defined, must be one of 'sources' or 'javadoc'");
773 }
774 }
775
776
777
778
779
780
781
782
783 @Override
784 public void process(Artifact artifact, RepositorySystemSession session) {
785 StringBuilder buffer = new StringBuilder(256);
786 if (prefix != null && !prefix.isEmpty()) {
787 buffer.append(prefix);
788 if (!prefix.endsWith(".")) {
789 buffer.append('.');
790 }
791 }
792 buffer.append(artifact.getGroupId());
793 buffer.append(':');
794 buffer.append(artifact.getArtifactId());
795 buffer.append(':');
796 buffer.append(artifact.getExtension());
797 if (!artifact.getClassifier().isEmpty()) {
798 buffer.append(':');
799 buffer.append(artifact.getClassifier());
800 }
801
802 String path = artifact.getFile().getAbsolutePath();
803
804 getProject().setProperty(buffer.toString(), path);
805 }
806 }
807
808 private static class Group {
809
810 private final String classifier;
811
812 private final List<ArtifactConsumer> consumers = new ArrayList<>();
813
814 private final List<ArtifactRequest> requests = new ArrayList<>();
815
816 Group(String classifier) {
817 this.classifier = classifier;
818 }
819
820 public boolean isAttachments() {
821 return classifier != null;
822 }
823
824 public void add(ArtifactConsumer consumer) {
825 consumers.add(consumer);
826 }
827
828 public void createRequests(DependencyNode node) {
829 createRequests(node, new LinkedList<>());
830 }
831
832 private void createRequests(DependencyNode node, LinkedList<DependencyNode> parents) {
833 if (node.getDependency() != null) {
834 for (ArtifactConsumer consumer : consumers) {
835 if (consumer.accept(node, parents)) {
836 ArtifactRequest request = new ArtifactRequest(node);
837 if (classifier != null) {
838 request.setArtifact(new SubArtifact(request.getArtifact(), classifier, "jar"));
839 }
840 requests.add(request);
841 break;
842 }
843 }
844 }
845
846 parents.addFirst(node);
847
848 for (DependencyNode child : node.getChildren()) {
849 createRequests(child, parents);
850 }
851
852 parents.removeFirst();
853 }
854
855 public List<ArtifactRequest> getRequests() {
856 return requests;
857 }
858
859 public void processResults(List<ArtifactResult> results, RepositorySystemSession session) {
860 for (ArtifactResult result : results) {
861 if (!result.isResolved()) {
862 continue;
863 }
864 for (ArtifactConsumer consumer : consumers) {
865 if (consumer.accept(
866 result.getRequest().getDependencyNode(), Collections.<DependencyNode>emptyList())) {
867 consumer.process(result.getArtifact(), session);
868 }
869 }
870 }
871 }
872 }
873 }