    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.release.plugin.mojos;
    018
    019import java.io.File;
    020import java.io.IOException;
    021import java.io.InputStream;
    022import java.io.OutputStream;
    023import java.nio.file.Files;
    024import java.util.ArrayList;
    025import java.util.List;
    026import java.util.zip.ZipEntry;
    027import java.util.zip.ZipOutputStream;
    028
    029import org.apache.commons.io.IOUtils;
    030import org.apache.commons.lang3.StringUtils;
    031import org.apache.maven.plugin.AbstractMojo;
    032import org.apache.maven.plugin.MojoExecutionException;
    033import org.apache.maven.plugin.MojoFailureException;
    034import org.apache.maven.plugins.annotations.LifecyclePhase;
    035import org.apache.maven.plugins.annotations.Mojo;
    036import org.apache.maven.plugins.annotations.Parameter;
    037
    038/**
    039 * Takes the built <code>./target/site</code> directory and compresses it to
    040 * <code>./target/commons-release-plugin/site.zip</code>.
    041 *
    042 * @since 1.0
    043 * @deprecated - as we no longer wish to compress the site, we are going to put this functionality in the
    044 *               {@link CommonsDistributionStagingMojo}.
    045 */
    046@Deprecated
    047@Mojo(name = "compress-site",
    048        defaultPhase = LifecyclePhase.POST_SITE,
    049        threadSafe = true,
    050        aggregator = true)
    051public class CommonsSiteCompressionMojo extends AbstractMojo {
    052
    053    /**
    054     * The working directory for the plugin which, assuming the maven uses the default
    055     * <code>${project.build.directory}</code>, this becomes <code>target/commons-release-plugin</code>.
    056     */
    057    @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin",
    058            property = "commons.outputDirectory")
    059    private File workingDirectory;
    060
    061    /**
    062     * The site output directory.
    063     */
    064    @Parameter(defaultValue = "${project.build.directory}/site", property = "commons.siteOutputDirectory")
    065    private File siteDirectory;
    066
    067    /**
    068     * The url of the subversion repository to which we wish the artifacts to be staged. Typically
    069     * this would need to be of the form:
    070     * <code>scm:svn:https://dist.apache.org/repos/dist/dev/commons/foo</code>. Note. that the prefix to the
    071     * substring <code>https</code> is a requirement.
    072     */
    073    @Parameter(defaultValue = "", property = "commons.distSvnStagingUrl")
    074    private String distSvnStagingUrl;
    075
    076    /**
    077     * A parameter to generally avoid running unless it is specifically turned on by the consuming module.
    078     */
    079    @Parameter(defaultValue = "false", property = "commons.release.isDistModule")
    080    private Boolean isDistModule;
    081
    082    /**
    083     * The list of files to compress into the site.zip file.
    084     */
    085    private List<File> filesToCompress;
    086
    087    /**
    088     * Constructs a new instance.
    089     */
    090    public CommonsSiteCompressionMojo() {
    091        // empty
    092    }
    093
    094    /**
    095     * Given the <code>directoryToZip</code> we add the <code>file</code> to the ZIP archive represented by
    096     * <code>zos</code>.
    097     *
    098     * @param directoryToZip a {@link File} representing the directory from which the file exists that we are
    099     *                       compressing. Generally this is <code>target/site</code>.
    100     * @param file a {@link File} to add to the {@link ZipOutputStream} <code>zos</code>.
    101     * @param zos the {@link ZipOutputStream} to which to add our <code>file</code>.
    102     * @throws IOException if adding the <code>file</code> doesn't work out properly.
    103     */
    104    private void addToZip(final File directoryToZip, final File file, final ZipOutputStream zos) throws IOException {
    105        try (InputStream fis = Files.newInputStream(file.toPath())) {
    106            // we want the zipEntry's path to be a relative path that is relative
    107            // to the directory being zipped, so chop off the rest of the path
    108            final String zipFilePath = file.getCanonicalPath().substring(
    109                    directoryToZip.getCanonicalPath().length() + 1,
    110                    file.getCanonicalPath().length());
    111            final ZipEntry zipEntry = new ZipEntry(zipFilePath);
    112            zos.putNextEntry(zipEntry);
    113            IOUtils.copy(fis, zos);
    114        }
    115    }
    116
    117    @Override
    118    public void execute() throws MojoExecutionException, MojoFailureException {
    119        if (!isDistModule) {
    120            getLog().info("This module is marked as a non distribution "
    121                    + "or assembly module, and the plugin will not run.");
    122            return;
    123        }
    124        if (StringUtils.isEmpty(distSvnStagingUrl)) {
    125            getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
    126            return;
    127        }
    128        if (!siteDirectory.exists()) {
    129            getLog().error("\"mvn site\" was not run before this goal, or a siteDirectory did not exist.");
    130            throw new MojoFailureException(
    131                    "\"mvn site\" was not run before this goal, or a siteDirectory did not exist."
    132            );
    133        }
    134        if (!workingDirectory.exists()) {
    135            getLog().info("Current project contains no distributions. Not executing.");
    136            return;
    137        }
    138        try {
    139            filesToCompress = new ArrayList<>();
    140            getAllSiteFiles(siteDirectory, filesToCompress);
    141            writeZipFile(workingDirectory, siteDirectory, filesToCompress);
    142        } catch (final IOException e) {
    143            getLog().error("Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(), e);
    144            throw new MojoExecutionException(
    145                    "Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(),
    146                    e
    147            );
    148        }
    149    }
    150
    151    /**
    152     * By default this method iterates across the <code>target/site</code> directory and adds all the files
    153     * to the {@link CommonsSiteCompressionMojo#filesToCompress} {@link List}.
    154     *
    155     * @param siteDirectory the {@link File} that represents the <code>target/site</code> directory.
    156     * @param filesToCompress the {@link List} to which to add all the files.
    157     */
    158    private void getAllSiteFiles(final File siteDirectory, final List<File> filesToCompress) {
    159        final File[] files = siteDirectory.listFiles();
    160        for (final File file : files) {
    161            filesToCompress.add(file);
    162            if (file.isDirectory()) {
    163                getAllSiteFiles(file, filesToCompress);
    164            }
    165        }
    166    }
    167
    168    /**
    169     * A helper method for writing all the files in our <code>fileList</code> to a <code>site.zip</code> file
    170     * in the <code>workingDirectory</code>.
    171     *
    172     * @param outputDirectory is a {@link File} representing the place to put the site.zip file.
    173     * @param directoryToZip is a {@link File} representing the directory of the site (normally
    174     *                       <code>target/site</code>).
    175     * @param fileList the list of files to be zipped up, generally generated by
    176     *                 {@link CommonsSiteCompressionMojo#getAllSiteFiles(File, List)}.
    177     * @throws IOException when the copying of the files goes incorrectly.
    178     */
    179    private void writeZipFile(final File outputDirectory, final File directoryToZip, final List<File> fileList)
    180            throws IOException {
    181        try (OutputStream fos = Files.newOutputStream(new File(outputDirectory.getAbsolutePath() + "/site.zip")
    182                .toPath());
    183             ZipOutputStream zos = new ZipOutputStream(fos)) {
    184            for (final File file : fileList) {
    185                if (!file.isDirectory()) { // we only ZIP files, not directories
    186                    addToZip(directoryToZip, file, zos);
    187                }
    188            }
    189        }
    190    }
    191}