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