001package org.andromda.maven.plugin.modelarchiver;
002
003import java.io.File;
004import org.apache.commons.io.FileUtils;
005import org.apache.maven.plugin.MojoExecutionException;
006
007/**
008 * Builds archived model uml files.
009 *
010 * @author Bob Fields
011 * @version $Id: $
012 * @goal uml
013 * @phase package
014 * @description builds a versioned uml
015 */
016public class UmlArchiverMojo
017        extends BaseArchiveMojo
018{
019    private static final String ARTIFACT_TYPE = "uml";
020    /**
021     * The extensions to search for when doing replacement of embedded model HREF references
022     * within the archived model file from non-versioned to versioned references.
023     *
024     * @parameter expression=".uml"
025     * @required
026     */
027    protected String replacementExtensions;
028
029    /**
030     * The pattern of the model file(s) that should be versioned.
031     *
032     * @parameter expression=".*(\\.uml)"
033     * @required
034     * @readonly
035     */
036    private String modelFilePattern;
037
038    /**
039     * <p>execute</p>
040     *
041     * @throws org.apache.maven.plugin.MojoExecutionException
042     *          if any.
043     * @see org.apache.maven.plugin.Mojo#execute()
044     */
045    public void execute()
046            throws MojoExecutionException
047    {
048        if (getLog().isDebugEnabled())
049        {
050            getLog().debug(" ======= UmlArchiverMojo settings =======");
051            getLog().debug("modelSourceDirectory[" + modelSourceDirectory + ']');
052            getLog().debug("workDirectory[" + workDirectory + ']');
053            getLog().debug("outputDirectory[" + outputDirectory + ']');
054            getLog().debug("finalName[" + finalName + ']');
055            getLog().debug("replaceExtensions[" + replaceExtensions + ']');
056        }
057
058        try
059        {
060            final File buildDirectory = this.workDirectory;
061            if (buildDirectory.exists())
062            {   // old files in directory are not automatically deleted.
063                deleteFiles(buildDirectory.getAbsolutePath(), ARTIFACT_TYPE);
064            }
065            else
066            {
067                buildDirectory.mkdirs();
068            }
069
070            if (modelSourceDirectory.exists())
071            {
072                getLog().info("Copy " + ARTIFACT_TYPE + " resources to " + buildDirectory.getAbsolutePath());
073                final File[] modelFiles = modelSourceDirectory.listFiles();
074                for (final File file : modelFiles)
075                {
076                    if (file.isFile() && file.toString().matches(this.modelFilePattern))
077                    {
078                        final File newFile =
079                                new File(buildDirectory,
080                                        this.finalName + '.' + ARTIFACT_TYPE);
081                        FileUtils.copyFile(file, newFile);
082
083                        if (replaceExtensions)
084                        {
085                            getLog().info("Replace extensions in " + newFile);
086                            replaceExtensions(this.replacementExtensions, newFile);
087                        }
088
089                        setArtifactFile(newFile);
090                    }
091                }
092            }
093        }
094        catch (final Throwable throwable)
095        {
096            throw new MojoExecutionException("Error assembling model", throwable);
097        }
098    }
099}