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 org.apache.commons.io.FileUtils;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.forgerock.doc.maven.AbstractDocbkxMojo;
022import org.twdata.maven.mojoexecutor.MojoExecutor;
023
024import java.io.File;
025import java.io.IOException;
026import java.net.URL;
027
028/**
029 * Zip release documents if configured to do so.
030 *
031 * <p>
032 *
033 * This zips the release layout only on one level,
034 * and does not handle assembly of multiple zips
035 * into a single documentation set .zip.
036 */
037public class Zip {
038
039    /**
040     * The Mojo that holds configuration and related methods.
041     */
042    private AbstractDocbkxMojo m;
043
044    /**
045     * The Executor to run the assembly plugin.
046     */
047    private final Executor executor;
048
049    /**
050     * Constructor setting the Mojo that holds the configuration.
051     *
052     * @param mojo The Mojo that holds the configuration.
053     */
054    public Zip(final AbstractDocbkxMojo mojo) {
055        m = mojo;
056        this.executor = new Executor();
057    }
058
059    /**
060     * Zip release documents.
061     *
062     * @throws MojoExecutionException Failed to zip documents.
063     */
064    public void execute() throws MojoExecutionException {
065        executor.zip();
066    }
067
068    /**
069     * Enclose methods to run plugins.
070     */
071    class Executor extends MojoExecutor {
072
073        /**
074         * Zip release documents.
075         *
076         * @throws MojoExecutionException Failed to zip documents.
077         */
078        public void zip() throws MojoExecutionException {
079
080            if (!m.doBuildReleaseZip()) {
081                return;
082            }
083
084            final URL resource = getClass().getResource("/zip.xml");
085            final File assembly = new File(m.getBuildDirectory(), "assembly.xml");
086
087            try {
088                FileUtils.copyURLToFile(resource, assembly);
089            } catch (IOException e) {
090                throw new MojoExecutionException(e.getMessage(), e);
091            }
092
093
094            final String finalName = m.getProjectName() + "-" + m.getReleaseVersion();
095
096            executeMojo(
097                    plugin(
098                            groupId("org.apache.maven.plugins"),
099                            artifactId("maven-assembly-plugin"),
100                            version(m.getMavenAssemblyVersion())),
101                    goal("single"),
102                    configuration(
103                            element(name("finalName"), finalName),
104                            element(name("descriptors"),
105                                    element(name("descriptor"), m.path(assembly)))),
106                    executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
107        }
108    }
109}