BaseArchiveMojo.java

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

  2. import org.apache.commons.io.FileUtils;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.apache.maven.artifact.Artifact;
  5. import org.apache.maven.plugin.AbstractMojo;
  6. import org.apache.maven.project.MavenProject;
  7. import org.apache.maven.project.MavenProjectHelper;
  8. import org.codehaus.plexus.archiver.manager.ArchiverManager;

  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.util.Iterator;

  12. /**
  13.  * Base class for all ArchiverMojos
  14.  *
  15.  * @author Plushnikov Michail
  16.  * @version $Id: $
  17.  * @since 3.4
  18.  */
  19. public abstract class BaseArchiveMojo extends AbstractMojo
  20. {
  21.     /**
  22.      * Single directory that contains the model
  23.      *
  24.      * @parameter expression="${basedir}/src/main/uml"
  25.      * @required
  26.      */
  27.     protected File modelSourceDirectory;
  28.     /**
  29.      * Directory that resources are copied to during the build.
  30.      *
  31.      * @parameter expression="${project.build.directory}"
  32.      * @required
  33.      */
  34.     protected File workDirectory;
  35.     /**
  36.      * The directory for the generated emx.
  37.      *
  38.      * @parameter expression="${project.build.outputDirectory}"
  39.      * @required
  40.      */
  41.     protected File outputDirectory;
  42.     /**
  43.      * The name of the emx file to generate.
  44.      *
  45.      * @parameter alias="modelName" expression="${project.build.finalName}"
  46.      * @required
  47.      * @readonly
  48.      */
  49.     protected String finalName;
  50.     /**
  51.      * The maven project.
  52.      *
  53.      * @parameter expression="${project}"
  54.      * @required
  55.      * @readonly
  56.      * @description "the maven project to use"
  57.      */
  58.     protected MavenProject project;

  59.     /**
  60.      * To look up Archiver/UnArchiver implementations
  61.      *
  62.      * @component role="org.codehaus.plexus.archiver.manager.ArchiverManager"
  63.      * @required
  64.      */
  65.     protected ArchiverManager archiverManager;

  66.     /**
  67.      * The maven project's helper.
  68.      *
  69.      * @component role="org.apache.maven.project.MavenProjectHelper"
  70.      * @required
  71.      * @readonly
  72.      */
  73.     protected MavenProjectHelper projectHelper;

  74.     /**
  75.      * Whether or not to do replacement of embedded model HREF reference extensions.
  76.      *
  77.      * @parameter expression=false
  78.      * @required
  79.      */
  80.     protected boolean replaceExtensions;

  81.     /**
  82.      * Deletes all files with given extension in the given directory
  83.      *
  84.      * @param pPath      path to directory
  85.      * @param pExtension extension of files
  86.      */
  87.     protected void deleteFiles(String pPath, String pExtension)
  88.     {
  89.         Iterator<File> lFileIter = FileUtils.iterateFiles(new File(pPath), new String[]{pExtension}, false);
  90.         while (lFileIter.hasNext())
  91.         {
  92.             FileUtils.deleteQuietly(lFileIter.next());
  93.         }
  94.     }

  95.     /**
  96.      * Escapes the pattern so that the reserved regular expression
  97.      * characters are used literally.
  98.      *
  99.      * @param pattern the pattern to replace.
  100.      * @return the resulting pattern.
  101.      */
  102.     protected String escapePattern(String pattern)
  103.     {
  104.         pattern = StringUtils.replace(
  105.                 pattern,
  106.                 ".",
  107.                 "\\.");
  108.         pattern = StringUtils.replace(
  109.                 pattern,
  110.                 "-",
  111.                 "\\-");
  112.         return pattern;
  113.     }

  114.     /**
  115.      * Replace all extensions in the file
  116.      *
  117.      * @param pReplacementExtensions Extensions to replace
  118.      * @param pFile                  File where all Extensions should be replaced
  119.      * @throws java.io.IOException Exception on IO-Error
  120.      */
  121.     protected void replaceExtensions(String pReplacementExtensions, File pFile) throws IOException
  122.     {
  123.         String[] replacementExtensions = pReplacementExtensions != null ? pReplacementExtensions.split(",\\s*") : new String[0];

  124.         final String version = escapePattern(this.project.getVersion());
  125.         String contents = FileUtils.readFileToString(pFile);
  126.         for (String replacementExtension : replacementExtensions)
  127.         {
  128.             final String extension = escapePattern(replacementExtension);
  129.             // add ' to extension, to match only href elements (example: href='abcdefg.xml')
  130.             // and not abc.xml.efg substrings
  131.             final String extensionPattern = "((\\-" + version + ")?)" + extension + "(['\"|])";
  132.             final String newExtension = "\\-" + version + extension + "$3";
  133.             contents = contents.replaceAll(
  134.                     extensionPattern,
  135.                     newExtension);
  136.             // Fix replacement error for standard UML profiles which follow the _Profile. naming convention.
  137.             contents =
  138.                     contents.replaceAll(
  139.                             "_Profile\\-" + version,
  140.                             "_Profile");
  141.         }
  142.         FileUtils.writeStringToFile(pFile, contents);
  143.     }

  144.     /**
  145.      * Sets File for current Artifact
  146.      * @param newFile File to set for current Artifact
  147.      */
  148.     protected void setArtifactFile(File newFile)
  149.     {
  150.         final Artifact artifact = this.project.getArtifact();
  151.         // - set the artifact file to the correct file
  152.         artifact.setFile(newFile);
  153.         getLog().debug("File artifact set " + newFile.getAbsolutePath() + " packaging " + project.getPackaging());
  154.     }
  155. }