EmxArchiverMojo.java

  1. package org.andromda.maven.plugin.modelarchiver;

  2. import java.io.File;
  3. import org.apache.commons.io.FileUtils;
  4. import org.apache.maven.plugin.MojoExecutionException;

  5. /**
  6.  * Builds archived model emx files.
  7.  *
  8.  * @author Chad Brandon
  9.  * @version $Id: $
  10.  * @goal emx
  11.  * @phase package
  12.  * @description builds a versioned emx
  13.  */
  14. public class EmxArchiverMojo
  15.         extends BaseArchiveMojo
  16. {
  17.     private static final String ARTIFACT_TYPE = "emx";

  18.     /**
  19.      * The extensions to search for when doing replacement of embedded model HREF references
  20.      * within the archived model file from non-versioned to versioned references.
  21.      *
  22.      * @parameter expression=".emx"
  23.      * @required
  24.      */
  25.     protected String replacementExtensions;

  26.     /**
  27.      * The pattern of the model file(s) that should be versioned.
  28.      *
  29.      * @parameter expression=".*(\\.emx)"
  30.      * @required
  31.      * @readonly
  32.      */
  33.     protected String modelFilePattern;

  34.     /**
  35.      * <p>execute</p>
  36.      *
  37.      * @throws org.apache.maven.plugin.MojoExecutionException
  38.      *          if any.
  39.      * @see org.apache.maven.plugin.Mojo#execute()
  40.      */
  41.     public void execute()
  42.             throws MojoExecutionException
  43.     {
  44.         if (getLog().isDebugEnabled())
  45.         {
  46.             getLog().debug(" ======= EmxArchiverMojo settings =======");
  47.             getLog().debug("modelSourceDirectory[" + modelSourceDirectory + ']');
  48.             getLog().debug("workDirectory[" + workDirectory + ']');
  49.             getLog().debug("outputDirectory[" + outputDirectory + ']');
  50.             getLog().debug("finalName[" + finalName + ']');
  51.             getLog().debug("replaceExtensions[" + replaceExtensions + ']');
  52.         }

  53.         try
  54.         {
  55.             final File buildDirectory = this.workDirectory;
  56.             if (buildDirectory.exists())
  57.             {   // old files in directory are not automatically deleted.
  58.                 deleteFiles(buildDirectory.getAbsolutePath(), ARTIFACT_TYPE);
  59.             }
  60.             else
  61.             {
  62.                 buildDirectory.mkdirs();
  63.             }

  64.             if (modelSourceDirectory.exists())
  65.             {
  66.                 final File[] modelFiles = modelSourceDirectory.listFiles();
  67.                 for (final File file : modelFiles)
  68.                 {
  69.                     if (file.isFile() && file.toString().matches(this.modelFilePattern))
  70.                     {
  71.                         final File newFile =
  72.                                 new File(buildDirectory,
  73.                                         this.finalName + '.' + ARTIFACT_TYPE);
  74.                         getLog().info("File " + file + " copied  to " + newFile);
  75.                         FileUtils.copyFile(file, newFile);

  76.                         if (replaceExtensions)
  77.                         {
  78.                             getLog().info("Replace extensions in " + newFile);
  79.                             replaceExtensions(this.replacementExtensions, newFile);
  80.                         }

  81.                         setArtifactFile(newFile);
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         catch (final Throwable throwable)
  87.         {
  88.             throw new MojoExecutionException("Error assembling model", throwable);
  89.         }
  90.     }
  91. }