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