You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2018/04/29 19:35:13 UTC

[maven-artifact-transfer] 02/08: [MSHARED-695] - WIP - Move checksum generation from install to deploy

This is an automated email from the ASF dual-hosted git repository.

khmarbaise pushed a commit to branch MSHARED-695
in repository https://gitbox.apache.org/repos/asf/maven-artifact-transfer.git

commit a0e1f304dd1aefe4cdc2df7f1714ebf0b6754b91
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Sat Mar 31 10:37:00 2018 +0200

    [MSHARED-695] - WIP - Move checksum generation from install to deploy
---
 .../project/deploy/ProjectDeployerRequest.java     |  22 ----
 .../deploy/internal/DefaultProjectDeployer.java    | 104 +++++++++++++++++--
 .../project/deploy/internal/DualDigester.java      | 115 +++++++++++++++++++++
 .../project/install/ProjectInstallerRequest.java   |  20 ----
 .../install/internal/DefaultProjectInstaller.java  |  10 +-
 .../project/install/internal/DualDigesterTest.java |  25 +++++
 src/test/resources/test.jar                        |   1 +
 7 files changed, 241 insertions(+), 56 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployerRequest.java b/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployerRequest.java
index 3f98471..3a50c18 100644
--- a/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployerRequest.java
+++ b/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployerRequest.java
@@ -28,10 +28,6 @@ import org.apache.maven.project.MavenProject;
 public class ProjectDeployerRequest
 {
 
-    // From AbstractDeployMojo
-
-    private boolean updateReleaseInfo;
-
     private int retryFailedDeploymentCount;
 
     // From DeployMojo
@@ -45,24 +41,6 @@ public class ProjectDeployerRequest
     private String altReleaseDeploymentRepository;
 
     /**
-     * @return the updateReleaseInfo
-     */
-    public boolean isUpdateReleaseInfo()
-    {
-        return updateReleaseInfo;
-    }
-
-    /**
-     * @param theUpdateReleaseInfoToBeSet the updateReleaseInfo to set
-     * @return {@link ProjectDeployerRequest} for chaining.
-     */
-    public ProjectDeployerRequest setUpdateReleaseInfo( boolean theUpdateReleaseInfoToBeSet )
-    {
-        this.updateReleaseInfo = theUpdateReleaseInfoToBeSet;
-        return this;
-    }
-
-    /**
      * @return the retryFailedDeploymentCount
      */
     public int getRetryFailedDeploymentCount()
diff --git a/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java b/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java
index 74049be..ca51ccc 100644
--- a/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java
+++ b/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java
@@ -20,6 +20,7 @@ package org.apache.maven.shared.project.deploy.internal;
  */
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -35,6 +36,7 @@ import org.apache.maven.shared.project.deploy.ProjectDeployer;
 import org.apache.maven.shared.project.deploy.ProjectDeployerRequest;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.util.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -53,6 +55,8 @@ class DefaultProjectDeployer
     @Requirement
     private ArtifactDeployer deployer;
 
+    private final DualDigester digester = new DualDigester();
+
     /**
      * {@inheritDoc}
      */
@@ -60,6 +64,8 @@ class DefaultProjectDeployer
                         ArtifactRepository artifactRepository )
         throws NoFileAssignedException, IllegalArgumentException, ArtifactDeployerException
     {
+        boolean createChecksum = true;
+
         validateParameters( buildingRequest, projectDeployerRequest, artifactRepository );
 
         Artifact artifact = projectDeployerRequest.getProject().getArtifact();
@@ -80,13 +86,8 @@ class DefaultProjectDeployer
             artifact.addMetadata( metadata );
         }
 
-        // FIXME: It does not make sense to set an artifact explicitly to a "Release"
-        // cause this should be choosen only by the not existing of "-SNAPSHOT" in the
-        // version.
-        if ( projectDeployerRequest.isUpdateReleaseInfo() )
-        {
-            artifact.setRelease( true );
-        }
+        // What consequence does this have?
+        // artifact.setRelease( true );
 
         artifact.setRepository( artifactRepository );
 
@@ -103,6 +104,8 @@ class DefaultProjectDeployer
 
             if ( file != null && file.isFile() )
             {
+//                installChecksums( buildingRequest, artifact, createChecksum );
+                // ?
                 deployableArtifacts.add( artifact );
             }
             else if ( !attachedArtifacts.isEmpty() )
@@ -121,6 +124,7 @@ class DefaultProjectDeployer
 
         for ( Artifact attached : attachedArtifacts )
         {
+//            installChecksums( buildingRequest, artifact, createChecksum );
             deployableArtifacts.add( attached );
         }
 
@@ -186,4 +190,90 @@ class DefaultProjectDeployer
         }
     }
 
+    /**
+     * Installs the checksums for the specified artifact if this has been enabled in the plugin configuration. This
+     * method creates checksums for files that have already been installed to the local repo to account for on-the-fly
+     * generated/updated files. For example, in Maven 2.0.4- the <code>ProjectArtifactMetadata</code> did not install
+     * the original POM file (cf. MNG-2820). While the plugin currently requires Maven 2.0.6, we continue to hash the
+     * installed POM for robustness with regard to future changes like re-introducing some kind of POM filtering.
+     *
+     * @param buildingRequest The project building request, must not be <code>null</code>.
+     * @param artifact The artifact for which to create checksums, must not be <code>null</code>.
+     * @param createChecksum {@code true} if checksum should be created, otherwise {@code false}.
+     * @throws IOException If the checksums could not be installed.
+     */
+    private void installChecksums( ProjectBuildingRequest buildingRequest, Artifact artifact, boolean createChecksum )
+        throws IOException
+    {
+        if ( !createChecksum )
+        {
+            return;
+        }
+
+//        File artifactFile = getLocalRepoFile( buildingRequest, artifact );
+//        installChecksums( artifactFile );
+    }
+
+    /**
+     * Installs the checksums for the specified metadata files.
+     *
+     * @param metadataFiles The collection of metadata files to install checksums for, must not be <code>null</code>.
+     * @throws IOException If the checksums could not be installed.
+     */
+    private void installChecksums( Collection<File> metadataFiles )
+        throws IOException
+    {
+        for ( File metadataFile : metadataFiles )
+        {
+            installChecksums( metadataFile );
+        }
+    }
+
+    /**
+     * Installs the checksums for the specified file (if it exists).
+     *
+     * @param installedFile The path to the already installed file in the local repo for which to generate checksums,
+     *            must not be <code>null</code>.
+     * @throws IOException In case of errors. Could not install checksums.
+     */
+    private void installChecksums( File installedFile )
+        throws IOException
+    {
+        boolean signatureFile = installedFile.getName().endsWith( ".asc" );
+        if ( installedFile.isFile() && !signatureFile )
+        {
+            LOGGER.debug( "Calculating checksums for " + installedFile );
+            digester.calculate( installedFile );
+            installChecksum( installedFile, ".md5", digester.getMd5() );
+            installChecksum( installedFile, ".sha1", digester.getSha1() );
+        }
+    }
+
+    /**
+     * Installs a checksum for the specified file.
+     *
+     * @param installedFile The base path from which the path to the checksum files is derived by appending the given
+     *            file extension, must not be <code>null</code>.
+     * @param ext The file extension (including the leading dot) to use for the checksum file, must not be
+     *            <code>null</code>.
+     * @param checksum the checksum to write
+     * @throws IOException If the checksum could not be installed.
+     */
+    private void installChecksum( File installedFile, String ext, String checksum )
+        throws IOException
+    {
+        File checksumFile = new File( installedFile.getAbsolutePath() + ext );
+        LOGGER.debug( "Installing checksum to " + checksumFile );
+        try
+        {
+            // noinspection ResultOfMethodCallIgnored
+            checksumFile.getParentFile().mkdirs();
+            FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum );
+        }
+        catch ( IOException e )
+        {
+            throw new IOException( "Failed to install checksum to " + checksumFile, e );
+        }
+    }
+
 }
diff --git a/src/main/java/org/apache/maven/shared/project/deploy/internal/DualDigester.java b/src/main/java/org/apache/maven/shared/project/deploy/internal/DualDigester.java
new file mode 100644
index 0000000..a2b8c34
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/project/deploy/internal/DualDigester.java
@@ -0,0 +1,115 @@
+package org.apache.maven.shared.project.deploy.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.codec.binary.Hex;
+import org.codehaus.plexus.util.IOUtil;
+
+
+/**
+ * Calculates md5 and sha1 digest.
+ * <p/>
+ * Todo: Consider using a thread to calculate one of the digests when the files are large; it's fairly slow !
+ *
+ * @author Kristian Rosenvold
+ */
+//TODO: Think about this class if we could use the ChecksumUtils class of
+// aether-util ? I think we need to go via reflection.
+//
+class DualDigester
+{
+    private final MessageDigest md5 = getDigester( "MD5" );
+
+    private final MessageDigest sh1 = getDigester( "SHA-1" );
+
+    private static final int BUFSIZE = 65536 * 2;
+
+    private final byte[] buffer = new byte[BUFSIZE];
+
+    static MessageDigest getDigester( String algorithm )
+    {
+        try
+        {
+            return MessageDigest.getInstance( algorithm );
+        }
+        catch ( NoSuchAlgorithmException e )
+        {
+            throw new RuntimeException( "Unable to initialize digest " + algorithm + " : " + e.getMessage() );
+        }
+    }
+
+    public void calculate( File file ) throws IOException
+    {
+        FileInputStream fis = null;
+
+        try
+        {
+            fis = new FileInputStream( file );
+            calculate( fis );
+            fis.close();
+            fis = null;
+        }
+        catch ( IOException e )
+        {
+            throw new IOException( "Failed to calculate digest checksum for " + file, e );
+        }
+        finally
+        {
+            IOUtil.close( fis );
+        }
+    }
+
+    void calculate( InputStream stream )
+        throws IOException
+    {
+        md5.reset();
+        sh1.reset();
+        update( stream );
+    }
+
+    public String getMd5()
+    {
+        return Hex.encodeHexString( md5.digest() );
+    }
+
+    public String getSha1()
+    {
+        return Hex.encodeHexString( sh1.digest() );
+    }
+
+    private void update( InputStream is )
+        throws IOException
+    {
+        int size = is.read( buffer, 0, BUFSIZE );
+        while ( size >= 0 )
+        {
+            md5.update( buffer, 0, size );
+            sh1.update( buffer, 0, size );
+            size = is.read( buffer, 0, BUFSIZE );
+        }
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/project/install/ProjectInstallerRequest.java b/src/main/java/org/apache/maven/shared/project/install/ProjectInstallerRequest.java
index c37329b..6a9505f 100644
--- a/src/main/java/org/apache/maven/shared/project/install/ProjectInstallerRequest.java
+++ b/src/main/java/org/apache/maven/shared/project/install/ProjectInstallerRequest.java
@@ -30,8 +30,6 @@ public class ProjectInstallerRequest
 
     private boolean createChecksum;
 
-    private boolean updateReleaseInfo;
-
     // From InstallMojo
 
     private MavenProject project;
@@ -55,24 +53,6 @@ public class ProjectInstallerRequest
     }
 
     /**
-     * @return the updateReleaseInfo
-     */
-    public boolean isUpdateReleaseInfo()
-    {
-        return updateReleaseInfo;
-    }
-
-    /**
-     * @param theUpdateReleaseInfoToBeSet the updateReleaseInfo to set
-     * @return {@link ProjectInstallerRequest} for chaining.
-     */
-    public ProjectInstallerRequest setUpdateReleaseInfo( boolean theUpdateReleaseInfoToBeSet )
-    {
-        this.updateReleaseInfo = theUpdateReleaseInfoToBeSet;
-        return this;
-    }
-
-    /**
      * @return the project
      */
     public MavenProject getProject()
diff --git a/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java b/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java
index f0ee602..ceb17f4 100644
--- a/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java
+++ b/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java
@@ -71,11 +71,12 @@ class DefaultProjectInstaller
     public void install( ProjectBuildingRequest buildingRequest, ProjectInstallerRequest installerRequest )
         throws IOException, ArtifactInstallerException, NoFileAssignedException, IllegalArgumentException
     {
+
         validateParameters( buildingRequest, installerRequest );
         MavenProject project = installerRequest.getProject();
-        boolean createChecksum = installerRequest.isCreateChecksum();
-        boolean updateReleaseInfo = installerRequest.isUpdateReleaseInfo();
 
+        boolean createChecksum = true;
+        
         Artifact artifact = project.getArtifact();
         String packaging = project.getPackaging();
         File pomFile = project.getFile();
@@ -87,11 +88,6 @@ class DefaultProjectInstaller
 
         ProjectArtifactMetadata metadata;
 
-        if ( updateReleaseInfo )
-        {
-            artifact.setRelease( true );
-        }
-
         Collection<File> metadataFiles = new LinkedHashSet<File>();
 
         if ( isPomArtifact )
diff --git a/src/test/java/org/apache/maven/shared/project/install/internal/DualDigesterTest.java b/src/test/java/org/apache/maven/shared/project/install/internal/DualDigesterTest.java
new file mode 100644
index 0000000..38da15b
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/project/install/internal/DualDigesterTest.java
@@ -0,0 +1,25 @@
+package org.apache.maven.shared.project.install.internal;
+
+import java.io.InputStream;
+
+import org.junit.Test;
+
+public class DualDigesterTest
+{
+
+    enum ChecksumTypes {
+        MD5,
+        SHA1,
+        SHA256
+    }
+    @Test
+    public void testName()
+    {
+        InputStream resourceAsStream = this.getClass().getResourceAsStream( "/test.jar" );
+        
+        DualDigester dd = new DualDigester();
+        
+        dd.calculate( file );
+    }
+    
+}
diff --git a/src/test/resources/test.jar b/src/test/resources/test.jar
new file mode 100644
index 0000000..71d030c
--- /dev/null
+++ b/src/test/resources/test.jar
@@ -0,0 +1 @@
+This is a Test File

-- 
To stop receiving notification emails like this one, please contact
khmarbaise@apache.org.