001package org.andromda.maven.plugin.cartridge.site;
002
003import java.io.File;
004import java.util.List;
005import org.andromda.maven.plugin.AndroMDAMojo;
006import org.apache.commons.lang.exception.ExceptionUtils;
007import org.apache.maven.plugin.MojoExecutionException;
008import org.apache.maven.plugin.MojoFailureException;
009import org.apache.maven.project.MavenProject;
010import org.apache.maven.settings.Settings;
011
012/**
013 * Goal that runs AndroMDA over the howto model to generate the cartridge java source files
014 * which are referenced via the cartridge xhtml docs.  It also unpacks the cartridge related
015 * archive files, such as the howto pictures for the cartridge site documentation,
016 * to target/site/howto locations preparing for deployment.
017 *
018 * @phase site
019 * @goal generate-cartridge-howto-artifacts
020 * @description Goal to run AndroMDA and generate howto source and unpack cartridge
021 * archive files prior to site deployment
022 * @author vancek
023 */
024public class GenerateCartridgeHowtoArtifactsMojo
025    extends AbstractSiteMojo
026{
027    /**
028     * Path to the cartridge howto pictures zip source.  This file's path is typically
029     * cartridge/src/site/resource/howto/HowToPictures.zip.
030     *
031     * @parameter expression="${basedir}/src/site/resources/howto/HowToPictures.zip"
032     */
033    private File howtoCartridgePicturesSourcePath;
034
035    /**
036     * Path to the cartride howto pictures destination extraction directory
037     *
038     * @parameter expression="${basedir}/target/site/howto"
039     */
040    private File howtoCartridgePicturesOutputDirectory;
041
042    /**
043     * This is the URI to the AndroMDA configuration file.
044     *
045     * @parameter expression="file:${basedir}/conf/howto/andromda.xml"
046     * @required
047     */
048    private String configurationUri;
049
050    /**
051     * @parameter expression="${project.build.filters}"
052     */
053    private List<String> propertyFiles;
054
055    /**
056     * The current user system settings for use in Maven. (allows us to pass the user
057     * settings to the AndroMDA configuration).
058     *
059     * @parameter expression="${settings}"
060     * @required
061     * @readonly
062     */
063    private Settings settings;
064
065    /**
066     * The name of the project injected from pom.xml. Not used.
067     *
068     * @parameter default-value="${project.name}"
069     */
070    @SuppressWarnings("unused")
071    private String projectName;
072
073    /**
074     * @parameter expression="${project}"
075     * @required
076     * @readonly
077     */
078    protected MavenProject project;
079
080    /**
081     * The directory where the model generation output history is located
082     * (Modelname file containing a list of files generated by that model).
083     *
084     * @parameter expression="${project.build.directory}/history"
085     */
086    private File modelOutputHistory;
087
088    /**
089     * @see org.apache.maven.plugin.Mojo#execute()
090     */
091    public void execute()
092        throws MojoExecutionException, MojoFailureException
093    {
094        this.getLog().info("---------------------------------------------------------------------------------------");
095        this.getLog().info("  A n d r o M D A   G e n e r a t e   C a r t r i d g e   H o w T o   A r t i f a c t s");
096        this.getLog().info("---------------------------------------------------------------------------------------");
097
098        this.unpackHowToPictures();
099        this.generateHowToSource();
100
101        this.getLog().info("GENERATE CARTRIDGE HOWTO ARTIFACTS SUCCESSFUL");
102    }
103
104    /**
105     * Unpack the cartridge howto pictures
106     *
107     * @throws MojoExecutionException
108     * @throws MojoFailureException
109     */
110    private void unpackHowToPictures()
111        throws MojoExecutionException, MojoFailureException
112    {
113        try
114        {
115            if (!this.howtoCartridgePicturesSourcePath.exists())
116            {
117                throw new MojoExecutionException("Cartridge howto pictures source location is invalid");
118            }
119
120            this.unpack(
121                    this.howtoCartridgePicturesSourcePath,
122                    this.howtoCartridgePicturesOutputDirectory);
123        }
124        catch (final Throwable throwable)
125        {
126            if (throwable instanceof MojoExecutionException)
127            {
128                throw (MojoExecutionException)throwable;
129            }
130            throw new MojoExecutionException("An error occurred unpacking cartridge howto pictures '" +
131                this.project.getArtifactId() + '\'',
132                ExceptionUtils.getRootCause(throwable));
133        }
134    }
135
136    /**
137     * Generate the howto source by running AndroMDA using the howto specific model
138     * and andromda.xml config.
139     *
140     * @throws MojoExecutionException
141     * @throws MojoFailureException
142     */
143    private void generateHowToSource()
144        throws MojoExecutionException, MojoFailureException
145    {
146        try
147        {
148            final AndroMDAMojo andromdaMojo = new AndroMDAMojo();
149            andromdaMojo.setConfigurationUri(this.configurationUri);
150            andromdaMojo.setProject(this.project);
151            andromdaMojo.setSettings(this.settings);
152            andromdaMojo.setPropertyFiles(this.propertyFiles);
153            andromdaMojo.setModelOutputHistory(this.modelOutputHistory);
154            andromdaMojo.execute();
155        }
156        catch (final Throwable throwable)
157        {
158            if (throwable instanceof MojoExecutionException)
159            {
160                throw (MojoExecutionException)throwable;
161            }
162            throw new MojoExecutionException(
163                    "An error occurred while running AndroMDA over the cartridge howto model '" +
164                    this.project.getArtifactId() + '\'', ExceptionUtils.getRootCause(throwable));
165        }
166    }
167}