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 | 0 | public class EarMojo |
58 | |
extends AbstractEarMojo |
59 | |
{ |
60 | 0 | 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 | 0 | 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 | 0 | super.execute(); |
241 | |
|
242 | |
|
243 | 0 | List unpackTypesList = new ArrayList(); |
244 | 0 | if ( unpackTypes != null ) |
245 | |
{ |
246 | 0 | unpackTypesList = Arrays.asList( unpackTypes.split( "," ) ); |
247 | 0 | final Iterator it = unpackTypesList.iterator(); |
248 | 0 | while ( it.hasNext() ) |
249 | |
{ |
250 | 0 | String type = (String) it.next(); |
251 | 0 | if ( !EarModuleFactory.standardArtifactTypes.contains( type ) ) |
252 | |
{ |
253 | 0 | throw new MojoExecutionException( |
254 | |
"Invalid type[" + type + "] supported types are " + EarModuleFactory.standardArtifactTypes ); |
255 | |
} |
256 | 0 | } |
257 | 0 | getLog().debug( "Initialized unpack types " + unpackTypesList ); |
258 | |
} |
259 | |
|
260 | |
|
261 | |
try |
262 | |
{ |
263 | 0 | for ( Iterator iter = getModules().iterator(); iter.hasNext(); ) |
264 | |
{ |
265 | 0 | EarModule module = (EarModule) iter.next(); |
266 | 0 | if ( module instanceof JavaModule ) |
267 | |
{ |
268 | 0 | getLog().warn( "JavaModule is deprecated (" + module + "), please use JarModule instead." ); |
269 | |
} |
270 | 0 | if ( module instanceof Ejb3Module ) |
271 | |
{ |
272 | 0 | getLog().warn( "Ejb3Module is deprecated (" + module + "), please use EjbModule instead." ); |
273 | |
} |
274 | 0 | final File sourceFile = module.getArtifact().getFile(); |
275 | 0 | final File destinationFile = buildDestinationFile( getWorkDirectory(), module.getUri() ); |
276 | 0 | if ( !sourceFile.isFile() ) |
277 | |
{ |
278 | 0 | throw new MojoExecutionException( "Cannot copy a directory: " + sourceFile.getAbsolutePath() + |
279 | |
"; Did you package/install " + module.getArtifact() + "?" ); |
280 | |
} |
281 | |
|
282 | 0 | if ( destinationFile.getCanonicalPath().equals( sourceFile.getCanonicalPath() ) ) |
283 | |
{ |
284 | 0 | getLog().info( |
285 | |
"Skipping artifact[" + module + "], as it already exists at[" + module.getUri() + "]" ); |
286 | 0 | continue; |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | 0 | if ( ( unpackTypesList.contains( module.getType() ) && |
292 | |
( module.shouldUnpack() == null || module.shouldUnpack().booleanValue() ) ) || |
293 | |
( module.shouldUnpack() != null && module.shouldUnpack().booleanValue() ) ) |
294 | |
{ |
295 | 0 | getLog().info( "Copying artifact[" + module + "] to[" + module.getUri() + "] (unpacked)" ); |
296 | |
|
297 | 0 | destinationFile.mkdirs(); |
298 | 0 | unpack( sourceFile, destinationFile ); |
299 | |
} |
300 | |
else |
301 | |
{ |
302 | 0 | if ( sourceFile.lastModified() > destinationFile.lastModified() ) |
303 | |
{ |
304 | 0 | getLog().info( "Copying artifact[" + module + "] to[" + module.getUri() + "]" ); |
305 | 0 | FileUtils.copyFile( sourceFile, destinationFile ); |
306 | |
} |
307 | |
else |
308 | |
{ |
309 | 0 | getLog().debug( "Skipping artifact[" + module + "], as it is already up to date at[" + |
310 | |
module.getUri() + "]" ); |
311 | |
} |
312 | |
} |
313 | 0 | } |
314 | |
} |
315 | 0 | catch ( IOException e ) |
316 | |
{ |
317 | 0 | throw new MojoExecutionException( "Error copying EAR modules", e ); |
318 | |
} |
319 | 0 | catch ( ArchiverException e ) |
320 | |
{ |
321 | 0 | throw new MojoExecutionException( "Error unpacking EAR modules", e ); |
322 | |
} |
323 | 0 | catch ( NoSuchArchiverException e ) |
324 | |
{ |
325 | 0 | throw new MojoExecutionException( "No Archiver found for EAR modules", e ); |
326 | 0 | } |
327 | |
|
328 | |
|
329 | |
try |
330 | |
{ |
331 | 0 | File earSourceDir = earSourceDirectory; |
332 | 0 | if ( earSourceDir.exists() ) |
333 | |
{ |
334 | 0 | getLog().info( "Copy ear sources to " + getWorkDirectory().getAbsolutePath() ); |
335 | 0 | String[] fileNames = getEarFiles( earSourceDir ); |
336 | 0 | for ( int i = 0; i < fileNames.length; i++ ) |
337 | |
{ |
338 | 0 | copyFile( new File( earSourceDir, fileNames[i] ), |
339 | |
new File( getWorkDirectory(), fileNames[i] ) ); |
340 | |
} |
341 | |
} |
342 | |
|
343 | 0 | if ( applicationXml != null && !"".equals( applicationXml ) ) |
344 | |
{ |
345 | |
|
346 | 0 | getLog().info( "Including custom application.xml[" + applicationXml + "]" ); |
347 | 0 | File metaInfDir = new File( getWorkDirectory(), META_INF ); |
348 | 0 | copyFile( new File( applicationXml ), new File( metaInfDir, "/application.xml" ) ); |
349 | |
} |
350 | |
|
351 | |
} |
352 | 0 | catch ( IOException e ) |
353 | |
{ |
354 | 0 | throw new MojoExecutionException( "Error copying EAR sources", e ); |
355 | |
} |
356 | 0 | catch ( MavenFilteringException e ) |
357 | |
{ |
358 | 0 | throw new MojoExecutionException( "Error filetering EAR sources", e ); |
359 | 0 | } |
360 | |
|
361 | |
|
362 | |
try |
363 | |
{ |
364 | 0 | if ( resourcesDir != null && resourcesDir.exists() ) |
365 | |
{ |
366 | 0 | getLog().warn( "resourcesDir is deprecated. Please use the earSourceDirectory property instead."); |
367 | 0 | getLog().info( "Copy ear resources to " + getWorkDirectory().getAbsolutePath() ); |
368 | 0 | String[] fileNames = getEarFiles( resourcesDir ); |
369 | 0 | for ( int i = 0; i < fileNames.length; i++ ) |
370 | |
{ |
371 | 0 | FileUtils.copyFile( new File( resourcesDir, fileNames[i] ), |
372 | |
new File( getWorkDirectory(), fileNames[i] ) ); |
373 | |
} |
374 | |
} |
375 | |
} |
376 | 0 | catch ( IOException e ) |
377 | |
{ |
378 | 0 | throw new MojoExecutionException( "Error copying EAR resources", e ); |
379 | 0 | } |
380 | |
|
381 | |
|
382 | 0 | File ddFile = new File( getWorkDirectory(), APPLICATION_XML_URI ); |
383 | 0 | if ( !ddFile.exists() && !version.equals(VERSION_5 ) ) |
384 | |
{ |
385 | 0 | throw new MojoExecutionException( |
386 | |
"Deployment descriptor: " + ddFile.getAbsolutePath() + " does not exist." ); |
387 | |
} |
388 | |
|
389 | |
try |
390 | |
{ |
391 | 0 | File earFile = getEarFile( outputDirectory, finalName, classifier ); |
392 | 0 | MavenArchiver archiver = new MavenArchiver(); |
393 | 0 | final JarArchiver jarArchiver = getJarArchiver(); |
394 | 0 | getLog().debug( "Jar archiver implementation[" + jarArchiver.getClass().getName() + "]" ); |
395 | 0 | archiver.setArchiver( jarArchiver ); |
396 | 0 | archiver.setOutputFile( earFile ); |
397 | |
|
398 | |
|
399 | 0 | includeCustomManifestFile(); |
400 | |
|
401 | 0 | archiver.getArchiver().addDirectory( getWorkDirectory() ); |
402 | 0 | archiver.createArchive( getProject(), archive ); |
403 | |
|
404 | 0 | if ( classifier != null ) |
405 | |
{ |
406 | 0 | projectHelper.attachArtifact( getProject(), "ear", classifier, earFile ); |
407 | |
} |
408 | |
else |
409 | |
{ |
410 | 0 | getProject().getArtifact().setFile( earFile ); |
411 | |
} |
412 | |
} |
413 | 0 | catch ( Exception e ) |
414 | |
{ |
415 | 0 | throw new MojoExecutionException( "Error assembling EAR", e ); |
416 | 0 | } |
417 | 0 | } |
418 | |
|
419 | |
public String getApplicationXml() |
420 | |
{ |
421 | 0 | return applicationXml; |
422 | |
} |
423 | |
|
424 | |
public void setApplicationXml( String applicationXml ) |
425 | |
{ |
426 | 0 | this.applicationXml = applicationXml; |
427 | 0 | } |
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
protected String[] getExcludes() |
436 | |
{ |
437 | 0 | List excludeList = new ArrayList( FileUtils.getDefaultExcludesAsList() ); |
438 | 0 | if ( earSourceExcludes != null && !"".equals( earSourceExcludes ) ) |
439 | |
{ |
440 | 0 | excludeList.addAll( Arrays.asList( StringUtils.split( earSourceExcludes, "," ) ) ); |
441 | |
} |
442 | |
|
443 | |
|
444 | 0 | if ( getApplicationXml() != null && !"".equals( getApplicationXml() ) ) |
445 | |
{ |
446 | 0 | excludeList.add( "**/" + META_INF + "/application.xml" ); |
447 | |
} |
448 | |
|
449 | 0 | return (String[]) excludeList.toArray( EMPTY_STRING_ARRAY ); |
450 | |
} |
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
protected String[] getIncludes() |
459 | |
{ |
460 | 0 | return StringUtils.split( StringUtils.defaultString( earSourceIncludes ), "," ); |
461 | |
} |
462 | |
|
463 | |
private static File buildDestinationFile( File buildDir, String uri ) |
464 | |
{ |
465 | 0 | return new File( buildDir, uri ); |
466 | |
} |
467 | |
|
468 | |
private void includeCustomManifestFile() |
469 | |
{ |
470 | 0 | File customManifestFile = manifestFile; |
471 | |
|
472 | 0 | if ( !customManifestFile.exists() ) |
473 | |
{ |
474 | 0 | getLog().info( "Could not find manifest file: " + manifestFile + " - Generating one" ); |
475 | |
} |
476 | |
else |
477 | |
{ |
478 | 0 | getLog().info( "Including custom manifest file[" + customManifestFile + "]" ); |
479 | 0 | archive.setManifestFile( customManifestFile ); |
480 | |
} |
481 | 0 | } |
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
private static File getEarFile( String basedir, String finalName, String classifier ) |
492 | |
{ |
493 | 0 | if ( classifier == null ) |
494 | |
{ |
495 | 0 | classifier = ""; |
496 | |
} |
497 | 0 | else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) ) |
498 | |
{ |
499 | 0 | classifier = "-" + classifier; |
500 | |
} |
501 | |
|
502 | 0 | return new File( basedir, finalName + classifier + ".ear" ); |
503 | |
} |
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
private String[] getEarFiles( File sourceDir ) |
513 | |
{ |
514 | 0 | DirectoryScanner scanner = new DirectoryScanner(); |
515 | 0 | scanner.setBasedir( sourceDir ); |
516 | 0 | scanner.setExcludes( getExcludes() ); |
517 | 0 | scanner.addDefaultExcludes(); |
518 | |
|
519 | 0 | scanner.setIncludes( getIncludes() ); |
520 | |
|
521 | 0 | scanner.scan(); |
522 | |
|
523 | 0 | 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 | 0 | UnArchiver unArchiver = archiverManager.getUnArchiver( "zip" ); |
536 | 0 | unArchiver.setSourceFile( source ); |
537 | 0 | unArchiver.setDestDirectory( destDir ); |
538 | |
|
539 | |
|
540 | 0 | unArchiver.extract(); |
541 | 0 | } |
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
protected JarArchiver getJarArchiver() |
552 | |
{ |
553 | 0 | return jarArchiver; |
554 | |
} |
555 | |
|
556 | |
private void copyFile(File source, File target) |
557 | |
throws MavenFilteringException, IOException, MojoExecutionException { |
558 | 0 | if ( filtering && !isNonFilteredExtension( source.getName() ) ) |
559 | |
{ |
560 | 0 | mavenFileFilter.copyFile( source, target, true, getFilterWrappers(), null ); |
561 | |
} |
562 | |
else |
563 | |
{ |
564 | 0 | FileUtils.copyFile(source,target); |
565 | |
} |
566 | 0 | } |
567 | |
|
568 | |
public boolean isNonFilteredExtension( String fileName ) |
569 | |
{ |
570 | 0 | return !mavenResourcesFiltering.filteredFileExtension( fileName, nonFilteredFileExtensions ); |
571 | |
} |
572 | |
|
573 | |
private List getFilterWrappers() throws MojoExecutionException { |
574 | 0 | if (filterWrappers == null) |
575 | |
{ |
576 | |
try |
577 | |
{ |
578 | 0 | MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(); |
579 | 0 | mavenResourcesExecution.setEscapeString( escapeString ); |
580 | 0 | filterWrappers = mavenFileFilter.getDefaultFilterWrappers( project, filters, |
581 | |
escapedBackslashesInFilePath, this.session, mavenResourcesExecution ); |
582 | |
} |
583 | 0 | catch ( MavenFilteringException e ) |
584 | |
{ |
585 | 0 | getLog().error( "fail to build filering wrappers " + e.getMessage() ); |
586 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
587 | 0 | } |
588 | |
} |
589 | 0 | return filterWrappers; |
590 | |
} |
591 | |
} |