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