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 *      https://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 final class CommonsSiteCompressionMojo extends AbstractMojo {
052
053    /**
054     * The working directory for the plugin, which is <code>target/commons-release-plugin</code> when Maven uses
055     * the default <code>${project.build.directory}</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     * Adds the <code>file</code> from <code>directoryToZip</code> to the ZIP archive represented by
096     * <code>zos</code>.
097     *
098     * @param directoryToZip A {@link File} representing the directory containing the file to compress. Generally, this is <code>target/site</code>.
099     * @param file A {@link File} to add to the {@link ZipOutputStream} <code>zos</code>.
100     * @param zos The {@link ZipOutputStream} to which to add our <code>file</code>.
101     * @throws IOException Thrown if an error occurs while adding the <code>file</code> to the ZIP archive.
102     */
103    private void addToZip(final File directoryToZip, final File file, final ZipOutputStream zos) throws IOException {
104        try (InputStream fis = Files.newInputStream(file.toPath())) {
105            // Make the ZIP entry path relative to the directory being compressed.
106            final String zipFilePath = file.getCanonicalPath().substring(
107                    directoryToZip.getCanonicalPath().length() + 1,
108                    file.getCanonicalPath().length());
109            final ZipEntry zipEntry = new ZipEntry(zipFilePath);
110            zos.putNextEntry(zipEntry);
111            IOUtils.copy(fis, zos);
112        }
113    }
114
115    @Override
116    public void execute() throws MojoExecutionException, MojoFailureException {
117        if (!isDistModule) {
118            getLog().info("This module is marked as a non distribution "
119                    + "or assembly module, and the plugin will not run.");
120            return;
121        }
122        if (StringUtils.isEmpty(distSvnStagingUrl)) {
123            getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
124            return;
125        }
126        if (!siteDirectory.exists()) {
127            getLog().error("\"mvn site\" was not run before this goal, or a siteDirectory did not exist.");
128            throw new MojoFailureException(
129                    "\"mvn site\" was not run before this goal, or a siteDirectory did not exist."
130            );
131        }
132        if (!workingDirectory.exists()) {
133            getLog().info("Current project contains no distributions. Not executing.");
134            return;
135        }
136        try {
137            filesToCompress = new ArrayList<>();
138            getAllSiteFiles(siteDirectory, filesToCompress);
139            writeZipFile(workingDirectory, siteDirectory, filesToCompress);
140        } catch (final IOException e) {
141            getLog().error("Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(), e);
142            throw new MojoExecutionException(
143                    "Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(),
144                    e
145            );
146        }
147    }
148
149    /**
150     * Gets all site files and adds them to the {@link CommonsSiteCompressionMojo#filesToCompress} {@link List}.
151     * This method searches the <code>target/site</code> directory by default.
152     *
153     * @param siteDirectory The {@link File} that represents the <code>target/site</code> directory.
154     * @param filesToCompress The {@link List} to which to add all the files.
155     */
156    private void getAllSiteFiles(final File siteDirectory, final List<File> filesToCompress) {
157        final File[] files = siteDirectory.listFiles();
158        for (final File file : files) {
159            filesToCompress.add(file);
160            if (file.isDirectory()) {
161                getAllSiteFiles(file, filesToCompress);
162            }
163        }
164    }
165
166    /**
167     * Writes all the files in <code>fileList</code> to a <code>site.zip</code> file
168     * in the <code>workingDirectory</code>.
169     *
170     * @param outputDirectory is a {@link File} representing the place to put the site.zip file.
171     * @param directoryToZip is a {@link File} representing the directory of the site (normally
172     *                       <code>target/site</code>).
173     * @param fileList The list of files to be zipped up, generally generated by
174     *                 {@link CommonsSiteCompressionMojo#getAllSiteFiles(File, List)}.
175     * @throws IOException Thrown if an error occurs while copying the files.
176     */
177    private void writeZipFile(final File outputDirectory, final File directoryToZip, final List<File> fileList)
178            throws IOException {
179        try (OutputStream fos = Files.newOutputStream(new File(outputDirectory.getAbsolutePath() + "/site.zip")
180                .toPath());
181             ZipOutputStream zos = new ZipOutputStream(fos)) {
182            for (final File file : fileList) {
183                if (!file.isDirectory()) { // we only ZIP files, not directories
184                    addToZip(directoryToZip, file, zos);
185                }
186            }
187        }
188    }
189}