You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2009/01/01 18:54:01 UTC

svn commit: r730574 - in /maven/plugins/trunk/maven-install-plugin/src: main/java/org/apache/maven/plugin/install/ test/java/org/apache/maven/plugin/install/

Author: bentmann
Date: Thu Jan  1 09:54:00 2009
New Revision: 730574

URL: http://svn.apache.org/viewvc?rev=730574&view=rev
Log:
o Refactored checksum creation bits

Modified:
    maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java
    maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallFileMojo.java
    maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallMojo.java
    maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java
    maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java

Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java?rev=730574&r1=730573&r2=730574&view=diff
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java (original)
+++ maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java Thu Jan  1 09:54:00 2009
@@ -22,6 +22,7 @@
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.installer.ArtifactInstaller;
+import org.apache.maven.artifact.metadata.ArtifactMetadata;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -31,7 +32,6 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
 
 /**
  * Common fields for installation mojos.
@@ -61,7 +61,7 @@
     protected ArtifactRepository localRepository;
 
     /**
-     * Flag Whether to create checksums(MD5, SHA1) or not.
+     * Flag whether to create checksums (MD5, SHA-1) or not.
      *
      * @parameter expression="${createChecksum}" default-value="false"
      */
@@ -81,80 +81,88 @@
      */
     protected Digester sha1Digester;
 
-    protected void installCheckSum( File file, boolean isPom )
-        throws MojoExecutionException
+    /**
+     * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist
+     * (yet).
+     * 
+     * @param artifact The artifact whose local repo path should be determined, must not be <code>null</code>.
+     * @return The absolute path to the artifact when installed, never <code>null</code>.
+     */
+    protected File getLocalRepoFile( Artifact artifact )
     {
-        installCheckSum( file, null, isPom );
+        String path = localRepository.pathOf( artifact );
+        return new File( localRepository.getBasedir(), path );
     }
 
-    protected void installCheckSum( File file, Artifact artifact, boolean isPom )
-        throws MojoExecutionException
+    /**
+     * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need
+     * not exist (yet).
+     * 
+     * @param metadata The artifact metadata whose local repo path should be determined, must not be <code>null</code>.
+     * @return The absolute path to the artifact metadata when installed, never <code>null</code>.
+     */
+    protected File getLocalRepoFile( ArtifactMetadata metadata )
     {
-        try
-        {
-            getLog().info( "Creating Checksums..." );
-
-            String md5Sum = getChecksum( file, "MD5" );
-            String sha1Sum = getChecksum( file, "SHA-1" );
-
-            File temp = File.createTempFile( "maven-md5-checksum", null );
-            temp.deleteOnExit();
-            FileUtils.fileWrite( temp.getAbsolutePath(), md5Sum );
-
-            File tempSha1 = File.createTempFile( "maven-sha1-checksum", null );
-            tempSha1.deleteOnExit();
-            FileUtils.fileWrite( tempSha1.getAbsolutePath(), sha1Sum );
-
-            File destination = null;
-
-            if ( isPom )
-            {
-                destination = file;
-            }
-            else
-            {
-                String localPath = localRepository.pathOf( artifact );
-                destination = new File( localRepository.getBasedir(), localPath );
-            }
-
-            if ( !destination.getParentFile().exists() )
-            {
-                destination.getParentFile().mkdirs();
-            }
-
-            getLog().debug( "Installing checksum for " + destination );
+        String path = localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository );
+        return new File( localRepository.getBasedir(), path );
+    }
 
-            FileUtils.copyFile( temp, new File( destination + ".md5" ) );
-            FileUtils.copyFile( tempSha1, new File( destination + ".sha1" ) );
-        }
-        catch ( IOException e )
+    /**
+     * Installs the checksums for the specified file if this has been enabled in the plugin configuration.
+     * 
+     * @param originalFile The path to the file from which the checksums are generated, must not be <code>null</code>.
+     * @param installedFile The base path from which the paths to the checksum files are derived, must not be
+     *            <code>null</code>.
+     * @throws MojoExecutionException If the checksums could not be installed.
+     */
+    protected void installChecksums( File originalFile, File installedFile )
+        throws MojoExecutionException
+    {
+        boolean signatureFile = installedFile.getName().endsWith( ".asc" );
+        if ( createChecksum && !signatureFile )
         {
-            throw new MojoExecutionException( "Error creating checksum", e );
+            installChecksum( originalFile, installedFile, md5Digester, ".md5" );
+            installChecksum( originalFile, installedFile, sha1Digester, ".sha1" );
         }
-        catch ( NoSuchAlgorithmException e )
+    }
+
+    /**
+     * Installs a checksum for the specified file.
+     * 
+     * @param originalFile The path to the file from which the checksum is generated, must not be <code>null</code>.
+     * @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 digester The checksum algorithm to use, 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>.
+     * @throws MojoExecutionException If the checksum could not be installed.
+     */
+    private void installChecksum( File originalFile, File installedFile, Digester digester, String ext )
+        throws MojoExecutionException
+    {
+        String checksum;
+        getLog().debug( "Calculating " + digester.getAlgorithm() + " checksum for " + originalFile );
+        try
         {
-            throw new MojoExecutionException( "Error in algorithm", e );
+            checksum = digester.calc( originalFile );
         }
         catch ( DigesterException e )
         {
-            throw new MojoExecutionException( e.getMessage(), e );
+            throw new MojoExecutionException( "Failed to calculate " + digester.getAlgorithm() + " checksum for "
+                + originalFile, e );
         }
-    }
 
-    protected String getChecksum( File file, String algo )
-        throws NoSuchAlgorithmException, DigesterException
-    {
-        if ( "MD5".equals( algo ) )
-        {
-            return md5Digester.calc( file );
-        }
-        else if ( "SHA-1".equals( algo ) )
+        File checksumFile = new File( installedFile.getAbsolutePath() + ext );
+        getLog().debug( "Installing checksum to " + checksumFile );
+        try
         {
-            return sha1Digester.calc( file );
+            checksumFile.getParentFile().mkdirs();
+            FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum );
         }
-        else
+        catch ( IOException e )
         {
-            throw new NoSuchAlgorithmException( "No support for algorithm " + algo + "." );
+            throw new MojoExecutionException( "Failed to install checksum to " + checksumFile, e );
         }
     }
+
 }

Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallFileMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallFileMojo.java?rev=730574&r1=730573&r2=730574&view=diff
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallFileMojo.java (original)
+++ maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallFileMojo.java Thu Jan  1 09:54:00 2009
@@ -179,8 +179,6 @@
 
         Artifact pomArtifact = null;
 
-        File pom = null;
-
         if ( pomFile != null )
         {
             if ( pomFile.isFile() )
@@ -215,14 +213,16 @@
         Artifact artifact =
             artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
 
+        File generatedPomFile = null;
+
         // TODO: check if it exists first, and default to true if not
         if ( generatePom )
         {
             Writer fw = null;
             try
             {
-                File tempFile = File.createTempFile( "mvninstall", ".pom" );
-                tempFile.deleteOnExit();
+                generatedPomFile = File.createTempFile( "mvninstall", ".pom" );
+                generatedPomFile.deleteOnExit();
 
                 Model model = new Model();
                 model.setModelVersion( "4.0.0" );
@@ -232,9 +232,9 @@
                 model.setPackaging( packaging );
                 model.setDescription( "POM was created from install:install-file" );
 
-                fw = WriterFactory.newXmlWriter( tempFile );
+                fw = WriterFactory.newXmlWriter( generatedPomFile );
                 new MavenXpp3Writer().write( fw, model );
-                metadata = new ProjectArtifactMetadata( artifact, tempFile );
+                metadata = new ProjectArtifactMetadata( artifact, generatedPomFile );
                 artifact.addMetadata( metadata );
             }
             catch ( IOException e )
@@ -258,28 +258,16 @@
             if ( !file.equals( destination ) )
             {
                 installer.install( file, artifact, localRepository );
-
-                if ( createChecksum )
+                installChecksums( file, getLocalRepoFile( artifact ) );
+                if ( generatePom )
                 {
-                    if ( generatePom )
-                    {
-                        //create checksums for pom and artifact
-                        pom = new File( localRepository.getBasedir(),
-                                        localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository ) );
-
-                        installCheckSum( pom, true );
-                    }
-                    installCheckSum( file, artifact, false );
+                    installChecksums( generatedPomFile, getLocalRepoFile( metadata ) );
                 }
 
                 if ( pomFile != null && pomFile.isFile() )
                 {
                     installer.install( pomFile, pomArtifact, localRepository );
-
-                    if ( createChecksum )
-                    {
-                        installCheckSum( pomFile, pomArtifact, false );
-                    }
+                    installChecksums( pomFile, getLocalRepoFile( pomArtifact ) );
                 }
             }
             else

Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallMojo.java?rev=730574&r1=730573&r2=730574&view=diff
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallMojo.java (original)
+++ maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/InstallMojo.java Thu Jan  1 09:54:00 2009
@@ -93,11 +93,7 @@
             if ( isPomArtifact )
             {
                 installer.install( pomFile, artifact, localRepository );
-
-                if ( createChecksum )
-                {
-                    installCheckSum( pomFile, artifact, false );
-                }
+                installChecksums( pomFile, getLocalRepoFile( artifact ) );
             }
             else
             {
@@ -111,17 +107,8 @@
                 if ( file != null && file.isFile() )
                 {
                     installer.install( file, artifact, localRepository );
-
-                    if ( createChecksum )
-                    {
-                        //create checksums for pom and artifact
-                        File pom = new File( localRepository.getBasedir(),
-                                             localRepository.pathOfLocalRepositoryMetadata( metadata,
-                                                                                            localRepository ) );
-
-                        installCheckSum( pom, true );
-                        installCheckSum( file, artifact, false );
-                    }
+                    installChecksums( file, getLocalRepoFile( artifact ) );
+                    installChecksums( pomFile, getLocalRepoFile( metadata ) );
                 }
                 else if ( !attachedArtifacts.isEmpty() )
                 {
@@ -137,10 +124,7 @@
                     }
 
                     installer.install( pomFile, pomArtifact, localRepository );
-                    if ( createChecksum )
-                    {
-                        installCheckSum( pomFile, pomArtifact, false );
-                    }
+                    installChecksums( pomFile, getLocalRepoFile( pomArtifact ) );
                 }
                 else
                 {
@@ -154,13 +138,7 @@
                 Artifact attached = (Artifact) i.next();
 
                 installer.install( attached.getFile(), attached, localRepository );
-
-                boolean signatureFile = attached.getFile().getName().endsWith( ".asc" );
-
-                if ( createChecksum && !signatureFile )
-                {
-                    installCheckSum( attached.getFile(), attached, false );
-                }
+                installChecksums( attached.getFile(), getLocalRepoFile( attached ) );
             }
         }
         catch ( ArtifactInstallationException e )

Modified: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java?rev=730574&r1=730573&r2=730574&view=diff
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java (original)
+++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java Thu Jan  1 09:54:00 2009
@@ -241,8 +241,8 @@
         mojo.execute();
 
         //get the actual checksum of the artifact
-        String actualMd5Sum = mojo.getChecksum( file, "MD5" );
-        String actualSha1Sum = mojo.getChecksum( file, "SHA-1" );
+        String actualMd5Sum = mojo.md5Digester.calc( file );
+        String actualSha1Sum = mojo.sha1Digester.calc( file );
 
         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
             artifactId + "-" + version;
@@ -255,8 +255,8 @@
         assertTrue( md5.exists() );
         assertTrue( sha1.exists() );
 
-        String generatedMd5 = FileUtils.fileRead( md5 );
-        String generatedSha1 = FileUtils.fileRead( sha1 );
+        String generatedMd5 = FileUtils.fileRead( md5, "UTF-8" );
+        String generatedSha1 = FileUtils.fileRead( sha1, "UTF-8" );
 
         assertEquals( actualMd5Sum, generatedMd5 );
         assertEquals( actualSha1Sum, generatedSha1 );

Modified: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java?rev=730574&r1=730573&r2=730574&view=diff
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java (original)
+++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java Thu Jan  1 09:54:00 2009
@@ -236,12 +236,12 @@
         assertTrue( pom.exists() );
 
         //get the actual checksum of the pom
-        String actualPomMd5Sum = mojo.getChecksum( pom, "MD5" );
-        String actualPomSha1Sum = mojo.getChecksum( pom, "SHA-1" );
+        String actualPomMd5Sum = mojo.md5Digester.calc( pom );
+        String actualPomSha1Sum = mojo.sha1Digester.calc( pom );
 
         //get the actual checksum of the artifact
-        String actualMd5Sum = mojo.getChecksum( file, "MD5" );
-        String actualSha1Sum = mojo.getChecksum( file, "SHA-1" );
+        String actualMd5Sum = mojo.md5Digester.calc( file );
+        String actualSha1Sum = mojo.sha1Digester.calc( file );
 
         String groupId = dotToSlashReplacer( artifact.getGroupId() );
 
@@ -263,10 +263,10 @@
         assertTrue( md5.exists() );
         assertTrue( sha1.exists() );
 
-        String generatedMd5 = FileUtils.fileRead( md5 );
-        String generatedSha1 = FileUtils.fileRead( sha1 );
-        String generatedPomMd5 = FileUtils.fileRead( pomMd5 );
-        String generatedPomSha1 = FileUtils.fileRead( pomSha1 );
+        String generatedMd5 = FileUtils.fileRead( md5, "UTF-8" );
+        String generatedSha1 = FileUtils.fileRead( sha1, "UTF-8" );
+        String generatedPomMd5 = FileUtils.fileRead( pomMd5, "UTF-8" );
+        String generatedPomSha1 = FileUtils.fileRead( pomSha1, "UTF-8" );
 
         assertEquals( actualMd5Sum, generatedMd5 );
         assertEquals( actualSha1Sum, generatedSha1 );