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.util.HashMap;
23 import java.util.Map;
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.types.Artifact;
28 import org.apache.maven.resolver.internal.ant.types.Artifacts;
29 import org.apache.maven.resolver.internal.ant.types.Pom;
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.types.Reference;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public abstract class AbstractDistTask extends Task {
51
52
53
54
55 public AbstractDistTask() {
56
57 }
58
59
60
61
62 private Pom pom;
63
64
65
66
67 private Artifacts artifacts;
68
69
70
71
72
73
74
75
76 protected void validate() {
77 getArtifacts().validate(this);
78
79 final Map<String, File> duplicates = new HashMap<>();
80 for (final Artifact artifact : getArtifacts().getArtifacts()) {
81 final String key = artifact.getType() + ':' + artifact.getClassifier();
82 if ("pom:".equals(key)) {
83 throw new BuildException(
84 "You must not specify an <artifact> with type=pom" + ", please use the <pom> element instead.");
85 } else if (duplicates.containsKey(key)) {
86 throw new BuildException("You must not specify two or more artifacts with the same type ("
87 + artifact.getType() + ") and classifier (" + artifact.getClassifier() + ")");
88 } else {
89 duplicates.put(key, artifact.getFile());
90 }
91
92 validateArtifactGav(artifact);
93 }
94
95 final Pom defaultPom = AntRepoSys.getInstance(getProject()).getDefaultPom();
96 if (pom == null && defaultPom != null) {
97 log("Using default POM (" + defaultPom.getCoords() + ")", Project.MSG_INFO);
98 pom = defaultPom;
99 }
100
101 if (pom == null) {
102 throw new BuildException(
103 "You must specify the <pom file=\"...\"> element" + " to denote the descriptor for the artifacts");
104 }
105 if (pom.getFile() == null) {
106 throw new BuildException("You must specify a <pom> element that has the 'file' attribute set");
107 }
108 }
109
110
111
112
113
114
115
116 private void validateArtifactGav(final Artifact artifact) {
117 final Pom artifactPom = artifact.getPom();
118 if (artifactPom != null) {
119 final String gid;
120 final String aid;
121 final String version;
122 if (artifactPom.getFile() != null) {
123 final Model model = artifactPom.getModel(this);
124 gid = model.getGroupId();
125 aid = model.getArtifactId();
126 version = model.getVersion();
127 } else {
128 gid = artifactPom.getGroupId();
129 aid = artifactPom.getArtifactId();
130 version = artifactPom.getVersion();
131 }
132
133 final Model model = getPom().getModel(this);
134
135 if (!(model.getGroupId().equals(gid)
136 && model.getArtifactId().equals(aid)
137 && model.getVersion().equals(version))) {
138 throw new BuildException(
139 "Artifact references different pom than it would be installed with: " + artifact.toString());
140 }
141 }
142 }
143
144
145
146
147
148
149 protected Artifacts getArtifacts() {
150 if (artifacts == null) {
151 artifacts = new Artifacts();
152 artifacts.setProject(getProject());
153 }
154 return artifacts;
155 }
156
157
158
159
160
161
162 public void addArtifact(final Artifact artifact) {
163 getArtifacts().addArtifact(artifact);
164 }
165
166
167
168
169
170
171 public void addArtifacts(final Artifacts artifacts) {
172 getArtifacts().addArtifacts(artifacts);
173 }
174
175
176
177
178
179
180 public void setArtifactsRef(final Reference ref) {
181 final Artifacts artifacts = new Artifacts();
182 artifacts.setProject(getProject());
183 artifacts.setRefid(ref);
184 getArtifacts().addArtifacts(artifacts);
185 }
186
187
188
189
190
191
192 protected Pom getPom() {
193 if (pom == null) {
194 return AntRepoSys.getInstance(getProject()).getDefaultPom();
195 }
196
197 return pom;
198 }
199
200
201
202
203
204
205
206 public void addPom(final Pom pom) {
207 if (this.pom != null) {
208 throw new BuildException("You must not specify multiple <pom> elements");
209 }
210 this.pom = pom;
211 }
212
213
214
215
216
217
218
219 public void setPomRef(final Reference ref) {
220 if (this.pom != null) {
221 throw new BuildException("You must not specify multiple <pom> elements");
222 }
223 pom = new Pom();
224 pom.setProject(getProject());
225 pom.setRefid(ref);
226 }
227 }