1 | |
package org.apache.maven.plugin.ejb; |
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.archiver.MavenArchiveConfiguration; |
23 | |
import org.apache.maven.archiver.MavenArchiver; |
24 | |
import org.apache.maven.artifact.DependencyResolutionRequiredException; |
25 | |
import org.apache.maven.execution.MavenSession; |
26 | |
import org.apache.maven.plugin.AbstractMojo; |
27 | |
import org.apache.maven.plugin.MojoExecutionException; |
28 | |
import org.apache.maven.project.MavenProject; |
29 | |
import org.apache.maven.project.MavenProjectHelper; |
30 | |
import org.apache.maven.shared.filtering.MavenFileFilter; |
31 | |
import org.apache.maven.shared.filtering.MavenFilteringException; |
32 | |
import org.apache.maven.shared.filtering.MavenResourcesExecution; |
33 | |
import org.codehaus.plexus.archiver.ArchiverException; |
34 | |
import org.codehaus.plexus.archiver.jar.JarArchiver; |
35 | |
import org.codehaus.plexus.archiver.jar.ManifestException; |
36 | |
import org.codehaus.plexus.util.FileUtils; |
37 | |
import org.codehaus.plexus.util.xml.XmlStreamReader; |
38 | |
|
39 | |
import java.io.File; |
40 | |
import java.io.IOException; |
41 | |
import java.util.List; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 15 | public class EjbMojo |
54 | |
extends AbstractMojo |
55 | |
{ |
56 | |
private static final String EJB_JAR_XML = "META-INF/ejb-jar.xml"; |
57 | |
|
58 | |
|
59 | 1 | private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"}; |
60 | |
|
61 | 1 | private static final String[] DEFAULT_EXCLUDES = new String[]{EJB_JAR_XML, "**/package.html"}; |
62 | |
|
63 | 1 | private static final String[] DEFAULT_CLIENT_EXCLUDES = |
64 | |
new String[]{"**/*Bean.class", "**/*CMP.class", "**/*Session.class", "**/package.html"}; |
65 | |
|
66 | 1 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private File basedir; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private File outputDirectory; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private String jarName; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private String classifier; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
private boolean generateClient; |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private List clientExcludes; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private List clientIncludes; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
private List excludes; |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
private MavenProject project; |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private JarArchiver jarArchiver; |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
private String ejbVersion; |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
private JarArchiver clientJarArchiver; |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
private MavenProjectHelper projectHelper; |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | 15 | private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private boolean escapeBackslashesInFilePath; |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
protected String escapeString; |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
private boolean filterDeploymentDescriptor; |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
private List filters; |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
private MavenFileFilter mavenFileFilter; |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
private MavenSession session; |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
public void execute() |
260 | |
throws MojoExecutionException |
261 | |
{ |
262 | 14 | if ( getLog().isInfoEnabled() ) |
263 | |
{ |
264 | 14 | getLog().info( "Building EJB " + jarName + " with EJB version " + ejbVersion ); |
265 | |
} |
266 | |
|
267 | 14 | File jarFile = getEJBJarFile( basedir, jarName, classifier ); |
268 | |
|
269 | 14 | MavenArchiver archiver = new MavenArchiver(); |
270 | |
|
271 | 14 | archiver.setArchiver( jarArchiver ); |
272 | |
|
273 | 14 | archiver.setOutputFile( jarFile ); |
274 | |
|
275 | 14 | File deploymentDescriptor = new File( outputDirectory, EJB_JAR_XML ); |
276 | |
|
277 | |
|
278 | 14 | if ( !ejbVersion.matches( "\\A[2-3]\\.[0-9]\\z" ) ) |
279 | |
{ |
280 | 0 | throw new MojoExecutionException( |
281 | |
"ejbVersion is not valid: " + ejbVersion + ". Must be 2.x or 3.x (where x is a digit)" ); |
282 | |
} |
283 | |
|
284 | 14 | if ( ejbVersion.matches( "\\A2\\.[0-9]\\z" ) && !deploymentDescriptor.exists() ) |
285 | |
{ |
286 | 1 | throw new MojoExecutionException( |
287 | |
"Error assembling EJB: " + EJB_JAR_XML + " is required for ejbVersion 2.x" ); |
288 | |
} |
289 | |
|
290 | |
try |
291 | |
{ |
292 | 13 | String[] mainJarExcludes = DEFAULT_EXCLUDES; |
293 | |
|
294 | 13 | if ( excludes != null && !excludes.isEmpty() ) |
295 | |
{ |
296 | 1 | excludes.add( EJB_JAR_XML ); |
297 | 1 | mainJarExcludes = (String[]) excludes.toArray( EMPTY_STRING_ARRAY ); |
298 | |
} |
299 | |
|
300 | 13 | archiver.getArchiver().addDirectory( outputDirectory, DEFAULT_INCLUDES, mainJarExcludes ); |
301 | |
|
302 | 13 | if ( deploymentDescriptor.exists() ) |
303 | |
{ |
304 | |
|
305 | 12 | if ( filterDeploymentDescriptor ) |
306 | |
{ |
307 | 0 | getLog().debug( "Filtering deployment descriptor." ); |
308 | 0 | MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(); |
309 | 0 | mavenResourcesExecution.setEscapeString( escapeString ); |
310 | 0 | List filterWrappers = mavenFileFilter.getDefaultFilterWrappers( project, filters, |
311 | |
escapeBackslashesInFilePath, |
312 | |
this.session, |
313 | |
mavenResourcesExecution ); |
314 | |
|
315 | |
|
316 | 0 | File unfilteredDeploymentDescriptor = new File( outputDirectory, EJB_JAR_XML + ".unfiltered" ); |
317 | 0 | FileUtils.copyFile( deploymentDescriptor, unfilteredDeploymentDescriptor ); |
318 | 0 | mavenFileFilter.copyFile( unfilteredDeploymentDescriptor, deploymentDescriptor, true, |
319 | |
filterWrappers, getEncoding( unfilteredDeploymentDescriptor ) ); |
320 | |
|
321 | 0 | FileUtils.forceDelete( unfilteredDeploymentDescriptor ); |
322 | |
} |
323 | 12 | archiver.getArchiver().addFile( deploymentDescriptor, EJB_JAR_XML ); |
324 | |
} |
325 | |
|
326 | |
|
327 | 13 | archiver.createArchive( project, archive ); |
328 | |
} |
329 | 0 | catch ( ArchiverException e ) |
330 | |
{ |
331 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
332 | |
} |
333 | 0 | catch ( ManifestException e ) |
334 | |
{ |
335 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
336 | |
} |
337 | 0 | catch ( IOException e ) |
338 | |
{ |
339 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
340 | |
} |
341 | 0 | catch ( DependencyResolutionRequiredException e ) |
342 | |
{ |
343 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
344 | |
} |
345 | 0 | catch ( MavenFilteringException e ) |
346 | |
{ |
347 | 0 | throw new MojoExecutionException( "There was a problem filtering the deployment descriptor: " + e.getMessage(), e ); |
348 | 13 | } |
349 | |
|
350 | |
|
351 | 13 | if ( classifier != null ) |
352 | |
{ |
353 | 2 | projectHelper.attachArtifact( project, "ejb", classifier, jarFile ); |
354 | |
} |
355 | |
else |
356 | |
{ |
357 | 11 | project.getArtifact().setFile( jarFile ); |
358 | |
} |
359 | |
|
360 | 13 | if ( generateClient ) |
361 | |
{ |
362 | 8 | String clientJarName = jarName; |
363 | 8 | if ( classifier != null ) |
364 | |
{ |
365 | 1 | clientJarName += "-" + classifier; |
366 | |
} |
367 | |
|
368 | 8 | getLog().info( "Building EJB client " + clientJarName + "-client" ); |
369 | |
|
370 | 8 | String[] excludes = DEFAULT_CLIENT_EXCLUDES; |
371 | 8 | String[] includes = DEFAULT_INCLUDES; |
372 | |
|
373 | 8 | if ( clientIncludes != null && !clientIncludes.isEmpty() ) |
374 | |
{ |
375 | 2 | includes = (String[]) clientIncludes.toArray( EMPTY_STRING_ARRAY ); |
376 | |
} |
377 | |
|
378 | 8 | if ( clientExcludes != null && !clientExcludes.isEmpty() ) |
379 | |
{ |
380 | 2 | excludes = (String[]) clientExcludes.toArray( EMPTY_STRING_ARRAY ); |
381 | |
} |
382 | |
|
383 | 8 | File clientJarFile = new File( basedir, clientJarName + "-client.jar" ); |
384 | |
|
385 | 8 | MavenArchiver clientArchiver = new MavenArchiver(); |
386 | |
|
387 | 8 | clientArchiver.setArchiver( clientJarArchiver ); |
388 | |
|
389 | 8 | clientArchiver.setOutputFile( clientJarFile ); |
390 | |
|
391 | |
try |
392 | |
{ |
393 | 8 | clientArchiver.getArchiver().addDirectory( outputDirectory, includes, excludes ); |
394 | |
|
395 | |
|
396 | 8 | clientArchiver.createArchive( project, archive ); |
397 | |
|
398 | |
} |
399 | 0 | catch ( ArchiverException e ) |
400 | |
{ |
401 | 0 | throw new MojoExecutionException( |
402 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
403 | |
} |
404 | 0 | catch ( ManifestException e ) |
405 | |
{ |
406 | 0 | throw new MojoExecutionException( |
407 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
408 | |
} |
409 | 0 | catch ( IOException e ) |
410 | |
{ |
411 | 0 | throw new MojoExecutionException( |
412 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
413 | |
} |
414 | 0 | catch ( DependencyResolutionRequiredException e ) |
415 | |
{ |
416 | 0 | throw new MojoExecutionException( |
417 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
418 | 8 | } |
419 | |
|
420 | |
|
421 | 8 | if ( classifier != null ) |
422 | |
{ |
423 | 1 | projectHelper.attachArtifact( project, "ejb-client", classifier + "-client", clientJarFile ); |
424 | |
} |
425 | |
else |
426 | |
{ |
427 | 7 | projectHelper.attachArtifact( project, "ejb-client", "client", clientJarFile ); |
428 | |
} |
429 | |
} |
430 | 13 | } |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
private static File getEJBJarFile( File basedir, String finalName, String classifier ) |
441 | |
{ |
442 | 14 | if ( classifier == null ) |
443 | |
{ |
444 | 12 | classifier = ""; |
445 | |
} |
446 | 2 | else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) ) |
447 | |
{ |
448 | 2 | classifier = "-" + classifier; |
449 | |
} |
450 | |
|
451 | 14 | return new File( basedir, finalName + classifier + ".jar" ); |
452 | |
} |
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
private String getEncoding( File xmlFile ) |
462 | |
throws IOException |
463 | |
{ |
464 | 0 | XmlStreamReader xmlReader = new XmlStreamReader( xmlFile ); |
465 | 0 | return xmlReader.getEncoding(); |
466 | |
} |
467 | |
|
468 | |
} |