CommonsSiteCompressionMojo.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.release.plugin.mojos;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.nio.file.Files;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.zip.ZipEntry;
  26. import java.util.zip.ZipOutputStream;

  27. import org.apache.commons.io.IOUtils;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.apache.maven.plugin.AbstractMojo;
  30. import org.apache.maven.plugin.MojoExecutionException;
  31. import org.apache.maven.plugin.MojoFailureException;
  32. import org.apache.maven.plugins.annotations.LifecyclePhase;
  33. import org.apache.maven.plugins.annotations.Mojo;
  34. import org.apache.maven.plugins.annotations.Parameter;

  35. /**
  36.  * Takes the built <code>./target/site</code> directory and compresses it to
  37.  * <code>./target/commons-release-plugin/site.zip</code>.
  38.  *
  39.  * @since 1.0
  40.  * @deprecated - as we no longer wish to compress the site, we are going to put this functionality in the
  41.  *               {@link CommonsDistributionStagingMojo}.
  42.  */
  43. @Deprecated
  44. @Mojo(name = "compress-site",
  45.         defaultPhase = LifecyclePhase.POST_SITE,
  46.         threadSafe = true,
  47.         aggregator = true)
  48. public class CommonsSiteCompressionMojo extends AbstractMojo {

  49.     /**
  50.      * The working directory for the plugin which, assuming the maven uses the default
  51.      * <code>${project.build.directory}</code>, this becomes <code>target/commons-release-plugin</code>.
  52.      */
  53.     @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin",
  54.             property = "commons.outputDirectory")
  55.     private File workingDirectory;

  56.     /**
  57.      */
  58.     @Parameter(defaultValue = "${project.build.directory}/site", property = "commons.siteOutputDirectory")
  59.     private File siteDirectory;

  60.     /**
  61.      * The url of the subversion repository to which we wish the artifacts to be staged. Typically
  62.      * this would need to be of the form:
  63.      * <code>scm:svn:https://dist.apache.org/repos/dist/dev/commons/foo</code>. Note. that the prefix to the
  64.      * substring <code>https</code> is a requirement.
  65.      */
  66.     @Parameter(defaultValue = "", property = "commons.distSvnStagingUrl")
  67.     private String distSvnStagingUrl;

  68.     /**
  69.      * A parameter to generally avoid running unless it is specifically turned on by the consuming module.
  70.      */
  71.     @Parameter(defaultValue = "false", property = "commons.release.isDistModule")
  72.     private Boolean isDistModule;

  73.     /**
  74.      * The list of files to compress into the site.zip file.
  75.      */
  76.     private List<File> filesToCompress;

  77.     @Override
  78.     public void execute() throws MojoExecutionException, MojoFailureException {
  79.         if (!isDistModule) {
  80.             getLog().info("This module is marked as a non distribution "
  81.                     + "or assembly module, and the plugin will not run.");
  82.             return;
  83.         }
  84.         if (StringUtils.isEmpty(distSvnStagingUrl)) {
  85.             getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
  86.             return;
  87.         }
  88.         if (!siteDirectory.exists()) {
  89.             getLog().error("\"mvn site\" was not run before this goal, or a siteDirectory did not exist.");
  90.             throw new MojoFailureException(
  91.                     "\"mvn site\" was not run before this goal, or a siteDirectory did not exist."
  92.             );
  93.         }
  94.         if (!workingDirectory.exists()) {
  95.             getLog().info("Current project contains no distributions. Not executing.");
  96.             return;
  97.         }
  98.         try {
  99.             filesToCompress = new ArrayList<>();
  100.             getAllSiteFiles(siteDirectory, filesToCompress);
  101.             writeZipFile(workingDirectory, siteDirectory, filesToCompress);
  102.         } catch (final IOException e) {
  103.             getLog().error("Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(), e);
  104.             throw new MojoExecutionException(
  105.                     "Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(),
  106.                     e
  107.             );
  108.         }
  109.     }

  110.     /**
  111.      * By default this method iterates across the <code>target/site</code> directory and adds all the files
  112.      * to the {@link CommonsSiteCompressionMojo#filesToCompress} {@link List}.
  113.      *
  114.      * @param siteDirectory the {@link File} that represents the <code>target/site</code> directory.
  115.      * @param filesToCompress the {@link List} to which to add all the files.
  116.      */
  117.     private void getAllSiteFiles(final File siteDirectory, final List<File> filesToCompress) {
  118.         final File[] files = siteDirectory.listFiles();
  119.         for (final File file : files) {
  120.             filesToCompress.add(file);
  121.             if (file.isDirectory()) {
  122.                 getAllSiteFiles(file, filesToCompress);
  123.             }
  124.         }
  125.     }

  126.     /**
  127.      * A helper method for writing all the files in our <code>fileList</code> to a <code>site.zip</code> file
  128.      * in the <code>workingDirectory</code>.
  129.      *
  130.      * @param outputDirectory is a {@link File} representing the place to put the site.zip file.
  131.      * @param directoryToZip is a {@link File} representing the directory of the site (normally
  132.      *                       <code>target/site</code>).
  133.      * @param fileList the list of files to be zipped up, generally generated by
  134.      *                 {@link CommonsSiteCompressionMojo#getAllSiteFiles(File, List)}.
  135.      * @throws IOException when the copying of the files goes incorrectly.
  136.      */
  137.     private void writeZipFile(final File outputDirectory, final File directoryToZip, final List<File> fileList)
  138.             throws IOException {
  139.         try (OutputStream fos = Files.newOutputStream(new File(outputDirectory.getAbsolutePath() + "/site.zip")
  140.                 .toPath());
  141.              ZipOutputStream zos = new ZipOutputStream(fos)) {
  142.             for (final File file : fileList) {
  143.                 if (!file.isDirectory()) { // we only ZIP files, not directories
  144.                     addToZip(directoryToZip, file, zos);
  145.                 }
  146.             }
  147.         }
  148.     }

  149.     /**
  150.      * Given the <code>directoryToZip</code> we add the <code>file</code> to the ZIP archive represented by
  151.      * <code>zos</code>.
  152.      *
  153.      * @param directoryToZip a {@link File} representing the directory from which the file exists that we are
  154.      *                       compressing. Generally this is <code>target/site</code>.
  155.      * @param file a {@link File} to add to the {@link ZipOutputStream} <code>zos</code>.
  156.      * @param zos the {@link ZipOutputStream} to which to add our <code>file</code>.
  157.      * @throws IOException if adding the <code>file</code> doesn't work out properly.
  158.      */
  159.     private void addToZip(final File directoryToZip, final File file, final ZipOutputStream zos) throws IOException {
  160.         try (InputStream fis = Files.newInputStream(file.toPath())) {
  161.             // we want the zipEntry's path to be a relative path that is relative
  162.             // to the directory being zipped, so chop off the rest of the path
  163.             final String zipFilePath = file.getCanonicalPath().substring(
  164.                     directoryToZip.getCanonicalPath().length() + 1,
  165.                     file.getCanonicalPath().length());
  166.             final ZipEntry zipEntry = new ZipEntry(zipFilePath);
  167.             zos.putNextEntry(zipEntry);
  168.             IOUtils.copy(fis, zos);
  169.         }
  170.     }
  171. }