You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ch...@apache.org on 2008/08/04 16:25:33 UTC

svn commit: r682400 - in /servicemix/maven-plugins/checksum-maven-plugin/trunk/src: main/java/org/apache/servicemix/maven/plugin/checksum/ site/apt/

Author: chirino
Date: Mon Aug  4 07:25:32 2008
New Revision: 682400

URL: http://svn.apache.org/viewvc?rev=682400&view=rev
Log:
Combined the generator and validation into 1 Mojo and they now execute in the validate phase.  So validation now happens very early in the build
and the pom configuraiton is slightly easier.


Removed:
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumGeneratorMojo.java
Modified:
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/index.apt
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/usage.apt

Modified: servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java?rev=682400&r1=682399&r2=682400&view=diff
==============================================================================
--- servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java (original)
+++ servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java Mon Aug  4 07:25:32 2008
@@ -19,6 +19,7 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -44,12 +45,18 @@
  * Validates the checksums of the dependencies of the project
  * against the checksums.txt file.
  * 
- * @goal validate-checksums
- * @phase process-classes 
+ * This plugin can also be used to add all the checksums of the 
+ * dependencies of the current build to the checksum.txt file.
+ * 
+ * @requiresDependencyResolution
+ * @goal validate
+ * @phase validate 
  * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 
  */
 public class ChecksumValidatorMojo extends AbstractMojo {
 
+    static char hexTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
     /**
      * The maven project.
      * 
@@ -110,37 +117,12 @@
      */
     private String checksumAlgorithm;
 
-    public void execute() throws MojoExecutionException {
-        
-        LinkedHashMap checksums = getCheckSums();
-        
-        boolean failed = false;
-        
-        for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
-        {
-            Artifact artifact = (Artifact) it.next();
-            Artifact pom = getPomArtifact( artifact );
-            
-            
-            String sum = checksum(pom.getFile());
-            String key = key(pom);
-            List list = (List)checksums.get(key);
-            if( list == null ) {
-                list = (List)checksums.get(keyAnyVersion(pom));
-            }
-            if( list == null ) {
-                getLog().error("No checksum specified for "+key+" in "+this.checksums+" ("+sum+")" );
-                failed = true;
-            } else if ( !list.contains(sum) && !list.contains("*") ) {
-                getLog().error("Checksum mismatch for "+key+" in "+this.checksums+" expected one of "+list+" but was "+sum );
-                failed = true;
-            }
-        }
-        
-        if( failed ) {
-            throw new MojoExecutionException("Invalid checksum(s) found.. see previous error messages for more details.");
-        }
-    }
+    /**
+     * Should we generate the checksum file instead of validating against it? 
+     * 
+     * @parameter default-value="false"
+     */
+    private boolean generate;
 
     protected String key(Artifact pom) {
         StringBuffer sb = new StringBuffer();
@@ -216,7 +198,7 @@
         return null;
     }
 
-    String checksum(File file) throws MojoExecutionException {
+    protected String checksum(File file) throws MojoExecutionException {
         try {
             MessageDigest md = MessageDigest.getInstance(checksumAlgorithm);
             FileInputStream is=null;
@@ -244,8 +226,6 @@
             throw new MojoExecutionException("Invalid checksum algorithm: "+checksumAlgorithm, e);
         }
     }
-
-    static char hexTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
     
     static String toString(byte[] digest) {
         StringBuilder rc = new StringBuilder(digest.length*2);
@@ -256,4 +236,137 @@
         return rc.toString();
     }
 
+    public void execute() throws MojoExecutionException {
+        if( generate ) {
+            generate();
+        } else {
+            validate();
+        }
+    }
+
+    private void validate() throws MojoExecutionException {
+        LinkedHashMap checksums = getCheckSums();
+        
+        boolean failed = false;
+        
+        for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+            Artifact pom = getPomArtifact( artifact );
+            failed |= validateArtifact(checksums, pom);
+            failed |= validateArtifact(checksums, artifact);
+        }
+        
+        if( failed ) {
+            throw new MojoExecutionException("Invalid checksum(s) found.. see previous error messages for more details.");
+        }
+    }
+
+    /**
+     * 
+     * @param checksums
+     * @param artifact
+     * @return - true if validation failed.
+     * @throws MojoExecutionException
+     */
+    private boolean validateArtifact(LinkedHashMap checksums, Artifact artifact) throws MojoExecutionException {
+        String sum = checksum(artifact.getFile());
+        String key = key(artifact);
+        List list = (List)checksums.get(key);
+        if( list == null ) {
+            list = (List)checksums.get(keyAnyVersion(artifact));
+        }
+        if( list == null ) {
+            getLog().error("No checksum specified for "+key+" in "+this.checksums+" ("+sum+")" );
+            return true;
+        } else if ( !list.contains(sum) && !list.contains("*") ) {
+            getLog().error("Checksum mismatch for "+key+" in "+this.checksums+" expected one of "+list+" but was "+sum );
+            return true;
+        }
+        return false;
+    }
+
+    public void generate() throws MojoExecutionException {
+        
+        LinkedHashMap checksums = new LinkedHashMap();
+        
+        boolean modified=true;
+        try { 
+            checksums = getCheckSums();
+            modified=false;
+        } catch ( MojoExecutionException e) {
+        }
+        
+        for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+            Artifact pom = getPomArtifact( artifact );
+
+            modified |= generateArtifact(checksums, pom);
+            modified |= generateArtifact(checksums, artifact);
+            
+        }
+        
+        if( modified ) {
+            
+            // put it back to property file format
+            Properties p = new Properties();
+
+            for (Iterator iterator = checksums.entrySet().iterator(); iterator.hasNext();) {
+                Map.Entry i = (Map.Entry)iterator.next();            
+                StringBuffer b = new StringBuffer();
+                for (Iterator iterator2 = ((List)i.getValue()).iterator(); iterator2.hasNext();) {
+                    String s = (String)iterator2.next();            
+                    if( b.length()!=0 ) {
+                        b.append("|");
+                    }
+                    b.append(s);
+                }
+                p.put(i.getKey(), b.toString());
+            }
+            
+            // Store it.
+            FileOutputStream os=null;
+            try {
+                os = new FileOutputStream(this.checksums);
+                p.store(os, "");
+            } catch (Throwable e) {
+                throw new MojoExecutionException("Could not write: "+this.checksums);
+            } finally {
+                try {
+                    os.close();
+                } catch (Throwable ignore ) {
+                }
+            }
+        }
+    }
+
+    /**
+     * 
+     * @param checksums
+     * @param artifact
+     * @return true if this method modified the checksums
+     * @throws MojoExecutionException
+     */
+    private boolean generateArtifact(HashMap checksums, Artifact artifact) throws MojoExecutionException {
+        String sum = checksum(artifact.getFile());
+        List sums = (List)checksums.get(key(artifact));
+        if( sums == null ) {
+            sums = (List)checksums.get(keyAnyVersion(artifact));
+        }
+        if( sums == null ) {
+            sums = new ArrayList();
+            sums.add(sum);
+            checksums.put(key(artifact), sums);
+            return true;
+        } else {
+            if ( !sums.contains(sum) && !sums.contains("*") ) {
+                sums.add(sum);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    
 }

Modified: servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/index.apt
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/index.apt?rev=682400&r1=682399&r2=682400&view=diff
==============================================================================
--- servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/index.apt (original)
+++ servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/index.apt Mon Aug  4 07:25:32 2008
@@ -33,13 +33,8 @@
 
 * Goals Overview
 
-  The Checksum Plugin has two goals:
-
-  * {{{generate-checksums-mojo.html}checksum-maven-plugin:generate-checksums}} is bound to the <<<compile>>> phase and
-  is used to generate the checksum.txt file.
-
-  * {{{validate-checksums-mojo.html}checksum-maven-plugin:validate-checksums}} is bound to the <<<process-classes>>> phase and
-  is used to validate the Project dependencies against the checksum.txt file.
+  * {{{validate-checksums-mojo.html}checksum-maven-plugin:validate}} is bound to the <<<validate>>> phase and
+  is used to validate or generate the Project dependencies against the checksum.txt file.
 
 * Usage
 

Modified: servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/usage.apt
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/usage.apt?rev=682400&r1=682399&r2=682400&view=diff
==============================================================================
--- servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/usage.apt (original)
+++ servicemix/maven-plugins/checksum-maven-plugin/trunk/src/site/apt/usage.apt Mon Aug  4 07:25:32 2008
@@ -34,10 +34,10 @@
         <groupId>org.apache.servicemix.tooling</groupId>
         <artifactId>checksum-maven-plugin</artifactId>
         <version>1.0-SNAPSHOT</version>
-		    <executions>
+ 	    <executions>
           <execution>
             <goals>
-              <goal>validate-checksums</goal>
+              <goal>validate</goal>
             </goals>
           </execution>
         </executions>
@@ -51,17 +51,20 @@
     <profile>
       <id>generate-checksums</id>
       <build>
-        <defaultGoal>compile</defaultGoal>
+        <defaultGoal>validate</defaultGoal>
         <plugins>
           <plugin>
             <groupId>org.apache.servicemix.tooling</groupId>
             <artifactId>checksum-maven-plugin</artifactId>
             <version>1.0-SNAPSHOT</version>
-    		    <executions>
+            <executions>
               <execution>
                 <goals>
-                  <goal>generate-checksums</goal>
+                  <goal>validate</goal>
                 </goals>
+                <configuration>
+                  <generate>true</generate>
+                </configuration>
               </execution>
             </executions>
           </plugin>
@@ -88,7 +91,7 @@
 
 * Validating Checksums
 
-  Checksums get validated once the build hits the <<<process-classes>>> build phase.
+  Checksums get validated once the build hits the <<<validate>>> build phase.
 
 +-----
 mvn install