    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 * @author chtompki
    043 * @since 1.0
    044 * @deprecated - as we no longer wish to compress the site, we are going to put this functionality in the
    045 *               {@link CommonsDistributionStagingMojo}.
    046 */
    047@Deprecated
    048@Mojo(name = "compress-site",
    049        defaultPhase = LifecyclePhase.POST_SITE,
    050        threadSafe = true,
    051        aggregator = true)
    052public class CommonsSiteCompressionMojo extends AbstractMojo {
    053
    054    /**
    055     * The working directory for the plugin which, assuming the maven uses the default
    056     * <code>${project.build.directory}</code>, this becomes <code>target/commons-release-plugin</code>.
    057     */
    058    @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin",
    059            property = "commons.outputDirectory")
    060    private File workingDirectory;
    061
    062    /**
    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. Typicallly
    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    @Override
    088    public void execute() throws MojoExecutionException, MojoFailureException {
    089        if (!isDistModule) {
    090            getLog().info("This module is marked as a non distribution "
    091                    + "or assembly module, and the plugin will not run.");
    092            return;
    093        }
    094        if (StringUtils.isEmpty(distSvnStagingUrl)) {
    095            getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
    096            return;
    097        }
    098        if (!siteDirectory.exists()) {
    099            getLog().error("\"mvn site\" was not run before this goal, or a siteDirectory did not exist.");
    100            throw new MojoFailureException(
    101                    "\"mvn site\" was not run before this goal, or a siteDirectory did not exist."
    102            );
    103        }
    104        if (!workingDirectory.exists()) {
    105            getLog().info("Current project contains no distributions. Not executing.");
    106            return;
    107        }
    108        try {
    109            filesToCompress = new ArrayList<>();
    110            getAllSiteFiles(siteDirectory, filesToCompress);
    111            writeZipFile(workingDirectory, siteDirectory, filesToCompress);
    112        } catch (final IOException e) {
    113            getLog().error("Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(), e);
    114            throw new MojoExecutionException(
    115                    "Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(),
    116                    e
    117            );
    118        }
    119    }
    120
    121    /**
    122     * By default this method iterates across the <code>target/site</code> directory and adds all of the files
    123     * to the {@link CommonsSiteCompressionMojo#filesToCompress} {@link List}.
    124     *
    125     * @param siteDirectory the {@link File} that represents the <code>target/site</code> directory.
    126     * @param filesToCompress the {@link List} to which to add all the files.
    127     */
    128    private void getAllSiteFiles(final File siteDirectory, final List<File> filesToCompress) {
    129        final File[] files = siteDirectory.listFiles();
    130        for (final File file : files) {
    131            filesToCompress.add(file);
    132            if (file.isDirectory()) {
    133                getAllSiteFiles(file, filesToCompress);
    134            }
    135        }
    136    }
    137
    138    /**
    139     * A helper method for writing all of the files in our <code>fileList</code> to a <code>site.zip</code> file
    140     * in the <code>workingDirectory</code>.
    141     *
    142     * @param outputDirectory is a {@link File} representing the place to put the site.zip file.
    143     * @param directoryToZip is a {@link File} representing the directory of the site (normally
    144     *                       <code>target/site</code>).
    145     * @param fileList the list of files to be zipped up, generally generated by
    146     *                 {@link CommonsSiteCompressionMojo#getAllSiteFiles(File, List)}.
    147     * @throws IOException when the copying of the files goes incorrectly.
    148     */
    149    private void writeZipFile(final File outputDirectory, final File directoryToZip, final List<File> fileList)
    150            throws IOException {
    151        try (OutputStream fos = Files.newOutputStream(new File(outputDirectory.getAbsolutePath() + "/site.zip")
    152                .toPath());
    153             ZipOutputStream zos = new ZipOutputStream(fos)) {
    154            for (final File file : fileList) {
    155                if (!file.isDirectory()) { // we only zip files, not directories
    156                    addToZip(directoryToZip, file, zos);
    157                }
    158            }
    159        }
    160    }
    161
    162    /**
    163     * Given the <code>directoryToZip</code> we add the <code>file</code> to the zip archive represented by
    164     * <code>zos</code>.
    165     *
    166     * @param directoryToZip a {@link File} representing the directory from which the file exists that we are
    167     *                       compressing. Generally this is <code>target/site</code>.
    168     * @param file a {@link File} to add to the {@link ZipOutputStream} <code>zos</code>.
    169     * @param zos the {@link ZipOutputStream} to which to add our <code>file</code>.
    170     * @throws IOException if adding the <code>file</code> doesn't work out properly.
    171     */
    172    private void addToZip(final File directoryToZip, final File file, final ZipOutputStream zos) throws IOException {
    173        try (InputStream fis = Files.newInputStream(file.toPath())) {
    174            // we want the zipEntry's path to be a relative path that is relative
    175            // to the directory being zipped, so chop off the rest of the path
    176            final String zipFilePath = file.getCanonicalPath().substring(
    177                    directoryToZip.getCanonicalPath().length() + 1,
    178                    file.getCanonicalPath().length());
    179            final ZipEntry zipEntry = new ZipEntry(zipFilePath);
    180            zos.putNextEntry(zipEntry);
    181            IOUtils.copy(fis, zos);
    182        }
    183    }
    184}