You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kr...@apache.org on 2014/10/22 08:58:48 UTC

svn commit: r1633544 - in /maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly: archive/task/ filter/ format/ utils/

Author: krosenvold
Date: Wed Oct 22 06:58:47 2014
New Revision: 1633544

URL: http://svn.apache.org/r1633544
Log:
Minor cleanups

Added:
    maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReaderFormatter.java
Modified:
    maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/archive/task/AddFileSetsTask.java
    maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/filter/SimpleAggregatingDescriptorHandler.java
    maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
    maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java

Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/archive/task/AddFileSetsTask.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/archive/task/AddFileSetsTask.java?rev=1633544&r1=1633543&r2=1633544&view=diff
==============================================================================
--- maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/archive/task/AddFileSetsTask.java (original)
+++ maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/archive/task/AddFileSetsTask.java Wed Oct 22 06:58:47 2014
@@ -20,28 +20,20 @@ package org.apache.maven.plugin.assembly
  */
 
 import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
-import org.apache.commons.io.input.ReaderInputStream;
 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
-import org.apache.maven.plugin.assembly.format.FileFormatter;
+import org.apache.maven.plugin.assembly.format.ReaderFormatter;
 import org.apache.maven.plugin.assembly.model.FileSet;
 import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
-import org.apache.maven.plugin.assembly.utils.LineEndings;
-import org.apache.maven.plugin.assembly.utils.LineEndingsUtils;
 import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.archiver.Archiver;
 import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
-import org.codehaus.plexus.components.io.resources.PlexusIoResource;
 import org.codehaus.plexus.logging.Logger;
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 
@@ -139,7 +131,8 @@ public class AddFileSetsTask
 
         if ( fileSetDir.exists() )
         {
-            InputStreamTransformer fileSetTransformers = getFileSetTransformers( configSource, fileSet );
+            InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( configSource, fileSet.isFiltered(),
+                                                                                                 fileSet.getLineEnding() );
             if (fileSetTransformers == null)
             {
                 logger.debug( "NOT reformatting any files in " + fileSetDir );
@@ -179,65 +172,6 @@ public class AddFileSetsTask
         }
     }
 
-    private InputStreamTransformer getFileSetTransformers( final AssemblerConfigurationSource configSource,
-                                                           final org.apache.maven.plugin.assembly.model.FileSet set )
-        throws AssemblyFormattingException
-    {
-        final String lineEndingHint = set.getLineEnding();
-
-        String lineEnding = LineEndingsUtils.getLineEndingCharacters( lineEndingHint );
-
-        if ( ( lineEnding != null ) || set.isFiltered() )
-        {
-            InputStreamTransformer isf = new InputStreamTransformer()
-            {
-                public InputStream transform( PlexusIoResource plexusIoResource, InputStream inputStream )
-                    throws IOException
-                {
-                    FileFormatter fileFormatter = new FileFormatter( configSource, logger );
-
-                    if ( set.isFiltered() )
-                    {
-                        final String encoding = configSource.getEncoding();
-
-                        Reader source = encoding != null ? new InputStreamReader( inputStream, encoding )
-                            : new InputStreamReader( inputStream ); // wtf platform encoding ?
-                        try
-                        {
-                            Reader filtered = fileFormatter.doReaderFilter( source, plexusIoResource.getName(),
-                                                                            configSource.getEncoding(),
-                                                                            configSource.getEscapeString(),
-                                                                            configSource.getDelimiters() );
-                            final ReaderInputStream readerInputStream = encoding != null ? new ReaderInputStream( filtered, encoding)
-                                : new ReaderInputStream( filtered );
-
-                            LineEndings lineEnding = LineEndingsUtils.getLineEnding( lineEndingHint );
-                            if ( !LineEndings.keep.equals( lineEnding ) )
-                            {
-                                return LineEndingsUtils.lineEndingConverter( readerInputStream, lineEnding );
-
-                            }
-                            return readerInputStream;
-
-                        }
-                        catch ( AssemblyFormattingException e )
-                        {
-                            throw new IOException( e );
-                        }
-
-                    }
-                    else
-                    {
-                        return inputStream;
-                    }
-                }
-            };
-            return isf;
-        }
-        return null;
-    }
-
-
     protected File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir )
         throws ArchiveCreationException, AssemblyFormattingException
     {

Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/filter/SimpleAggregatingDescriptorHandler.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/filter/SimpleAggregatingDescriptorHandler.java?rev=1633544&r1=1633543&r2=1633544&view=diff
==============================================================================
--- maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/filter/SimpleAggregatingDescriptorHandler.java (original)
+++ maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/filter/SimpleAggregatingDescriptorHandler.java Wed Oct 22 06:58:47 2014
@@ -19,18 +19,6 @@ package org.apache.maven.plugin.assembly
  * under the License.
  */
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-
 import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
 import org.codehaus.plexus.archiver.Archiver;
 import org.codehaus.plexus.archiver.ArchiverException;
@@ -42,6 +30,20 @@ import org.codehaus.plexus.logging.Logge
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 import org.codehaus.plexus.util.IOUtil;
 
+import javax.annotation.Nonnull;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
 /**
  * @version $Id$
  */
@@ -56,7 +58,7 @@ public class SimpleAggregatingDescriptor
 
     private String outputPath;
 
-    private final String commentChars = "#";
+    private static final String commentChars = "#";
 
     // calculated, temporary values.
 
@@ -107,8 +109,11 @@ public class SimpleAggregatingDescriptor
             f = File.createTempFile( "maven-assembly-plugin", "tmp" );
             f.deleteOnExit();
 
-            // FIXME if it is a properties file, encoding should be ISO-8859-1
-            writer = new FileWriter( f ); // platform encoding
+
+            boolean isProperty = AssemblyFileUtils.isPropertyFile(f);
+            FileOutputStream fos = new FileOutputStream( f );
+            writer = isProperty ? new OutputStreamWriter( fos,  "ISO-8859-1")
+                : new OutputStreamWriter( fos); // Still platform encoding
 
             writer.write( commentChars + " Aggregated on " + new Date() + " from: " );
 
@@ -186,8 +191,10 @@ public class SimpleAggregatingDescriptor
         Reader reader = null;
         try
         {
-            // FIXME if it is a properties file, encoding should be ISO-8859-1
-            reader = new InputStreamReader( fileInfo.getContents() ); // platform encoding
+            boolean isProperty = AssemblyFileUtils.isPropertyFile(fileInfo.getName());
+
+            reader = isProperty ? new InputStreamReader( fileInfo.getContents(), "ISO-8859-1" ) :
+            new InputStreamReader( fileInfo.getContents() ); // platform encoding
 
             IOUtil.copy( reader, writer );
         }

Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java?rev=1633544&r1=1633543&r2=1633544&view=diff
==============================================================================
--- maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java (original)
+++ maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java Wed Oct 22 06:58:47 2014
@@ -22,7 +22,6 @@ package org.apache.maven.plugin.assembly
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.Reader;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Locale;
@@ -36,10 +35,8 @@ import org.apache.maven.plugin.assembly.
 import org.apache.maven.plugin.assembly.utils.LineEndingsUtils;
 import org.apache.maven.shared.filtering.MavenFileFilterRequest;
 import org.apache.maven.shared.filtering.MavenFilteringException;
-import org.apache.maven.shared.filtering.MavenReaderFilterRequest;
 import org.codehaus.plexus.logging.Logger;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.StringUtils;
 
@@ -59,16 +56,10 @@ public class FileFormatter
         this.logger = logger;
     }
 
-    public File format( File source, boolean filter, String lineEnding, String encoding )
-        throws AssemblyFormattingException
-    {
-        return format( source, filter, lineEnding, configSource.getTemporaryRootDirectory(), encoding );
-    }
-
-    private File format( @Nonnull File source, boolean filter, String lineEndingCharacters, @Nullable File tempRoot,
-                        String encoding )
+    public File format( @Nonnull File source, boolean filter, String lineEnding, String encoding )
         throws AssemblyFormattingException
     {
+        File tempRoot = configSource.getTemporaryRootDirectory();
         AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot );
 
         File result = source;
@@ -85,10 +76,10 @@ public class FileFormatter
                 doFileFilter( source, tempRoot, encoding, configSource.getEscapeString(), configSource.getDelimiters() );
         }
 
-        LineEndings lineEnding = LineEndingsUtils.getLineEnding( lineEndingCharacters );
-        if ( !LineEndings.keep.equals( lineEnding ) )
+        LineEndings lineEnding1 = LineEndingsUtils.getLineEnding( lineEnding );
+        if ( !LineEndings.keep.equals( lineEnding1 ) )
         {
-            result = formatLineEndings( lineEnding, result, tempRoot, encoding );
+            result = formatLineEndings( lineEnding1, result, tempRoot, encoding );
         }
 
         return result;
@@ -145,58 +136,10 @@ public class FileFormatter
         }
     }
 
-    public Reader doReaderFilter( @Nonnull Reader source, String sourceName, String encoding, String escapeString,
-                               List<String> delimiters )
-        throws AssemblyFormattingException
-    {
-        try
-        {
-            // @todo this test can be improved
-            boolean isPropertiesFile = sourceName.toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
-
-            MavenReaderFilterRequest filterRequest =
-                new MavenReaderFilterRequest( source, true, configSource.getProject(), configSource.getFilters(),
-                                            isPropertiesFile, encoding, configSource.getMavenSession(), null );
-            filterRequest.setEscapeString( escapeString );
-
-            // if these are NOT set, just use the defaults, which are '${*}' and '@'.
-            if ( delimiters != null && !delimiters.isEmpty() )
-            {
-                LinkedHashSet<String> delims = new LinkedHashSet<String>();
-                for ( String delim : delimiters )
-                {
-                    if ( delim == null )
-                    {
-                        // FIXME: ${filter:*} could also trigger this condition. Need a better long-term solution.
-                        delims.add( "${*}" );
-                    }
-                    else
-                    {
-                        delims.add( delim );
-                    }
-                }
-
-                filterRequest.setDelimiters( delims );
-            }
-            else
-            {
-                filterRequest.setDelimiters( filterRequest.getDelimiters() );
-            }
-
-            filterRequest.setInjectProjectBuildFilters( configSource.isIncludeProjectBuildFilters() );
-            return configSource.getMavenReaderFilter().filter( filterRequest );
-        }
-        catch ( MavenFilteringException e )
-        {
-            throw new AssemblyFormattingException( "Error filtering file '" + source + "': " + e.getMessage(), e );
-        }
-    }
-
 
     private File formatLineEndings( LineEndings lineEnding, File source, File tempRoot, String encoding )
         throws AssemblyFormattingException
     {
-        Reader contentReader = null;
         try
         {
             File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot );
@@ -213,9 +156,5 @@ public class FileFormatter
         {
             throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(), e );
         }
-        finally
-        {
-            IOUtil.close( contentReader );
-        }
     }
 }

Added: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReaderFormatter.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReaderFormatter.java?rev=1633544&view=auto
==============================================================================
--- maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReaderFormatter.java (added)
+++ maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReaderFormatter.java Wed Oct 22 06:58:47 2014
@@ -0,0 +1,144 @@
+package org.apache.maven.plugin.assembly.format;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.commons.io.input.ReaderInputStream;
+import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
+import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
+import org.apache.maven.plugin.assembly.utils.LineEndings;
+import org.apache.maven.plugin.assembly.utils.LineEndingsUtils;
+import org.apache.maven.shared.filtering.MavenFilteringException;
+import org.apache.maven.shared.filtering.MavenReaderFilterRequest;
+import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
+import org.codehaus.plexus.components.io.resources.PlexusIoResource;
+
+import javax.annotation.Nonnull;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.LinkedHashSet;
+import java.util.List;
+
+public class ReaderFormatter
+{
+    public static Reader createReaderFilter( @Nonnull Reader source, String sourceName, String encoding,
+                                             String escapeString, List<String> delimiters,
+                                             AssemblerConfigurationSource configSource )
+        throws AssemblyFormattingException
+    {
+        try
+        {
+            // @todo this test can be improved
+            boolean isPropertiesFile = AssemblyFileUtils.isPropertyFile( sourceName );
+
+            MavenReaderFilterRequest filterRequest =
+                new MavenReaderFilterRequest( source, true, configSource.getProject(), configSource.getFilters(),
+                                            isPropertiesFile, encoding, configSource.getMavenSession(), null );
+            filterRequest.setEscapeString( escapeString );
+
+            // if these are NOT set, just use the defaults, which are '${*}' and '@'.
+            if ( delimiters != null && !delimiters.isEmpty() )
+            {
+                LinkedHashSet<String> delims = new LinkedHashSet<String>();
+                for ( String delim : delimiters )
+                {
+                    if ( delim == null )
+                    {
+                        // FIXME: ${filter:*} could also trigger this condition. Need a better long-term solution.
+                        delims.add( "${*}" );
+                    }
+                    else
+                    {
+                        delims.add( delim );
+                    }
+                }
+
+                filterRequest.setDelimiters( delims );
+            }
+            else
+            {
+                filterRequest.setDelimiters( filterRequest.getDelimiters() );
+            }
+
+            filterRequest.setInjectProjectBuildFilters( configSource.isIncludeProjectBuildFilters() );
+            return configSource.getMavenReaderFilter().filter( filterRequest );
+        }
+        catch ( MavenFilteringException e )
+        {
+            throw new AssemblyFormattingException( "Error filtering file '" + source + "': " + e.getMessage(), e );
+        }
+    }
+
+    public static InputStreamTransformer getFileSetTransformers( final AssemblerConfigurationSource configSource, final boolean isFiltered, String fileSetLineEnding )
+        throws AssemblyFormattingException
+    {
+        final String lineEndingHint = fileSetLineEnding;
+
+        String lineEnding = LineEndingsUtils.getLineEndingCharacters( lineEndingHint );
+
+        if ( ( lineEnding != null ) || isFiltered )
+        {
+            return new InputStreamTransformer()
+            {
+                public InputStream transform( PlexusIoResource plexusIoResource, InputStream inputStream )
+                    throws IOException
+                {
+                    if ( isFiltered )
+                    {
+                        final String encoding = configSource.getEncoding();
+
+                        Reader source = encoding != null ? new InputStreamReader( inputStream, encoding )
+                            : new InputStreamReader( inputStream ); // wtf platform encoding ?
+                        try
+                        {
+                            Reader filtered = createReaderFilter( source, plexusIoResource.getName(),
+                                                                                  configSource.getEncoding(),
+                                                                                  configSource.getEscapeString(),
+                                                                                  configSource.getDelimiters(),
+                                                                                  configSource );
+                            final ReaderInputStream readerInputStream = encoding != null ? new ReaderInputStream( filtered, encoding)
+                                : new ReaderInputStream( filtered );
+
+                            LineEndings lineEnding = LineEndingsUtils.getLineEnding( lineEndingHint );
+                            if ( !LineEndings.keep.equals( lineEnding ) )
+                            {
+                                return LineEndingsUtils.lineEndingConverter( readerInputStream, lineEnding );
+
+                            }
+                            return readerInputStream;
+
+                        }
+                        catch ( AssemblyFormattingException e )
+                        {
+                            throw new IOException( e );
+                        }
+
+                    }
+                    else
+                    {
+                        return inputStream;
+                    }
+                }
+            };
+        }
+        return null;
+    }
+}

Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java?rev=1633544&r1=1633543&r2=1633544&view=diff
==============================================================================
--- maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java (original)
+++ maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java Wed Oct 22 06:58:47 2014
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
+import java.util.Locale;
 
 import javax.annotation.Nonnull;
 
@@ -150,4 +151,13 @@ public final class AssemblyFileUtils
         return name.replace( File.separatorChar, '/' ); // How can this be anything but a no-op
     }
 
+    public static boolean isPropertyFile( String sourceName )
+    {
+        return sourceName.toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
+    }
+
+    public static boolean isPropertyFile( File file )
+    {
+        return isPropertyFile( file.getName());
+    }
 }