EnvironmentMojo.java

  1. package com.soebes.maven.plugins.multienv;

  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.List;

  5. import org.apache.maven.archiver.MavenArchiver;
  6. import org.apache.maven.artifact.Artifact;
  7. import org.apache.maven.artifact.DependencyResolutionRequiredException;
  8. import org.apache.maven.plugin.MojoExecutionException;
  9. import org.apache.maven.plugin.MojoFailureException;
  10. import org.apache.maven.plugins.annotations.Component;
  11. import org.apache.maven.plugins.annotations.LifecyclePhase;
  12. import org.apache.maven.plugins.annotations.Mojo;
  13. import org.codehaus.plexus.archiver.Archiver;
  14. import org.codehaus.plexus.archiver.ArchiverException;
  15. import org.codehaus.plexus.archiver.UnArchiver;
  16. import org.codehaus.plexus.archiver.jar.JarArchiver;
  17. import org.codehaus.plexus.archiver.jar.ManifestException;
  18. import org.codehaus.plexus.archiver.manager.ArchiverManager;
  19. import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
  20. import org.codehaus.plexus.archiver.util.DefaultFileSet;

  21. /**
  22.  * This mojo will get the main artifact of the current project unpack it and use the files of the appropriate
  23.  * environment and produce new files which contain the original files plus the supplemental files which have been given
  24.  * by the configuration for each environment.
  25.  *
  26.  * @author Karl-Heinz Marbaise <a href="mailto:khmarbaise@soebes.de">khmarbaise@soebes.de</a>
  27.  */
  28. @Mojo( name = "environment", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true )
  29. public class EnvironmentMojo
  30.     extends AbstractMultiEnvMojo
  31. {

  32.     /**
  33.      * The JAR archiver needed for archiving the environments.
  34.      */
  35.     @Component( role = Archiver.class, hint = "jar" )
  36.     private JarArchiver jarArchiver;

  37.     @Component
  38.     private ArchiverManager manager;

  39.     public void execute()
  40.         throws MojoExecutionException, MojoFailureException
  41.     {
  42.         String[] identifiedEnvironments = getTheEnvironments( getSourceDirectory() );

  43.         if ( identifiedEnvironments.length == 0 )
  44.         {
  45.             getLog().warn( "No Environment directories found." );
  46.             return;
  47.         }

  48.         validateEnvironments( identifiedEnvironments );

  49.         createLoggingOutput( identifiedEnvironments );

  50.         Artifact artifact = getMavenSession().getCurrentProject().getArtifact();
  51.         String archiveExt = "zip";
  52.         if ( artifact.getFile().isFile() )
  53.         {
  54.             archiveExt = getArchiveExtensionOfTheArtifact( artifact );
  55.             getLog().info( "Selected main artifact " + artifact.getId() + " of the project for further processing." );
  56.         }
  57.         else
  58.         {
  59.             List<Artifact> attachedArtifacts = getMavenSession().getCurrentProject().getAttachedArtifacts();
  60.             if ( attachedArtifacts.size() > 1 )
  61.             {
  62.                 getLog().error( "We can not decide which attached artifact to use." );
  63.                 throw new MojoExecutionException( "We can not decide which attached artifact to be used." );
  64.             }

  65.             archiveExt = getArchiveExtensionOfTheArtifact( attachedArtifacts.get( 0 ) );
  66.             artifact = attachedArtifacts.get( 0 );
  67.             getLog().info( "Selected attached artifact " + artifact.getId() + " of the project for further processing." );
  68.         }

  69.         File unpackDirectory = createUnpackDirectory();

  70.         File resourceResult = createPluginResourceOutput();

  71.         filterResources( resourceResult );

  72.         unarchiveFile( artifact.getFile(), unpackDirectory, archiveExt );

  73.         for ( String environment : identifiedEnvironments )
  74.         {
  75.             getLog().info( "Building Environment: '" + environment + "'" );

  76.             // Check why this can happen?
  77.             if ( environment.isEmpty() )
  78.             {
  79.                 getLog().warn( "The given directory '" + environment + "' is empty." );
  80.                 continue;
  81.             }

  82.             try
  83.             {
  84.                 File targetDirectory = new File( resourceResult, environment );
  85.                 File createArchiveFile = createArchiveFile( unpackDirectory, targetDirectory, environment, archiveExt );
  86.                 getProjectHelper().attachArtifact( getMavenProject(), getMavenProject().getPackaging(), environment,
  87.                                                    createArchiveFile );
  88.             }
  89.             catch ( NoSuchArchiverException e )
  90.             {
  91.                 getLog().error( "Archive creation failed.", e );
  92.                 throw new MojoExecutionException( "Archive creation failed.", e );
  93.             }
  94.             catch ( IOException e )
  95.             {
  96.                 getLog().error( "IO Exception.", e );
  97.                 throw new MojoExecutionException( "IO Exception.", e );
  98.             }
  99.         }

  100.     }

  101.     private void unarchiveFile( File sourceFile, File destDirectory, String archiveExt )
  102.         throws MojoExecutionException
  103.     {
  104.         try
  105.         {
  106.             UnArchiver unArchiver = manager.getUnArchiver( archiveExt );

  107.             unArchiver.setSourceFile( sourceFile );
  108.             unArchiver.setUseJvmChmod( true );
  109.             unArchiver.setDestDirectory( destDirectory );
  110.             unArchiver.setOverwrite( true );
  111.             unArchiver.extract();
  112.         }
  113.         catch ( ArchiverException e )
  114.         {
  115.             throw new MojoExecutionException( "Error unpacking file [" + sourceFile.getAbsolutePath() + "]" + " to ["
  116.                 + destDirectory.getAbsolutePath() + "]", e );
  117.         }
  118.         catch ( NoSuchArchiverException e )
  119.         {
  120.             getLog().error( "Unknown archiver." + " with unknown extension [" + archiveExt + "]" );
  121.         }
  122.     }

  123.     private File createArchiveFile( File unpackDirectory, File targetDirectory, String directory, String archiveExt )
  124.         throws NoSuchArchiverException, IOException, MojoExecutionException
  125.     {
  126.         final MavenArchiver mavenArchiver = new MavenArchiver();

  127.         mavenArchiver.setArchiver( jarArchiver );

  128.         jarArchiver.addFileSet( new DefaultFileSet( targetDirectory ) );
  129.         jarArchiver.addFileSet( new DefaultFileSet( unpackDirectory ) );
  130.         // jarArchiver.setDuplicateBehavior( duplicate );

  131.         File resultArchive = getArchiveFile( getOutputDirectory(), getFinalName(), directory, archiveExt );

  132.         mavenArchiver.setOutputFile( resultArchive );
  133.         try
  134.         {
  135.             mavenArchiver.createArchive( getMavenSession(), getMavenProject(), getArchive() );
  136.         }
  137.         catch ( ArchiverException | ManifestException | DependencyResolutionRequiredException e )
  138.         {
  139.             getLog().error( e.getMessage(), e );
  140.             throw new MojoExecutionException( e.getMessage(), e );
  141.         }

  142.         return resultArchive;

  143.     }

  144. }