001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2012-2014 ForgeRock AS
015 */
016
017package org.forgerock.doc.maven.release;
018
019import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
020import static org.twdata.maven.mojoexecutor.MojoExecutor.name;
021
022import org.apache.commons.io.FileUtils;
023import org.apache.maven.plugin.MojoExecutionException;
024import org.forgerock.doc.maven.AbstractDocbkxMojo;
025import org.forgerock.doc.maven.AbstractDocbkxMojo.Format;
026import org.twdata.maven.mojoexecutor.MojoExecutor;
027
028import java.io.File;
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * Lay out built documents,
035 * by default under {@code ${project.build.directory}/release/version}.
036 *
037 * <p>
038 *
039 * Currently only HTML and PDF are released.
040 */
041public class Layout {
042
043    /**
044     * The Mojo that holds configuration and related methods.
045     */
046    private AbstractDocbkxMojo m;
047
048    /**
049     * The Executor to run the resources plugin.
050     */
051    private final Executor executor;
052
053    /**
054     * Constructor setting the Mojo that holds the configuration.
055     *
056     * @param mojo The Mojo that holds the configuration.
057     */
058    public Layout(final AbstractDocbkxMojo mojo) {
059        m = mojo;
060        this.executor = new Executor();
061    }
062
063    /**
064     * Lay out built documents.
065     *
066     * @throws MojoExecutionException Failed to layout site.
067     */
068    public void execute() throws MojoExecutionException {
069        executor.layout();
070    }
071
072    /**
073     * Get element specifying built documents to copy to the release directory.
074     *
075     * <p>
076     *
077     * Currently only HTML and PDF are released.
078     *
079     * @return Compound element specifying built documents to copy.
080     * @throws MojoExecutionException Something went wrong getting document names.
081     */
082    private MojoExecutor.Element getResources() throws MojoExecutionException {
083
084        ArrayList<MojoExecutor.Element> r = new ArrayList<MojoExecutor.Element>();
085        final List<Format> formats = m.getFormats();
086        final String outputDir = m.path(m.getDocbkxOutputDirectory());
087
088        if (formats.contains(Format.html)) {
089            r.add(element(name("resource"),
090                    element(name("directory"), outputDir + "/html/")));
091        }
092
093        if (formats.contains(Format.pdf)) {
094            r.add(element(name("resource"),
095                    element(name("directory"), outputDir + "/pdf/"),
096                    element(name("includes"),
097                            element(name("include"), "**/*.pdf"))));
098        }
099
100        return element("resources", r.toArray(new MojoExecutor.Element[r.size()]));
101    }
102
103    /**
104     * Enclose methods to run plugins.
105     */
106    class Executor extends MojoExecutor {
107
108        /**
109         * Lay out built documents.
110         *
111         * @throws MojoExecutionException Failed to lay out documents.
112         */
113        public void layout() throws MojoExecutionException {
114            final File outputDir = new File(m.getReleaseDirectory(), m.getReleaseVersion());
115
116            executeMojo(
117                    plugin(
118                            groupId("org.apache.maven.plugins"),
119                            artifactId("maven-resources-plugin"),
120                            version(m.getMavenResourcesVersion())),
121                    goal("copy-resources"),
122                    configuration(
123                            element(name("encoding"), "UTF-8"),
124                            element(name("outputDirectory"), m.path(outputDir)),
125                            getResources()),
126                    executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
127
128
129            // Optionally copy an entire directory of arbitrary resources, too.
130            if (m.doCopyResourceFiles() && m.getResourcesDirectory().exists()) {
131                try {
132                    FileUtils.copyDirectoryToDirectory(m.getResourcesDirectory(), outputDir);
133                } catch (IOException e) {
134                    throw new MojoExecutionException("Failed to copy resources", e);
135                }
136            }
137        }
138    }
139}