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

svn commit: r1493496 - in /commons/sandbox/commons-digest-plugin/trunk: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/commons/ src/main/java/org/apache/commons/plugins/ src/main/java/org/apache/c...

Author: sebb
Date: Sun Jun 16 11:05:09 2013
New Revision: 1493496

URL: http://svn.apache.org/r1493496
Log:
Initial implementation of commons-digest plugin

Added:
    commons/sandbox/commons-digest-plugin/trunk/pom.xml   (with props)
    commons/sandbox/commons-digest-plugin/trunk/src/
    commons/sandbox/commons-digest-plugin/trunk/src/main/
    commons/sandbox/commons-digest-plugin/trunk/src/main/java/
    commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/
    commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/
    commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/
    commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/
    commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/
    commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/DigestMojo.java   (with props)

Added: commons/sandbox/commons-digest-plugin/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/commons-digest-plugin/trunk/pom.xml?rev=1493496&view=auto
==============================================================================
--- commons/sandbox/commons-digest-plugin/trunk/pom.xml (added)
+++ commons/sandbox/commons-digest-plugin/trunk/pom.xml Sun Jun 16 11:05:09 2013
@@ -0,0 +1,133 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+<!-- 
+  <parent>
+    <groupId>org.apache.commons</groupId>
+    <artifactId>commons-parent</artifactId>
+    <version>30</version>
+  </parent>
+ -->
+ 
+  <groupId>org.apache.commons</groupId>
+  <artifactId>commons-digest-plugin</artifactId>
+  <packaging>maven-plugin</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>Apache commons-digest-plugin Maven Mojo</name>
+  <url>http://maven.apache.org</url>
+
+<description>
+Apache Commons Digest Maven plugin provides digest (hashing) facilities for files
+</description>
+  <prerequisites>
+    <maven>${mavenVersion}</maven>
+  </prerequisites>
+
+   <properties>
+    <mavenVersion>2.2.1</mavenVersion>
+    <mavenPluginPluginVersion>3.2</mavenPluginPluginVersion>
+    <maven.compile.source>1.5</maven.compile.source>
+    <maven.compile.target>1.5</maven.compile.target>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>commons-codec</groupId>
+      <artifactId>commons-codec</artifactId>
+      <!-- 1.6 only requires Java 1.5; later versions require Java 1.6 -->
+      <version>1.6</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+      <version>${mavenVersion}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-project</artifactId>
+      <version>${mavenVersion}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.codehaus.plexus</groupId>
+          <artifactId>plexus-container-default</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.plugin-tools</groupId>
+      <artifactId>maven-plugin-annotations</artifactId>
+      <version>${mavenPluginPluginVersion}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>3.0.7</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <artifactId>maven-plugin-plugin</artifactId>
+          <version>${mavenPluginPluginVersion}</version>
+          <configuration>
+            <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
+          </configuration>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-compiler-plugin</artifactId>
+          <version>3.1</version>
+          <configuration>
+            <source>${maven.compile.source}</source>
+            <target>${maven.compile.target}</target>
+            <encoding>${commons.encoding}</encoding>
+          </configuration>
+        </plugin>
+        <plugin>
+          <groupId>org.codehaus.plexus</groupId>
+          <artifactId>plexus-component-metadata</artifactId>
+          <version>1.5.5</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-component-metadata</artifactId>
+        <executions>
+          <execution>
+            <goals>
+              <goal>generate-metadata</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-plugin-plugin</artifactId>
+        <configuration>
+          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
+        </configuration>
+        <executions>
+          <execution>
+            <id>generate-descriptor</id>
+            <goals>
+              <goal>descriptor</goal>
+            </goals>
+          </execution>
+          <execution>
+            <id>generated-helpmojo</id>
+            <goals>
+              <goal>helpmojo</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: commons/sandbox/commons-digest-plugin/trunk/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/DigestMojo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/DigestMojo.java?rev=1493496&view=auto
==============================================================================
--- commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/DigestMojo.java (added)
+++ commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/DigestMojo.java Sun Jun 16 11:05:09 2013
@@ -0,0 +1,157 @@
+/*
+ * Licensed 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.
+ */
+
+package org.apache.commons.plugins.digest;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.security.MessageDigest;
+import java.util.Set;
+
+import org.apache.commons.codec.binary.Hex;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.DirectoryScanner;
+
+/**
+ * Creates digests for files.
+ */
+@Mojo( name = "digest")
+public class DigestMojo extends AbstractMojo {
+
+    /**
+     * The maven project.
+     */
+    @Component
+    private MavenProject project;
+
+    /**
+     * List of files to include, default none.
+     */
+    @Parameter
+    private Set<String> includes;
+
+    /**
+     * List of files to exclude, default none.
+     */
+    @Parameter
+    private Set<String> excludes;
+
+    /**
+     * List of files to include, comma-separated (intended for command-line usage).
+     * Overrides includes and excludes; uses same syntax as for {@code <include>}
+     */
+    @Parameter (property="digest.files")
+    private String files;
+
+    /**
+     * Create the MD5 hash.
+     */
+    @Parameter( property = "digest.md5", defaultValue = "true" )
+    private boolean createMD5;
+
+    /**
+     * Create the SHA1 hash.
+     */
+    @Parameter( property = "digest.sha1", defaultValue = "true" )
+    private boolean createSHA1;
+
+    /**
+     * Append ' *filename' to the hash
+     */
+    @Parameter( property = "digest.appendFilename", defaultValue = "false" )
+    private boolean appendFilename;
+
+    public void execute() throws MojoExecutionException {
+        String files[] = scanForSources();
+        Log log = getLog();
+        if (files.length == 0) {
+            log.warn("No files found. Please configure at least one <include> item or use -Ddigest.files");
+        } else {
+            try {
+                for(String file : files) {
+                    if (createMD5) {
+                        createDigest("MD5", ".md5", file);
+                    }
+                    if (createSHA1) {
+                        createDigest("SHA-1", ".sha1", file);
+                    }
+                }
+            } catch (Exception ex) {
+                throw new MojoExecutionException("Failed to create hash", ex);
+            }
+        }
+    }
+
+    private void createDigest(String algorithm, String extension, String file) throws Exception {
+        FileInputStream is = new FileInputStream(file);
+        PrintWriter pw = new PrintWriter(file+extension, "UTF-8");
+        // Unfortunately DigestUtils.getDigest is not public
+        pw.print(digestHex(MessageDigest.getInstance(algorithm), is));
+        if (appendFilename) {
+            pw.println(" *" + file);
+        } else {
+            pw.println();            
+        }
+        is.close();
+        pw.close();
+    }
+
+    private static String digestHex(MessageDigest digest, InputStream data) throws IOException {
+        return Hex.encodeHexString(digest(digest, data));
+    }
+
+    // Unfortunately, the Codec version is private
+    private static byte[] digest(MessageDigest digest, InputStream data) throws IOException {
+        byte[] buffer = new byte[1024];
+        int read = data.read(buffer);
+        while (read > -1) {
+            digest.update(buffer, 0, read);
+            read = data.read(buffer);
+        }
+        return digest.digest();
+    }
+
+    private String[]  scanForSources() {
+        DirectoryScanner ds = new DirectoryScanner();
+        ds.setFollowSymlinks( true );
+        ds.setBasedir( project.getBasedir() );
+        String[] inc;
+        if (files != null) { // Overrides includes / excludes
+            getLog().debug("files="+files);
+            inc=files.split(",");
+        } else {
+            if ( includes == null || includes.isEmpty() ) {
+                inc = new String[0]; // overrides default of **
+            } else {
+                inc = includes.toArray( new String[includes.size()] );
+            }
+            if (excludes != null) {
+                String[] excl = excludes.toArray( new String[excludes.size()] );
+                ds.setExcludes( excl );
+            }
+        }
+        ds.setIncludes( inc );
+        ds.addDefaultExcludes(); // TODO should this be optional?
+        ds.scan();
+        return ds.getIncludedFiles();
+    }
+}

Propchange: commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/DigestMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/commons-digest-plugin/trunk/src/main/java/org/apache/commons/plugins/digest/DigestMojo.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision