You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by se...@apache.org on 2013/06/25 03:15:46 UTC

svn commit: r1496326 - in /maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest: DigestMojo.java HelperMojo.java

Author: sebb
Date: Tue Jun 25 01:15:45 2013
New Revision: 1496326

URL: http://svn.apache.org/r1496326
Log:
Maven layout conventions

Modified:
    maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/DigestMojo.java
    maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java

Modified: maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/DigestMojo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/DigestMojo.java?rev=1496326&r1=1496325&r2=1496326&view=diff
==============================================================================
--- maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/DigestMojo.java (original)
+++ maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/DigestMojo.java Tue Jun 25 01:15:45 2013
@@ -34,159 +34,217 @@ import org.apache.maven.project.MavenPro
 import org.codehaus.plexus.util.DirectoryScanner;
 
 /**
- * Creates digests (MD5 and SHA1 by default) for files specified
- * by the configured includes and excludes.
- * Also allows specification of a list of files on the command line.
+ * Creates digests (MD5 and SHA1 by default) for files specified by the configured includes and excludes. Also allows
+ * specification of a list of files on the command line.
  */
-@Mojo( name = "digest")
-public class DigestMojo extends AbstractMojo {
+@Mojo( name = "digest" )
+public class DigestMojo
+    extends AbstractMojo
+{
+
+    // ----------------------------------------------------------------------
+    // Mojo components
+    // ----------------------------------------------------------------------
 
     @Component
     private MavenProject project;
 
+    // ----------------------------------------------------------------------
+    // Mojo parameters
+    // ----------------------------------------------------------------------
+
     /**
-     * List of files to include, default none.
-     * Standard Maven wildcard patterns apply.
-     * Patterns are assumed to be relative to the project base directory.
+     * List of files to include, default none. Standard Maven wildcard patterns apply. Patterns are assumed to be
+     * relative to the project base directory.
      */
     @Parameter
     private Set<String> includes;
 
     /**
-     * List of files to exclude, default none.
-     * Standard Maven wildcard patterns apply.
-     * Patterns are assumed to be relative to the project base directory.
+     * List of files to exclude, default none. Standard Maven wildcard patterns apply. Patterns are assumed to be
+     * relative to the project base directory.
      */
     @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>}
-     * Patterns are assumed to be relative to the project base directory.
+     * List of files to include, comma-separated (intended for command-line usage). Overrides includes and excludes;
+     * uses same syntax as for {@code <include>} Patterns are assumed to be relative to the project base directory.
      */
-    @Parameter (property="maven.digest.files")
+    @Parameter( property = "maven.digest.files" )
     private String files;
 
     /**
-     * List of digests (algorithms) to create, comma-separated (intended for command-line usage).
-     * Overrides algorithms; uses same syntax
+     * List of digests (algorithms) to create, comma-separated (intended for command-line usage). Overrides algorithms;
+     * uses same syntax
      */
-    @Parameter (property="maven.digest.digests")
+    @Parameter( property = "maven.digest.digests" )
     private String digests;
 
     /**
-     * The list of algorithm names with which to create digests.
-     * If none specified, the default is {@code MD5} and {@code SHA1}.
-     * By default the file extension is assumed to be the algorithm name
-     * converted to lower-case, and any "-" characters removed.
-     * The extension name can be provided by suffixing the algorithm name
-     * with ">" followed by the extension, for example: "SHA-1>sha".
+     * The list of algorithm names with which to create digests. If none specified, the default is {@code MD5} and
+     * {@code SHA1}. By default the file extension is assumed to be the algorithm name converted to lower-case, and any
+     * "-" characters removed. The extension name can be provided by suffixing the algorithm name with ">" followed by
+     * the extension, for example: "SHA-1>sha".
      */
     @Parameter
     private Set<String> algorithms;
 
+    // ----------------------------------------------------------------------
+    // Mojo options
+    // ----------------------------------------------------------------------
+
     /**
      * Whether to append ' *filename' to the hash in the generated file, default {@code false}
      */
     @Parameter( property = "maven.digest.appendFilename", defaultValue = "false" )
     private boolean appendFilename;
 
-    public void execute() throws MojoExecutionException {
+    // ----------------------------------------------------------------------
+    // Public methods
+    // ----------------------------------------------------------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    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 -Dmaven.digest.files");
-        } else {
-            if (digests != null && digests.length() > 0) {
-                String [] digest = digests.split(",");
-                algorithms = new HashSet<String>(digest.length);
-                for (String d : digest) {
-                   algorithms.add(d);
+        if ( files.length == 0 )
+        {
+            log.warn( "No files found. Please configure at least one <include> item or use -Dmaven.digest.files" );
+        }
+        else
+        {
+            if ( digests != null && digests.length() > 0 )
+            {
+                String[] digest = digests.split( "," );
+                algorithms = new HashSet<String>( digest.length );
+                for ( String d : digest )
+                {
+                    algorithms.add( d );
                 }
             }
-            if (algorithms == null || algorithms.size() == 0) {
-                algorithms = new HashSet<String>(2);
-                algorithms.add("MD5");
-                algorithms.add("SHA-1");
-            }
-            try {
-                for(String file : files) {
-                    for(String algorithm : algorithms) {
-                        String[] parts = algorithm.split(">");
+            if ( algorithms == null || algorithms.size() == 0 )
+            {
+                algorithms = new HashSet<String>( 2 );
+                algorithms.add( "MD5" );
+                algorithms.add( "SHA-1" );
+            }
+            try
+            {
+                for ( String file : files )
+                {
+                    for ( String algorithm : algorithms )
+                    {
+                        String[] parts = algorithm.split( ">" );
                         String extension;
-                        if (parts.length == 2) {
+                        if ( parts.length == 2 )
+                        {
                             algorithm = parts[0];
                             extension = parts[1];
-                        } else {
-                            extension = getExtension(algorithm);
                         }
-                        createDigest(algorithm, extension, file);         
+                        else
+                        {
+                            extension = getExtension( algorithm );
+                        }
+                        createDigest( algorithm, extension, file );
                     }
                 }
-            } catch (Exception ex) {
-                throw new MojoExecutionException("Failed to create hash", ex);
+            }
+            catch ( Exception ex )
+            {
+                throw new MojoExecutionException( "Failed to create hash", ex );
             }
         }
     }
 
+    // ----------------------------------------------------------------------
+    // Protected methods
+    // ----------------------------------------------------------------------
+
+    // ----------------------------------------------------------------------
+    // Private methods
+    // ----------------------------------------------------------------------
+
     /**
-     * Creates the extension from the algortithm name, by converting to lower-case
-     * and dropping any "-" characters. The result is prefixed with ".".
+     * Creates the extension from the algortithm name, by converting to lower-case and dropping any "-" characters. The
+     * result is prefixed with ".".
      * 
      * @param algorithm the algorithm name
      * @return the extension, e.g. ".md5", ".sha1"
      */
-    private String getExtension(String algorithm) {
-        return "." + algorithm.toLowerCase(Locale.ENGLISH).replace("-", "");
+    private String getExtension( String algorithm )
+    {
+        return "." + algorithm.toLowerCase( Locale.ENGLISH ).replace( "-", "" );
     }
 
-    private void createDigest(String algorithm, String extension, String file) throws Exception {
+    private void createDigest( String algorithm, String extension, String file )
+        throws Exception
+    {
         // Unfortunately DigestUtils.getDigest is not public
         // Do this before opening file in case not found
-        MessageDigest digest = MessageDigest.getInstance(algorithm);
-        FileInputStream is = new FileInputStream(file);
-        PrintWriter pw = new PrintWriter(file+extension, "UTF-8");
-        pw.print(digestHex(digest, is));
-        if (appendFilename) {
-            pw.println(" *" + file);
-        } else {
-            pw.println();            
+        MessageDigest digest = MessageDigest.getInstance( algorithm );
+        FileInputStream is = new FileInputStream( file );
+        PrintWriter pw = new PrintWriter( file + extension, "UTF-8" );
+        pw.print( digestHex( digest, 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));
+    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 {
+    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);
+        int read = data.read( buffer );
+        while ( read > -1 )
+        {
+            digest.update( buffer, 0, read );
+            read = data.read( buffer );
         }
         return digest.digest();
     }
 
-    private String[]  scanForSources() {
+    private String[] scanForSources()
+    {
         DirectoryScanner ds = new DirectoryScanner();
         ds.setFollowSymlinks( true );
         ds.setBasedir( project.getBasedir() ); // Cannot be omitted; implies that includes/excludes are relative
         String[] inc;
-        if (files != null) { // Overrides includes / excludes
-            getLog().debug("files="+files);
-            inc=files.split(",");
-        } else {
-            if ( includes == null || includes.isEmpty() ) {
+        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 {
+            }
+            else
+            {
                 inc = includes.toArray( new String[includes.size()] );
             }
-            if (excludes != null) {
+            if ( excludes != null )
+            {
                 String[] excl = excludes.toArray( new String[excludes.size()] );
                 ds.setExcludes( excl );
             }
@@ -196,4 +254,8 @@ public class DigestMojo extends Abstract
         ds.scan();
         return ds.getIncludedFiles();
     }
+
+    // ----------------------------------------------------------------------
+    // Static methods
+    // ----------------------------------------------------------------------
 }

Modified: maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java?rev=1496326&r1=1496325&r2=1496326&view=diff
==============================================================================
--- maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java (original)
+++ maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java Tue Jun 25 01:15:45 2013
@@ -21,47 +21,82 @@ package org.apache.maven.plugins.digest;
 import java.lang.reflect.Field;
 
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.help.DescribeMojo;
 import org.apache.maven.project.MavenProject;
 
 /**
- * Helper Mojo that extends the standard Maven help plugin describe goal.
- * This is needed because the generated help mojo 
+ * Helper Mojo that extends the standard Maven help plugin describe goal. This is needed because the generated help mojo
  * does not handle annotation property names at present.
  */
-@Mojo (name = "helper")
-public class HelperMojo extends DescribeMojo {
+@Mojo( name = "helper" )
+public class HelperMojo
+    extends DescribeMojo
+{
+
+    // ----------------------------------------------------------------------
+    // Mojo components
+    // ----------------------------------------------------------------------
 
     @Component
     private MavenProject myProject; // Must not use same name as DescribeMojo
 
-   /**
-     * @throws MojoExecutionException  
-     * @throws MojoFailureException 
+    // ----------------------------------------------------------------------
+    // Mojo parameters
+    // ----------------------------------------------------------------------
+
+    // ----------------------------------------------------------------------
+    // Mojo options
+    // ----------------------------------------------------------------------
+
+    // ----------------------------------------------------------------------
+    // Public methods
+    // ----------------------------------------------------------------------
+
+    /**
+     * {@inheritDoc}
      */
     @Override
-    public void execute() throws MojoExecutionException, MojoFailureException {
+    public void execute()
+        throws MojoExecutionException
+    {
         Field f = null;
         boolean isAccessible = true; // assume accessible
-        try {
+        try
+        {
             // Unfortunately the plugin field is private
-            f = DescribeMojo.class.getDeclaredField("plugin");
+            f = DescribeMojo.class.getDeclaredField( "plugin" );
             isAccessible = f.isAccessible();
-            if (!isAccessible) {
-                f.setAccessible(true);
+            if ( !isAccessible )
+            {
+                f.setAccessible( true );
             }
             String plugin = myProject.getGroupId() + ":" + myProject.getArtifactId();
-            f.set(this, plugin);
-            super.execute();        
-        } catch (Exception e) {
-            throw new MojoExecutionException("Could not set up plugin details");
-        } finally {
-            if (f != null && !isAccessible) {
-                f.setAccessible(isAccessible); // reset accessibility (prob not needed)
+            f.set( this, plugin );
+            super.execute();
+        }
+        catch ( Exception e )
+        {
+            throw new MojoExecutionException( "Could not set up plugin details" );
+        }
+        finally
+        {
+            if ( f != null && !isAccessible )
+            {
+                f.setAccessible( isAccessible ); // reset accessibility (prob not needed)
             }
         }
     }
+    // ----------------------------------------------------------------------
+    // Protected methods
+    // ----------------------------------------------------------------------
+
+    // ----------------------------------------------------------------------
+    // Private methods
+    // ----------------------------------------------------------------------
+
+    // ----------------------------------------------------------------------
+    // Static methods
+    // ----------------------------------------------------------------------
 }