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