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