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