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.archiver.MavenArchiveConfiguration;
23 import org.apache.maven.archiver.MavenArchiver;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.project.MavenProjectHelper;
27 import org.apache.maven.shared.filtering.MavenFileFilter;
28 import org.apache.maven.shared.filtering.MavenResourcesExecution;
29 import org.apache.maven.shared.filtering.MavenFilteringException;
30 import org.apache.maven.shared.filtering.MavenResourcesFiltering;
31 import org.apache.maven.execution.MavenSession;
32 import org.codehaus.plexus.archiver.ArchiverException;
33 import org.codehaus.plexus.archiver.UnArchiver;
34 import org.codehaus.plexus.archiver.jar.JarArchiver;
35 import org.codehaus.plexus.archiver.manager.ArchiverManager;
36 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
37 import org.codehaus.plexus.util.DirectoryScanner;
38 import org.codehaus.plexus.util.FileUtils;
39 import org.codehaus.plexus.util.StringUtils;
40
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Iterator;
46 import java.util.List;
47
48
49
50
51
52
53
54
55
56
57 public class EarMojo
58 extends AbstractEarMojo
59 {
60 private static final String[] EMPTY_STRING_ARRAY = {};
61
62
63
64
65
66
67
68
69 private File earSourceDirectory;
70
71
72
73
74
75
76 private String earSourceIncludes;
77
78
79
80
81
82
83 private String earSourceExcludes;
84
85
86
87
88
89
90
91 private boolean filtering;
92
93
94
95
96
97
98
99 private List filters;
100
101
102
103
104
105
106
107
108 private List nonFilteredFileExtensions;
109
110
111
112
113
114
115
116
117 private boolean escapedBackslashesInFilePath;
118
119
120
121
122
123
124
125
126 protected String escapeString;
127
128
129
130
131
132
133 private File manifestFile;
134
135
136
137
138
139
140
141 private String applicationXml;
142
143
144
145
146
147
148
149 private String outputDirectory;
150
151
152
153
154
155
156
157 private String finalName;
158
159
160
161
162
163
164
165 private String unpackTypes;
166
167
168
169
170
171
172
173 private String classifier;
174
175
176
177
178
179
180
181 private File resourcesDir;
182
183
184
185
186
187
188 private JarArchiver jarArchiver;
189
190
191
192
193
194
195
196 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
197
198
199
200
201 private MavenProjectHelper projectHelper;
202
203
204
205
206
207
208 private ArchiverManager archiverManager;
209
210
211
212
213
214
215 private MavenFileFilter mavenFileFilter;
216
217
218
219
220
221
222 private MavenResourcesFiltering mavenResourcesFiltering;
223
224
225
226
227
228
229
230 private MavenSession session;
231
232
233 private List filterWrappers;
234
235
236 public void execute()
237 throws MojoExecutionException, MojoFailureException
238 {
239
240 super.execute();
241
242
243 List unpackTypesList = new ArrayList();
244 if ( unpackTypes != null )
245 {
246 unpackTypesList = Arrays.asList( unpackTypes.split( "," ) );
247 final Iterator it = unpackTypesList.iterator();
248 while ( it.hasNext() )
249 {
250 String type = (String) it.next();
251 if ( !EarModuleFactory.standardArtifactTypes.contains( type ) )
252 {
253 throw new MojoExecutionException(
254 "Invalid type[" + type + "] supported types are " + EarModuleFactory.standardArtifactTypes );
255 }
256 }
257 getLog().debug( "Initialized unpack types " + unpackTypesList );
258 }
259
260
261 try
262 {
263 for ( Iterator iter = getModules().iterator(); iter.hasNext(); )
264 {
265 EarModule module = (EarModule) iter.next();
266 if ( module instanceof JavaModule )
267 {
268 getLog().warn( "JavaModule is deprecated (" + module + "), please use JarModule instead." );
269 }
270 if ( module instanceof Ejb3Module )
271 {
272 getLog().warn( "Ejb3Module is deprecated (" + module + "), please use EjbModule instead." );
273 }
274 final File sourceFile = module.getArtifact().getFile();
275 final File destinationFile = buildDestinationFile( getWorkDirectory(), module.getUri() );
276 if ( !sourceFile.isFile() )
277 {
278 throw new MojoExecutionException( "Cannot copy a directory: " + sourceFile.getAbsolutePath() +
279 "; Did you package/install " + module.getArtifact() + "?" );
280 }
281
282 if ( destinationFile.getCanonicalPath().equals( sourceFile.getCanonicalPath() ) )
283 {
284 getLog().info(
285 "Skipping artifact[" + module + "], as it already exists at[" + module.getUri() + "]" );
286 continue;
287 }
288
289
290
291 if ( ( unpackTypesList.contains( module.getType() ) &&
292 ( module.shouldUnpack() == null || module.shouldUnpack().booleanValue() ) ) ||
293 ( module.shouldUnpack() != null && module.shouldUnpack().booleanValue() ) )
294 {
295 getLog().info( "Copying artifact[" + module + "] to[" + module.getUri() + "] (unpacked)" );
296
297 destinationFile.mkdirs();
298 unpack( sourceFile, destinationFile );
299 }
300 else
301 {
302 if ( sourceFile.lastModified() > destinationFile.lastModified() )
303 {
304 getLog().info( "Copying artifact[" + module + "] to[" + module.getUri() + "]" );
305 FileUtils.copyFile( sourceFile, destinationFile );
306 }
307 else
308 {
309 getLog().debug( "Skipping artifact[" + module + "], as it is already up to date at[" +
310 module.getUri() + "]" );
311 }
312 }
313 }
314 }
315 catch ( IOException e )
316 {
317 throw new MojoExecutionException( "Error copying EAR modules", e );
318 }
319 catch ( ArchiverException e )
320 {
321 throw new MojoExecutionException( "Error unpacking EAR modules", e );
322 }
323 catch ( NoSuchArchiverException e )
324 {
325 throw new MojoExecutionException( "No Archiver found for EAR modules", e );
326 }
327
328
329 try
330 {
331 File earSourceDir = earSourceDirectory;
332 if ( earSourceDir.exists() )
333 {
334 getLog().info( "Copy ear sources to " + getWorkDirectory().getAbsolutePath() );
335 String[] fileNames = getEarFiles( earSourceDir );
336 for ( int i = 0; i < fileNames.length; i++ )
337 {
338 copyFile( new File( earSourceDir, fileNames[i] ),
339 new File( getWorkDirectory(), fileNames[i] ) );
340 }
341 }
342
343 if ( applicationXml != null && !"".equals( applicationXml ) )
344 {
345
346 getLog().info( "Including custom application.xml[" + applicationXml + "]" );
347 File metaInfDir = new File( getWorkDirectory(), META_INF );
348 copyFile( new File( applicationXml ), new File( metaInfDir, "/application.xml" ) );
349 }
350
351 }
352 catch ( IOException e )
353 {
354 throw new MojoExecutionException( "Error copying EAR sources", e );
355 }
356 catch ( MavenFilteringException e )
357 {
358 throw new MojoExecutionException( "Error filetering EAR sources", e );
359 }
360
361
362 try
363 {
364 if ( resourcesDir != null && resourcesDir.exists() )
365 {
366 getLog().warn( "resourcesDir is deprecated. Please use the earSourceDirectory property instead.");
367 getLog().info( "Copy ear resources to " + getWorkDirectory().getAbsolutePath() );
368 String[] fileNames = getEarFiles( resourcesDir );
369 for ( int i = 0; i < fileNames.length; i++ )
370 {
371 FileUtils.copyFile( new File( resourcesDir, fileNames[i] ),
372 new File( getWorkDirectory(), fileNames[i] ) );
373 }
374 }
375 }
376 catch ( IOException e )
377 {
378 throw new MojoExecutionException( "Error copying EAR resources", e );
379 }
380
381
382 File ddFile = new File( getWorkDirectory(), APPLICATION_XML_URI );
383 if ( !ddFile.exists() && !version.equals(VERSION_5 ) )
384 {
385 throw new MojoExecutionException(
386 "Deployment descriptor: " + ddFile.getAbsolutePath() + " does not exist." );
387 }
388
389 try
390 {
391 File earFile = getEarFile( outputDirectory, finalName, classifier );
392 MavenArchiver archiver = new MavenArchiver();
393 final JarArchiver jarArchiver = getJarArchiver();
394 getLog().debug( "Jar archiver implementation[" + jarArchiver.getClass().getName() + "]" );
395 archiver.setArchiver( jarArchiver );
396 archiver.setOutputFile( earFile );
397
398
399 includeCustomManifestFile();
400
401 archiver.getArchiver().addDirectory( getWorkDirectory() );
402 archiver.createArchive( getProject(), archive );
403
404 if ( classifier != null )
405 {
406 projectHelper.attachArtifact( getProject(), "ear", classifier, earFile );
407 }
408 else
409 {
410 getProject().getArtifact().setFile( earFile );
411 }
412 }
413 catch ( Exception e )
414 {
415 throw new MojoExecutionException( "Error assembling EAR", e );
416 }
417 }
418
419 public String getApplicationXml()
420 {
421 return applicationXml;
422 }
423
424 public void setApplicationXml( String applicationXml )
425 {
426 this.applicationXml = applicationXml;
427 }
428
429
430
431
432
433
434
435 protected String[] getExcludes()
436 {
437 List excludeList = new ArrayList( FileUtils.getDefaultExcludesAsList() );
438 if ( earSourceExcludes != null && !"".equals( earSourceExcludes ) )
439 {
440 excludeList.addAll( Arrays.asList( StringUtils.split( earSourceExcludes, "," ) ) );
441 }
442
443
444 if ( getApplicationXml() != null && !"".equals( getApplicationXml() ) )
445 {
446 excludeList.add( "**/" + META_INF + "/application.xml" );
447 }
448
449 return (String[]) excludeList.toArray( EMPTY_STRING_ARRAY );
450 }
451
452
453
454
455
456
457
458 protected String[] getIncludes()
459 {
460 return StringUtils.split( StringUtils.defaultString( earSourceIncludes ), "," );
461 }
462
463 private static File buildDestinationFile( File buildDir, String uri )
464 {
465 return new File( buildDir, uri );
466 }
467
468 private void includeCustomManifestFile()
469 {
470 File customManifestFile = manifestFile;
471
472 if ( !customManifestFile.exists() )
473 {
474 getLog().info( "Could not find manifest file: " + manifestFile + " - Generating one" );
475 }
476 else
477 {
478 getLog().info( "Including custom manifest file[" + customManifestFile + "]" );
479 archive.setManifestFile( customManifestFile );
480 }
481 }
482
483
484
485
486
487
488
489
490
491 private static File getEarFile( String basedir, String finalName, String classifier )
492 {
493 if ( classifier == null )
494 {
495 classifier = "";
496 }
497 else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
498 {
499 classifier = "-" + classifier;
500 }
501
502 return new File( basedir, finalName + classifier + ".ear" );
503 }
504
505
506
507
508
509
510
511
512 private String[] getEarFiles( File sourceDir )
513 {
514 DirectoryScanner scanner = new DirectoryScanner();
515 scanner.setBasedir( sourceDir );
516 scanner.setExcludes( getExcludes() );
517 scanner.addDefaultExcludes();
518
519 scanner.setIncludes( getIncludes() );
520
521 scanner.scan();
522
523 return scanner.getIncludedFiles();
524 }
525
526
527
528
529
530
531
532 public void unpack( File source, File destDir )
533 throws NoSuchArchiverException, IOException, ArchiverException
534 {
535 UnArchiver unArchiver = archiverManager.getUnArchiver( "zip" );
536 unArchiver.setSourceFile( source );
537 unArchiver.setDestDirectory( destDir );
538
539
540 unArchiver.extract();
541 }
542
543
544
545
546
547
548
549
550
551 protected JarArchiver getJarArchiver()
552 {
553 return jarArchiver;
554 }
555
556 private void copyFile(File source, File target)
557 throws MavenFilteringException, IOException, MojoExecutionException {
558 if ( filtering && !isNonFilteredExtension( source.getName() ) )
559 {
560 mavenFileFilter.copyFile( source, target, true, getFilterWrappers(), null );
561 }
562 else
563 {
564 FileUtils.copyFile(source,target);
565 }
566 }
567
568 public boolean isNonFilteredExtension( String fileName )
569 {
570 return !mavenResourcesFiltering.filteredFileExtension( fileName, nonFilteredFileExtensions );
571 }
572
573 private List getFilterWrappers() throws MojoExecutionException {
574 if (filterWrappers == null)
575 {
576 try
577 {
578 MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution();
579 mavenResourcesExecution.setEscapeString( escapeString );
580 filterWrappers = mavenFileFilter.getDefaultFilterWrappers( project, filters,
581 escapedBackslashesInFilePath, this.session, mavenResourcesExecution );
582 }
583 catch ( MavenFilteringException e )
584 {
585 getLog().error( "fail to build filering wrappers " + e.getMessage() );
586 throw new MojoExecutionException( e.getMessage(), e );
587 }
588 }
589 return filterWrappers;
590 }
591 }