1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.ear;
20
21 import java.util.Set;
22
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugins.ear.util.ArtifactRepository;
26 import org.apache.maven.shared.mapping.MappingUtils;
27 import org.codehaus.plexus.interpolation.InterpolationException;
28 import org.codehaus.plexus.util.xml.XMLWriter;
29
30
31
32
33
34
35 public abstract class AbstractEarModule implements EarModule {
36
37
38
39
40 protected static final String MODULE_ELEMENT = "module";
41
42
43
44
45 protected static final String JAVA_MODULE = "java";
46
47
48
49
50 protected static final String ALT_DD = "alt-dd";
51
52 private Artifact artifact;
53
54
55
56 private String groupId;
57
58 private String artifactId;
59
60
61
62
63 protected String type;
64
65 private String classifier;
66
67
68
69
70 protected String bundleDir;
71
72
73
74
75 protected String bundleFileName;
76
77
78
79
80 protected Boolean excluded = Boolean.FALSE;
81
82 private String uri;
83
84
85
86
87 protected Boolean unpack = null;
88
89
90
91
92 protected String altDeploymentDescriptor;
93
94 private String moduleId;
95
96
97
98
99
100
101
102 protected String libDirectory;
103
104
105
106
107
108
109
110
111
112 protected boolean classPathItem;
113
114
115
116
117
118
119 protected EarExecutionContext earExecutionContext;
120
121
122
123
124 public AbstractEarModule() {}
125
126
127
128
129
130
131 public AbstractEarModule(Artifact a) {
132 this.artifact = a;
133 this.groupId = a.getGroupId();
134 this.artifactId = a.getArtifactId();
135 this.type = a.getType();
136 this.classifier = a.getClassifier();
137 this.bundleDir = null;
138 }
139
140
141
142
143 public void setEarExecutionContext(EarExecutionContext earExecutionContext) {
144 this.earExecutionContext = earExecutionContext;
145 }
146
147
148 public void resolveArtifact(Set<Artifact> artifacts) throws EarPluginException, MojoFailureException {
149
150 if (artifact == null) {
151
152 if (groupId == null || artifactId == null) {
153 throw new MojoFailureException(
154 "Could not resolve artifact[" + groupId + ":" + artifactId + ":" + getType() + "]");
155 }
156 final ArtifactRepository ar = earExecutionContext.getArtifactRepository();
157 artifact = ar.getUniqueArtifact(groupId, artifactId, getType(), classifier);
158
159 if (artifact == null) {
160 Set<Artifact> candidates = ar.getArtifacts(groupId, artifactId, getType());
161 if (candidates.size() > 1) {
162 throw new MojoFailureException("Artifact[" + this + "] has " + candidates.size()
163 + " candidates, please provide a classifier.");
164 } else {
165 throw new MojoFailureException("Artifact[" + this + "] is not a dependency of the project.");
166 }
167 }
168 }
169 }
170
171
172
173
174 public Artifact getArtifact() {
175 return artifact;
176 }
177
178
179
180
181 public String getModuleId() {
182 return moduleId;
183 }
184
185
186
187
188 public String getUri() {
189 if (uri == null) {
190 if (getBundleDir() == null) {
191 uri = getBundleFileName();
192 } else {
193 uri = getBundleDir() + getBundleFileName();
194 }
195 }
196 return uri;
197 }
198
199
200
201
202 public String getType() {
203 return type;
204 }
205
206
207
208
209
210
211 public String getGroupId() {
212 return groupId;
213 }
214
215
216
217
218
219
220 public String getArtifactId() {
221 return artifactId;
222 }
223
224
225
226
227
228
229 public String getClassifier() {
230 return classifier;
231 }
232
233
234
235
236
237
238 public String getBundleDir() {
239 bundleDir = cleanArchivePath(bundleDir);
240 return bundleDir;
241 }
242
243
244
245
246 public String getLibDir() {
247 libDirectory = cleanArchivePath(libDirectory);
248 return libDirectory;
249 }
250
251
252
253
254 public boolean isClassPathItem() {
255 return classPathItem;
256 }
257
258
259
260
261 public String getBundleFileName() {
262 if (bundleFileName == null) {
263 try {
264 String outputFileNameMapping = earExecutionContext.getOutputFileNameMapping();
265 bundleFileName = MappingUtils.evaluateFileNameMapping(outputFileNameMapping, artifact);
266 } catch (InterpolationException e) {
267
268
269
270 }
271
272
273 }
274 return bundleFileName;
275 }
276
277
278
279
280
281
282
283
284 public String getAltDeploymentDescriptor() {
285 return altDeploymentDescriptor;
286 }
287
288
289
290
291
292
293 public boolean isExcluded() {
294 return excluded;
295 }
296
297
298
299
300 public Boolean shouldUnpack() {
301 return unpack;
302 }
303
304
305
306
307
308
309
310 protected void writeAltDeploymentDescriptor(XMLWriter writer, String version) {
311 if (getAltDeploymentDescriptor() != null) {
312 writer.startElement(ALT_DD);
313 writer.writeText(getAltDeploymentDescriptor());
314 writer.endElement();
315 }
316 }
317
318
319
320
321
322
323
324 protected void startModuleElement(XMLWriter writer, Boolean generateId) {
325 writer.startElement(MODULE_ELEMENT);
326
327
328 if (getModuleId() != null) {
329 writer.addAttribute("id", getModuleId());
330 } else if (generateId) {
331
332
333
334 Artifact theArtifact = getArtifact();
335 String generatedId = theArtifact.getType().toUpperCase() + "_" + theArtifact.getGroupId() + "."
336 + theArtifact.getArtifactId();
337 if (null != theArtifact.getClassifier()
338 && theArtifact.getClassifier().trim().length() > 0) {
339 generatedId += "-" + theArtifact.getClassifier().trim();
340 }
341 writer.addAttribute("id", generatedId);
342 }
343 }
344
345
346
347
348 public String toString() {
349 StringBuilder sb = new StringBuilder();
350 sb.append(getType()).append(":").append(groupId).append(":").append(artifactId);
351 if (classifier != null) {
352 sb.append(":").append(classifier);
353 }
354 if (artifact != null) {
355 sb.append(":").append(artifact.getVersion());
356 }
357 return sb.toString();
358 }
359
360
361
362
363
364
365
366 static String cleanArchivePath(String path) {
367 if (path == null) {
368 return null;
369 }
370
371
372 path = path.replace('\\', '/');
373
374
375 if (path.startsWith("/")) {
376 path = path.substring(1, path.length());
377 }
378
379 if (path.length() > 0 && !path.endsWith("/")) {
380
381 path = path + "/";
382 }
383
384 return path;
385 }
386
387
388
389
390
391
392 void setUri(String uri) {
393 this.uri = uri;
394 }
395
396
397
398
399 public boolean changeManifestClasspath() {
400 return true;
401 }
402 }