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