Adding extra files
Project: http://git-wip-us.apache.org/repos/asf/commons-release-plugin/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-release-plugin/commit/7cc0bd92 Tree: http://git-wip-us.apache.org/repos/asf/commons-release-plugin/tree/7cc0bd92 Diff: http://git-wip-us.apache.org/repos/asf/commons-release-plugin/diff/7cc0bd92 Branch: refs/heads/master Commit: 7cc0bd923a60bbb21aaabcae8e96da04d516f577 Parents: 1953878 Author: Rob Tompkins <chtom...@gmail.com> Authored: Mon Jan 1 15:34:51 2018 -0500 Committer: Rob Tompkins <chtom...@gmail.com> Committed: Mon Jan 1 15:34:51 2018 -0500 ---------------------------------------------------------------------- .../commons/release/plugin/SharedFunctions.java | 42 +++++++++ .../plugin/handler/DistributionScmHandler.java | 11 +++ .../release/plugin/handler/package-info.java | 1 + .../mojos/CommonsSiteCompressionMojo.java | 97 ++++++++++++++++++++ 4 files changed, 151 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-release-plugin/blob/7cc0bd92/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java b/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java new file mode 100644 index 0000000..238d58b --- /dev/null +++ b/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java @@ -0,0 +1,42 @@ +package org.apache.commons.release.plugin; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; +import java.io.IOException; + +/** + * Shared static functions for all of our Mojos + * + * @author chtompki + * @since 1.0 + */ +public class SharedFunctions { + + private SharedFunctions() { + //Uitility Class + } + + /** + * Cleans and then initializes an empty directory that is given by the <code>workingDirectory</code> + * parameter. + * + * @param log is the maven log for output logging, particularly in regards to error management. + * @param workingDirectory is a {@link File} that represents the directory to first attempt to delete then create. + */ + public static void initWorkingDirectory(Log log, File workingDirectory) throws MojoExecutionException { + if (workingDirectory.exists()) { + try { + FileUtils.deleteDirectory(workingDirectory); + } catch (IOException e) { + log.error(e.getMessage()); + throw new MojoExecutionException("Unable to remove working directory: " + e.getMessage(), e); + } + } + if (!workingDirectory.exists()) { + workingDirectory.mkdirs(); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-release-plugin/blob/7cc0bd92/src/main/java/org/apache/commons/release/plugin/handler/DistributionScmHandler.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/release/plugin/handler/DistributionScmHandler.java b/src/main/java/org/apache/commons/release/plugin/handler/DistributionScmHandler.java new file mode 100644 index 0000000..0a841f9 --- /dev/null +++ b/src/main/java/org/apache/commons/release/plugin/handler/DistributionScmHandler.java @@ -0,0 +1,11 @@ +package org.apache.commons.release.plugin.handler; + +import java.io.File; + +public class DistributionScmHandler { + + public void checkoutDirectory(String scmUrl, File checkoutRootDirectory) { + + } + +} http://git-wip-us.apache.org/repos/asf/commons-release-plugin/blob/7cc0bd92/src/main/java/org/apache/commons/release/plugin/handler/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/release/plugin/handler/package-info.java b/src/main/java/org/apache/commons/release/plugin/handler/package-info.java new file mode 100644 index 0000000..064b55a --- /dev/null +++ b/src/main/java/org/apache/commons/release/plugin/handler/package-info.java @@ -0,0 +1 @@ +package org.apache.commons.release.plugin.handler; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-release-plugin/blob/7cc0bd92/src/main/java/org/apache/commons/release/plugin/mojos/CommonsSiteCompressionMojo.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/release/plugin/mojos/CommonsSiteCompressionMojo.java b/src/main/java/org/apache/commons/release/plugin/mojos/CommonsSiteCompressionMojo.java new file mode 100644 index 0000000..5812061 --- /dev/null +++ b/src/main/java/org/apache/commons/release/plugin/mojos/CommonsSiteCompressionMojo.java @@ -0,0 +1,97 @@ +package org.apache.commons.release.plugin.mojos; + +import org.apache.commons.compress.archivers.zip.ParallelScatterZipCreator; +import org.apache.commons.compress.archivers.zip.ScatterZipOutputStream; +import org.apache.commons.release.plugin.SharedFunctions; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * Takes the built <code>./target/site</code> directory and compresses it to + * <code>./target/commons-release-plugin/site.zip</code>. + * + * @author chtompki + * @since 1.0 + */ +@Mojo(name = "compress-site", defaultPhase = LifecyclePhase.POST_SITE, threadSafe = true) +public class CommonsSiteCompressionMojo extends AbstractMojo { + + @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin", alias = "outputDirectory") + private File workingDirectory; + + @Parameter(defaultValue = "${project.build.directory}/site", alias = "siteOutputDirectory") + private File siteDirectory; + + private ScatterZipOutputStream dirs; + + private ParallelScatterZipCreator scatterZipCreator; + + private List<File> filesToCompress; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + if (!workingDirectory.exists()) { + SharedFunctions.initWorkingDirectory(getLog(), workingDirectory); + } + try { + filesToCompress = new ArrayList<>(); + getAllSiteFiles(siteDirectory, filesToCompress); + writeZipFile(workingDirectory, siteDirectory, filesToCompress); + } catch (IOException e) { + getLog().error("Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(), e); + throw new MojoExecutionException("Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(), e); + } + } + + private void getAllSiteFiles(File siteDirectory, List<File> filesToCompress) throws IOException { + File[] files = siteDirectory.listFiles(); + for (File file : files) { + filesToCompress.add(file); + if (file.isDirectory()) { + getAllSiteFiles(file, filesToCompress); + } + } + } + + private void writeZipFile(File workingDirectory, File directoryToZip, List<File> fileList) throws IOException { + FileOutputStream fos = new FileOutputStream(workingDirectory.getAbsolutePath() + "/site.zip"); + ZipOutputStream zos = new ZipOutputStream(fos); + for (File file : fileList) { + if (!file.isDirectory()) { // we only zip files, not directories + addToZip(directoryToZip, file, zos); + } + } + zos.close(); + fos.close(); + } + + private void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws IOException { + FileInputStream fis = new FileInputStream(file); + // we want the zipEntry's path to be a relative path that is relative + // to the directory being zipped, so chop off the rest of the path + String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1, + file.getCanonicalPath().length()); + ZipEntry zipEntry = new ZipEntry(zipFilePath); + zos.putNextEntry(zipEntry); + byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zos.write(bytes, 0, length); + } + zos.closeEntry(); + fis.close(); + } +}