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.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Task;
29 import org.apache.tools.ant.types.DataType;
30 import org.apache.tools.ant.types.Reference;
31
32
33
34 public class Dependencies extends DataType implements DependencyContainer {
35
36 private File file;
37
38 private Pom pom;
39
40 private List<DependencyContainer> containers = new ArrayList<>();
41
42 private List<Exclusion> exclusions = new ArrayList<>();
43
44 private boolean nestedDependencies;
45
46 protected Dependencies getRef() {
47 return (Dependencies) getCheckedRef();
48 }
49
50 public void validate(Task task) {
51 if (isReference()) {
52 getRef().validate(task);
53 } else {
54 if (getPom() != null && getPom().getFile() == null) {
55 throw new BuildException("A <pom> used for dependency resolution has to be backed by a pom.xml file");
56 }
57 Map<String, String> ids = new HashMap<>();
58 for (DependencyContainer container : containers) {
59 container.validate(task);
60 if (container instanceof Dependency) {
61 Dependency dependency = (Dependency) container;
62 String id = dependency.getVersionlessKey();
63 String collision = ids.put(id, dependency.getVersion());
64 if (collision != null) {
65 throw new BuildException("You must not declare multiple <dependency> elements"
66 + " with the same coordinates but got " + id + " -> " + collision + " vs "
67 + dependency.getVersion());
68 }
69 }
70 }
71 }
72 }
73
74 public void setRefid(Reference ref) {
75 if (pom != null || !exclusions.isEmpty() || !containers.isEmpty()) {
76 throw noChildrenAllowed();
77 }
78 super.setRefid(ref);
79 }
80
81 public void setFile(File file) {
82 checkAttributesAllowed();
83 this.file = file;
84 checkExternalSources();
85 }
86
87 public File getFile() {
88 if (isReference()) {
89 return getRef().getFile();
90 }
91 return file;
92 }
93
94 public void addPom(Pom pom) {
95 checkChildrenAllowed();
96 if (this.pom != null) {
97 throw new BuildException("You must not specify multiple <pom> elements");
98 }
99 this.pom = pom;
100 checkExternalSources();
101 }
102
103 public Pom getPom() {
104 if (isReference()) {
105 return getRef().getPom();
106 }
107 return pom;
108 }
109
110 public void setPomRef(Reference ref) {
111 if (pom == null) {
112 pom = new Pom();
113 pom.setProject(getProject());
114 }
115 pom.setRefid(ref);
116 checkExternalSources();
117 }
118
119 private void checkExternalSources() {
120 if (file != null && pom != null) {
121 throw new BuildException("You must not specify both a text file and a POM to list dependencies");
122 }
123 if ((file != null || pom != null) && nestedDependencies) {
124 throw new BuildException("You must not specify both a file/POM and nested dependency collections");
125 }
126 }
127
128 public void addDependency(Dependency dependency) {
129 checkChildrenAllowed();
130 containers.add(dependency);
131 }
132
133 public void addDependencies(Dependencies dependencies) {
134 checkChildrenAllowed();
135 if (dependencies == this) {
136 throw circularReference();
137 }
138 containers.add(dependencies);
139 nestedDependencies = true;
140 checkExternalSources();
141 }
142
143 public List<DependencyContainer> getDependencyContainers() {
144 if (isReference()) {
145 return getRef().getDependencyContainers();
146 }
147 return containers;
148 }
149
150 public void addExclusion(Exclusion exclusion) {
151 checkChildrenAllowed();
152 this.exclusions.add(exclusion);
153 }
154
155 public List<Exclusion> getExclusions() {
156 if (isReference()) {
157 return getRef().getExclusions();
158 }
159 return exclusions;
160 }
161 }