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