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