You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ch...@apache.org on 2018/05/16 19:14:06 UTC

commons-release-plugin git commit: COMMONSSITE-113: adding sha1.properties file in workingDirectory

Repository: commons-release-plugin
Updated Branches:
  refs/heads/master 5152a0bca -> 9283b7477


COMMONSSITE-113: adding sha1.properties file in workingDirectory


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/9283b747
Tree: http://git-wip-us.apache.org/repos/asf/commons-release-plugin/tree/9283b747
Diff: http://git-wip-us.apache.org/repos/asf/commons-release-plugin/diff/9283b747

Branch: refs/heads/master
Commit: 9283b74770fa03875b08f3f4a89f2ad397c98e1a
Parents: 5152a0b
Author: Rob Tompkins <ch...@capitalone.com>
Authored: Wed May 16 15:13:29 2018 -0400
Committer: Rob Tompkins <ch...@capitalone.com>
Committed: Wed May 16 15:13:59 2018 -0400

----------------------------------------------------------------------
 .../CommonsDistributionDetachmentMojo.java      | 75 +++++++++++++++++---
 .../CommonsDistributionDetachmentMojoTest.java  |  2 +
 .../DistributionDetachmentProjectStub.java      | 18 +++--
 3 files changed, 79 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-release-plugin/blob/9283b747/src/main/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojo.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojo.java b/src/main/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojo.java
index 3587e64..ff8b785 100644
--- a/src/main/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojo.java
+++ b/src/main/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojo.java
@@ -16,6 +16,17 @@
  */
 package org.apache.commons.release.plugin.mojos;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.release.plugin.SharedFunctions;
@@ -27,16 +38,6 @@ import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.nio.file.Files;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
 /**
  * The purpose of this maven mojo is to detach the artifacts generated by the maven-assembly-plugin,
  * which for the Apache Commons Project do not get uploaded to Nexus, and putting those artifacts
@@ -75,6 +76,13 @@ public class CommonsDistributionDetachmentMojo extends AbstractMojo {
     private List<Artifact> detachedArtifacts = new ArrayList<>();
 
     /**
+     * A {@link Properties} of {@link Artifact} -> {@link String} containing the sha1 signatures
+     * for the individual artifacts, where the {@link Artifact} is represented as:
+     * <code>groupId:artifactId:version:type=sha1</code>.
+     */
+    private Properties artifactSha1s = new Properties();
+
+    /**
      * The maven project context injection so that we can get a hold of the variables at hand.
      */
     @Parameter(defaultValue = "${project}", required = true)
@@ -112,6 +120,7 @@ public class CommonsDistributionDetachmentMojo extends AbstractMojo {
         }
         getLog().info("Detaching Assemblies");
         for (Object attachedArtifact : project.getAttachedArtifacts()) {
+            putAttachedArtifactInSha1Map((Artifact) attachedArtifact);
             if (ARTIFACT_TYPES_TO_DETACH.contains(((Artifact) attachedArtifact).getType())) {
                 detachedArtifacts.add((Artifact) attachedArtifact);
             }
@@ -126,12 +135,58 @@ public class CommonsDistributionDetachmentMojo extends AbstractMojo {
         if (!workingDirectory.exists()) {
             SharedFunctions.initDirectory(getLog(), workingDirectory);
         }
+        logAllArtifactsInPropertiesFile();
         copyRemovedArtifactsToWorkingDirectory();
         getLog().info("");
         sha1AndMd5SignArtifacts();
     }
 
     /**
+     * Takes an attached artifact and puts the signature in the map.
+     * @param artifact is a maven {@link Artifact} taken from the project at start time of mojo.
+     * @throws MojoExecutionException if an {@link IOException} occurs when getting the sha1 of the
+     *                                artifact.
+     */
+    private void putAttachedArtifactInSha1Map(Artifact artifact) throws MojoExecutionException {
+        try {
+            StringBuffer artifactKey = new StringBuffer();
+            artifactKey
+                .append(artifact.getGroupId()).append('-')
+                .append(artifact.getArtifactId()).append('-')
+                .append(artifact.getVersion()).append('-')
+                .append(artifact.getType());
+            artifactSha1s.put(
+                artifactKey.toString(),
+                DigestUtils.sha1Hex(Files.readAllBytes(artifact.getFile().toPath()))
+            );
+        } catch (IOException e) {
+            throw new MojoExecutionException(
+                "Could not find artifact signature for: "
+                    + artifact.getArtifactId()
+                    + "-"
+                    + artifact.getVersion()
+                    + " type: "
+                    + artifact.getType()
+                ,e);
+        }
+    }
+
+    /**
+     * Writes to ./target/commons-release-plugin/sha1.properties the artifact sha1's.
+     *
+     * @throws MojoExecutionException if we cant write the file due to an {@link IOException}.
+     */
+    private void logAllArtifactsInPropertiesFile() throws MojoExecutionException {
+        try {
+            File sha1PropertiesFile = new File(workingDirectory, "sha1.properties");
+            FileOutputStream fileWriter = new FileOutputStream(sha1PropertiesFile);
+            artifactSha1s.store(fileWriter, "release sha1s");
+        } catch (IOException e) {
+            throw new MojoExecutionException("Failure to write sha1's", e);
+        }
+    }
+
+    /**
      * A helper method to copy the newly detached artifacts to <code>target/commons-release-plugin</code>
      * so that the {@link CommonsDistributionStagingMojo} can find the artifacts later.
      *

http://git-wip-us.apache.org/repos/asf/commons-release-plugin/blob/9283b747/src/test/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojoTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojoTest.java b/src/test/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojoTest.java
index 554432f..ecb458b 100644
--- a/src/test/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojoTest.java
+++ b/src/test/java/org/apache/commons/release/plugin/mojos/CommonsDistributionDetachmentMojoTest.java
@@ -74,6 +74,7 @@ public class CommonsDistributionDetachmentMojoTest {
         File detachedZipMd5 = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH + "/mockAttachedZip.zip.md5");
         File detachedZipSha1 = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH + "/mockAttachedZip.zip.sha1");
         File notDetachedMockAttachedFile = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH + "/mockAttachedFile.html");
+        File sha1Properties = new File(COMMONS_RELEASE_PLUGIN_TEST_DIR_PATH + "/sha1.properties");
         assertTrue(detachedTarGz.exists());
         assertTrue(detachedTarGzAsc.exists());
         assertTrue(detachedTarMd5.exists());
@@ -82,6 +83,7 @@ public class CommonsDistributionDetachmentMojoTest {
         assertTrue(detachedZipAsc.exists());
         assertTrue(detachedZipMd5.exists());
         assertTrue(detachedZipSha1.exists());
+        assertTrue(sha1Properties.exists());
         assertFalse(notDetachedMockAttachedFile.exists());
     }
 

http://git-wip-us.apache.org/repos/asf/commons-release-plugin/blob/9283b747/src/test/java/org/apache/commons/release/plugin/stubs/DistributionDetachmentProjectStub.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/release/plugin/stubs/DistributionDetachmentProjectStub.java b/src/test/java/org/apache/commons/release/plugin/stubs/DistributionDetachmentProjectStub.java
index 342f77b..5eb68ae 100644
--- a/src/test/java/org/apache/commons/release/plugin/stubs/DistributionDetachmentProjectStub.java
+++ b/src/test/java/org/apache/commons/release/plugin/stubs/DistributionDetachmentProjectStub.java
@@ -44,31 +44,36 @@ public class DistributionDetachmentProjectStub extends MavenProjectStub {
         attachedArtifacts.add(
                 new DistributionDetachmentArtifactStub(
                         new File("src/test/resources/mojos/detach-distributions/target/mockAttachedFile.html"),
-                        "html"
+                        "html",
+                    "mockAttachedFile"
                 )
         );
         attachedArtifacts.add(
                 new DistributionDetachmentArtifactStub(
                         new File("src/test/resources/mojos/detach-distributions/target/mockAttachedTar.tar.gz"),
-                        "tar.gz"
+                        "tar.gz",
+                        "mockAttachedTar"
                 )
         );
         attachedArtifacts.add(
                 new DistributionDetachmentArtifactStub(
                         new File("src/test/resources/mojos/detach-distributions/target/mockAttachedTar.tar.gz.asc"),
-                        "tar.gz.asc"
+                        "tar.gz.asc",
+                        "mockAttachedTar"
                 )
         );
         attachedArtifacts.add(
                 new DistributionDetachmentArtifactStub(
                         new File("src/test/resources/mojos/detach-distributions/target/mockAttachedZip.zip"),
-                        "zip"
+                        "zip",
+                        "mockAttachedZip"
                 )
         );
         attachedArtifacts.add(
                 new DistributionDetachmentArtifactStub(
                         new File("src/test/resources/mojos/detach-distributions/target/mockAttachedZip.zip.asc"),
-                        "zip.asc"
+                        "zip.asc",
+                        "mockAttachedZip"
                 )
         );
         return attachedArtifacts;
@@ -80,7 +85,8 @@ public class DistributionDetachmentProjectStub extends MavenProjectStub {
 
         private String type;
 
-        public DistributionDetachmentArtifactStub(File file, String type) {
+        public DistributionDetachmentArtifactStub(File file, String type, String artifactId) {
+            this.setArtifactId(artifactId);
             this.artifact = file;
             this.type = type;
         }