You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ar...@apache.org on 2006/05/25 09:37:27 UTC

svn commit: r409320 - in /maven/plugins/trunk/maven-install-plugin: ./ src/main/java/org/apache/maven/plugin/install/ src/test/java/org/apache/maven/plugin/install/ src/test/resources/unit/basic-install-checksum/ src/test/resources/unit/install-file-wi...

Author: aramirez
Date: Thu May 25 00:37:26 2006
New Revision: 409320

URL: http://svn.apache.org/viewvc?rev=409320&view=rev
Log:
PR: MINSTALL-9

allows install plugin to create md5 and sha1 for both the pom and the artifact

Added:
    maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/
    maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar
    maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/plugin-config.xml
    maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/
    maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar
    maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/plugin-config.xml
Modified:
    maven/plugins/trunk/maven-install-plugin/pom.xml
    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/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/pom.xml?rev=409320&r1=409319&r2=409320&view=diff
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-install-plugin/pom.xml Thu May 25 00:37:26 2006
@@ -37,5 +37,10 @@
 	  <scope>test</scope>
       <version>1.0-SNAPSHOT</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven.repository</groupId>
+      <artifactId>maven-repository-utils</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
   </dependencies>
 </project>

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=409320&r1=409319&r2=409320&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 May 25 00:37:26 2006
@@ -16,9 +16,17 @@
  * limitations under the License.
  */
 
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.installer.ArtifactInstaller;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.repository.digest.Digester;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
 
 /**
  * Common fields for installation mojos.
@@ -42,4 +50,76 @@
      * @readonly
      */
     protected ArtifactRepository localRepository;
+
+    /**
+     * @component role="org.apache.maven.repository.digest.Digester"
+     */
+    private Digester digester;
+
+    /**
+     * @parameter expression="${createChecksum}" default-value="false"
+     */
+    protected boolean createChecksum;
+
+    protected void installCheckSum( File file, boolean isPom )
+        throws MojoExecutionException
+    {
+        installCheckSum( file, null, isPom );
+    }
+    
+    protected void installCheckSum( File file, Artifact artifact, boolean isPom )
+        throws MojoExecutionException
+    {
+        try
+        {
+            getLog().info( "Creating Checksums..." );
+
+            String md5Sum = getChecksum( file, Digester.MD5 );
+            String sha1Sum = getChecksum( file, Digester.SHA1 );
+
+            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 );
+
+            FileUtils.copyFile( temp, new File( destination + ".md5" ) );
+            FileUtils.copyFile( tempSha1, new File( destination + ".sha1" ) );
+        }
+        catch( IOException e )
+        {
+            throw new MojoExecutionException( "Error creating checksum", e );
+        }
+        catch( NoSuchAlgorithmException e )
+        {
+            throw new MojoExecutionException( "Error in algorithm", e );
+        }
+    }
+
+    protected String getChecksum( File file, String algo )
+        throws NoSuchAlgorithmException, IOException
+    {
+        return digester.createChecksum( file, algo );
+    }
 }

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=409320&r1=409319&r2=409320&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 May 25 00:37:26 2006
@@ -72,7 +72,7 @@
      * @required
      */
     private File file;
-    
+
     /**
      * @parameter expression="${pomFile}"
      */
@@ -93,13 +93,17 @@
     {
         Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, packaging );
 
+        ArtifactMetadata metadata = null;
+
         Artifact pomArtifact = null;
-        
+
+        File pom = null;
+
         if( pomFile != null && pomFile.exists() )
         {
         	pomArtifact = artifactFactory.createArtifact( groupId, artifactId, version, null, "pom" );
         }
-        
+
         // TODO: check if it exists first, and default to true if not
         if ( generatePom )
         {
@@ -119,7 +123,7 @@
                 fw = new FileWriter( tempFile );
                 tempFile.deleteOnExit();
                 new MavenXpp3Writer().write( fw, model );
-                ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, tempFile );
+                metadata = new ProjectArtifactMetadata( artifact, tempFile );
                 artifact.addMetadata( metadata );
             }
             catch ( IOException e )
@@ -143,10 +147,28 @@
             if( !file.getPath().equals( destination.getPath() ) )
             {
                 installer.install( file, artifact, localRepository );
-                
+
+                if( createChecksum )
+                {
+                    if( generatePom )
+                    {
+                        //create checksums for pom and artifact
+                        pom = new File( localRepository.getBasedir(),
+                                             localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository ) );
+
+                        installCheckSum( pom, true );
+                    }
+                    installCheckSum( file, artifact , false );
+                }
+
                 if( pomFile != null && pomFile.exists() )
                 {
-                	installer.install( pomFile, pomArtifact, localRepository );
+                    installer.install( pomFile, pomArtifact, localRepository );
+
+                    if( createChecksum )
+                    {
+                        installCheckSum( pomFile, pomArtifact, false );
+                    }
                 }
             } 
             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=409320&r1=409319&r2=409320&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 May 25 00:37:26 2006
@@ -76,11 +76,7 @@
         // TODO: push into transformation
         boolean isPomArtifact = "pom".equals( packaging );
 
-        if ( !isPomArtifact )
-        {
-            ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
-            artifact.addMetadata( metadata );
-        }
+        ArtifactMetadata metadata = null;
 
         if ( updateReleaseInfo )
         {
@@ -95,6 +91,9 @@
             }
             else
             {
+                metadata = new ProjectArtifactMetadata( artifact, pomFile );
+                artifact.addMetadata( metadata );
+
                 File file = artifact.getFile();
 
                 // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile
@@ -102,6 +101,16 @@
                 if ( file != null && !file.isDirectory() )
                 {
                     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 );
+                    }
                 }
                 else if ( !attachedArtifacts.isEmpty() )
                 {
@@ -117,7 +126,13 @@
             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
             {
                 Artifact attached = (Artifact) i.next();
+
                 installer.install( attached.getFile(), attached, localRepository );
+
+                if( createChecksum )
+                {
+                    installCheckSum( attached.getFile(), attached, false );
+                }
             }
         }
         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=409320&r1=409319&r2=409320&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 May 25 00:37:26 2006
@@ -2,10 +2,14 @@
 
 import java.io.File;
 import java.io.FileReader;
+import java.util.Iterator;
 
 import org.apache.maven.model.Model;
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.metadata.ArtifactMetadata;
+import org.apache.maven.repository.digest.Digester;
 import org.codehaus.plexus.util.FileUtils;
 
 /*
@@ -183,6 +187,51 @@
                                       version + "." + "pom" );
 
         assertTrue( installedPom.exists() );
+    }
+
+    public void testInstallFileWithChecksum()
+        throws Exception
+    {
+        File testPom = new File( getBasedir(),
+                                 "target/test-classes/unit/install-file-with-checksum/" +
+                                 "plugin-config.xml" );
+
+        InstallFileMojo mojo = ( InstallFileMojo ) lookupMojo( "install-file", testPom );
+
+        assertNotNull( mojo );
+
+        assignValuesForParameter( mojo );
+
+        ArtifactRepository localRepo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
+
+        boolean createChecksum = ( (Boolean) getVariableValueFromObject( mojo, "createChecksum" ) ).booleanValue();
+
+        assertTrue( createChecksum );
+
+        mojo.execute();
+
+        //get the actual checksum of the artifact
+        String actualMd5Sum = mojo.getChecksum( file, Digester.MD5 );
+        String actualSha1Sum = mojo.getChecksum( file, Digester.SHA1 );
+
+        String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" +
+                          version + "/" + artifactId + "-" + version;
+
+        File installedArtifact = new File( localPath + "." + "jar" );
+
+        File md5 = new File( localPath + ".jar.md5" );
+        File sha1 = new File( localPath + ".jar.sha1" );
+
+        assertTrue( md5.exists() );
+        assertTrue( sha1.exists() );
+
+        String generatedMd5 = FileUtils.fileRead( md5 );
+        String generatedSha1 = FileUtils.fileRead( sha1 );
+
+        assertEquals( actualMd5Sum, generatedMd5 );
+        assertEquals( actualSha1Sum, generatedSha1 );
+
+        assertTrue( installedArtifact.exists() );
     }
     
     private void assignValuesForParameter( Object obj )

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=409320&r1=409319&r2=409320&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 May 25 00:37:26 2006
@@ -17,6 +17,7 @@
  */
 
 import java.io.File;
+import java.io.FileReader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -25,6 +26,10 @@
 import org.apache.maven.plugin.install.stubs.AttachedArtifactStub0;
 import org.apache.maven.plugin.install.stubs.InstallArtifactStub;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
+import org.apache.maven.repository.digest.Digester;
+import org.apache.maven.repository.digest.DefaultDigester;
+import org.apache.maven.artifact.metadata.ArtifactMetadata;
+import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.codehaus.plexus.util.FileUtils;
 
 /**
@@ -202,6 +207,89 @@
                                                groupId + "/" + artifact.getArtifactId() + "/" +
                                                artifact.getVersion() + "/" + artifact.getArtifactId() + "-" +
                                                artifact.getVersion() + "." + "jar" );
+
+       assertTrue( installedArtifact.exists() );
+   }
+
+   public void testBasicInstallAndCreateChecksumIsTrue()
+       throws Exception
+   {
+       File testPom = new File( getBasedir(),
+                                "target/test-classes/unit/basic-install-checksum/plugin-config.xml" );
+
+       InstallMojo mojo = ( InstallMojo ) lookupMojo( "install", testPom );
+
+       assertNotNull( mojo );
+
+       File file = new File( getBasedir(),
+                             "target/test-classes/unit/basic-install-checksum/" +
+                             "maven-test-jar.jar" );
+
+       artifact = ( InstallArtifactStub ) getVariableValueFromObject( mojo, "artifact" );
+
+       boolean createChecksum = ( (Boolean) getVariableValueFromObject( mojo, "createChecksum" ) ).booleanValue();
+
+       assertTrue( createChecksum );
+
+       artifact.setFile( file );
+
+       mojo.execute();
+
+       ArtifactMetadata metadata = null;
+       for( Iterator iter = artifact.getMetadataList().iterator(); iter.hasNext(); )
+       {
+           metadata = (ArtifactMetadata) iter.next();
+           if( metadata.getRemoteFilename().endsWith( "pom" ) )
+           {
+               break;
+           }
+       }
+
+       ArtifactRepository localRepo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
+
+       File pom = new File( localRepo.getBasedir(),
+                            localRepo.pathOfLocalRepositoryMetadata( metadata, localRepo ) );
+
+       assertTrue( pom.exists() );
+
+       //get the actual checksum of the pom
+       String actualPomMd5Sum = mojo.getChecksum( pom, Digester.MD5 );
+       String actualPomSha1Sum = mojo.getChecksum( pom, Digester.SHA1 );
+
+       //get the actual checksum of the artifact
+       String actualMd5Sum = mojo.getChecksum( file, Digester.MD5 );
+       String actualSha1Sum = mojo.getChecksum( file, Digester.SHA1 );
+
+       String groupId = dotToSlashReplacer( artifact.getGroupId() );
+
+       String packaging = getVariableValueFromObject( mojo, "packaging" ).toString();
+
+       String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
+                          artifact.getVersion() + "/" + artifact.getArtifactId() + "-" +
+                          artifact.getVersion();
+
+       File installedArtifact = new File( localPath + "." + packaging );
+
+       File pomMd5 = new File( localPath + ".pom.md5" );
+       File pomSha1 = new File( localPath + ".pom.sha1" );
+
+       File md5 = new File( localPath + "." + packaging + ".md5" );
+       File sha1 = new File( localPath + "." + packaging + ".sha1" );
+
+       assertTrue( pomMd5.exists() );
+       assertTrue( pomSha1.exists() );
+       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 );
+
+       assertEquals( actualMd5Sum, generatedMd5 );
+       assertEquals( actualSha1Sum, generatedSha1 );
+       assertEquals( actualPomMd5Sum, generatedPomMd5 );
+       assertEquals( actualPomSha1Sum, generatedPomSha1 );
 
        assertTrue( installedArtifact.exists() );
    }

Added: maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar?rev=409320&view=auto
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar (added)
+++ maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/maven-test-jar.jar Thu May 25 00:37:26 2006
@@ -0,0 +1 @@
+this is just a test jar
\ No newline at end of file

Added: maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/plugin-config.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/plugin-config.xml?rev=409320&view=auto
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/plugin-config.xml (added)
+++ maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/basic-install-checksum/plugin-config.xml Thu May 25 00:37:26 2006
@@ -0,0 +1,17 @@
+<project>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-install-plugin</artifactId>
+        <configuration>
+          <pomFile>${basedir}/src/test/resources/unit/basic-install-checksum/plugin-config.xml</pomFile>
+          <packaging>jar</packaging>
+          <artifact implementation="org.apache.maven.plugin.install.stubs.InstallArtifactStub" />
+          <attachedArtifacts />
+          <localRepository>${localRepository}</localRepository>
+          <createChecksum>true</createChecksum>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Added: maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar?rev=409320&view=auto
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar (added)
+++ maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar Thu May 25 00:37:26 2006
@@ -0,0 +1 @@
+this is just a test jar
\ No newline at end of file

Added: maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/plugin-config.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/plugin-config.xml?rev=409320&view=auto
==============================================================================
--- maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/plugin-config.xml (added)
+++ maven/plugins/trunk/maven-install-plugin/src/test/resources/unit/install-file-with-checksum/plugin-config.xml Thu May 25 00:37:26 2006
@@ -0,0 +1,19 @@
+<project>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-install-plugin</artifactId>
+        <configuration>
+          <groupId>org.apache.maven.test</groupId>
+          <artifactId>maven-install-file-test</artifactId>
+          <version>1.0-SNAPSHOT</version>
+          <packaging>jar</packaging>
+          <file>${basedir}/src/test/resources/unit/install-file-with-checksum/maven-test-jar.jar</file>
+          <localRepository>${localRepository}</localRepository>
+          <createChecksum>true</createChecksum>
+          <generatePom>true</generatePom>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>