You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by de...@apache.org on 2012/09/21 10:23:25 UTC

svn commit: r1388368 - in /maven/plugins/trunk/maven-war-plugin/src: it/web-resources-filtering/ it/web-resources-filtering/web/ it/web-resources-filtering/web/src/main/webresources/WEB-INF/ it/web-resources-filtering/web/src/main/webresources/WEB-INF/...

Author: dennisl
Date: Fri Sep 21 08:23:24 2012
New Revision: 1388368

URL: http://svn.apache.org/viewvc?rev=1388368&view=rev
Log:
[MWAR-164] Support for specifying which encoding to use when filtering resources
Patch ideas from Florian Fray.

Added:
    maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/classes/
    maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/classes/my.properties
Modified:
    maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/verify.bsh
    maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/pom.xml
    maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/jetty-env.xml
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java

Modified: maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/verify.bsh
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/verify.bsh?rev=1388368&r1=1388367&r2=1388368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/verify.bsh (original)
+++ maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/verify.bsh Fri Sep 21 08:23:24 2012
@@ -26,25 +26,37 @@ boolean result = true;
 
 try
 {
+    // Load and check jetty-env.xml
+
     File target = new File( basedir, "web/target/example-web/WEB-INF" );
     if ( !target.exists() || !target.isDirectory() )
     {
-        System.err.println( "web/target/example-web/WEB-INF file is missing or a directory." );
+        System.err.println( "web/target/example-web/WEB-INF is missing or is not a directory." );
         return false;
     }
 
     File jettyEnv = new File( target, "jetty-env.xml" );
     if ( !jettyEnv.exists() || jettyEnv.isDirectory() )
     {
-        System.err.println( "jetty-env.xml is missing or a directory." );
+        System.err.println( "jetty-env.xml is missing or is a directory." );
         return false;
     }
 
 
-    String paramContent = FileUtils.fileRead( jettyEnv, "UTF-8" );
+    FileInputStream fis = new FileInputStream ( jettyEnv );
+    String paramContent = IOUtil.toString ( fis, "UTF-8" );
+
+    System.out.println( "content='" + paramContent + "'" );
 
 
-    int indexOf = paramContent.indexOf( "<Set name=\"URL\">jdbc:oracle:thin:@localhost:1521:orcl</Set>" );
+    int indexOf = paramContent.indexOf( "Characters that should be encoded in UTF-8: åäö" );
+    if ( indexOf < 0 )
+    {
+      System.err.println( "Non-ascii characters changed encoding during filtering" );
+      return false;
+    }
+
+    indexOf = paramContent.indexOf( "<Set name=\"URL\">jdbc:oracle:thin:@localhost:1521:orcl</Set>" );
     if ( indexOf < 0 )
     {
       System.err.println( "jdbc.url not filtered correctly" );
@@ -57,6 +69,35 @@ try
       System.err.println( "jdbc.password has been filtered" );
       return false;
     }
+
+    // Load and check my.properties
+
+    target = new File( basedir, "web/target/example-web/WEB-INF/classes" );
+    if ( !target.exists() || !target.isDirectory() )
+    {
+        System.err.println( "web/target/example-web/WEB-INF/classes is missing or is not a directory." );
+        return false;
+    }
+
+    File myProperties = new File( target, "my.properties" );
+    if ( !myProperties.exists() || myProperties.isDirectory() )
+    {
+        System.err.println( "my.properties is missing or is a directory." );
+        return false;
+    }
+
+    Properties properties = new Properties();
+    FileInputStream fis = new FileInputStream( myProperties );
+    properties.load( fis );
+    fis.close();
+
+    String property = properties.get( "my.property" );
+    System.out.println( "my.property='" + property + "'" );
+    if ( !"Characters that should be encoded in ISO-8859-1: åäö".equals( property ) )
+    {
+        System.err.println( "Non-ascii characters has wrong encoding after filtering" );
+        return false;
+    }
 }
 catch( IOException e )
 {

Modified: maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/pom.xml?rev=1388368&r1=1388367&r2=1388368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/pom.xml (original)
+++ maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/pom.xml Fri Sep 21 08:23:24 2012
@@ -59,6 +59,7 @@ under the License.
               <filtering>true</filtering>
             </resource>
           </webResources>
+          <resourceEncoding>ISO-8859-1</resourceEncoding>
         </configuration>
       </plugin>
     </plugins>

Added: maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/classes/my.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/classes/my.properties?rev=1388368&view=auto
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/classes/my.properties (added)
+++ maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/classes/my.properties Fri Sep 21 08:23:24 2012
@@ -0,0 +1 @@
+my.property=Characters that should be encoded in ISO-8859-1: åäö
\ No newline at end of file

Modified: maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/jetty-env.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/jetty-env.xml?rev=1388368&r1=1388367&r2=1388368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/jetty-env.xml (original)
+++ maven/plugins/trunk/maven-war-plugin/src/it/web-resources-filtering/web/src/main/webresources/WEB-INF/jetty-env.xml Fri Sep 21 08:23:24 2012
@@ -1,7 +1,14 @@
-<?xml version="1.0"?>
+<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
         "http://jetty.mortbay.org/configure.dtd">
 <Configure class="org.mortbay.jetty.webapp.WebAppContext">
+  <!--
+    This file is encoded in UTF-8 and should be so after filtering, since it
+    specifies an encoding in the xml header. So the following characters should
+    remain unaltered after filtering even though the encoding for filtering is
+    set to ISO-8859-1:
+    Characters that should be encoded in UTF-8: åäö
+  -->
   <New id="MyDS" class="org.mortbay.jetty.plus.naming.Resource">
     <Arg>jdbc/EventdialogDS</Arg>
     <Arg>

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java?rev=1388368&r1=1388367&r2=1388368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java Fri Sep 21 08:23:24 2012
@@ -99,6 +99,14 @@ public abstract class AbstractWarMojo
     private boolean archiveClasses;
 
     /**
+     * The encoding to use when copying filtered web resources.
+     *
+     * @since 2.3
+     */
+    @Parameter( property = "resourceEncoding", defaultValue = "${project.build.sourceEncoding}" )
+    private String resourceEncoding;
+
+    /**
      * The JAR archiver needed for archiving the classes directory into a JAR file under WEB-INF/lib.
      */
     @Component( role = Archiver.class, hint = "jar" )
@@ -455,7 +463,7 @@ public abstract class AbstractWarMojo
                                                                             defaultFilterWrappers,
                                                                             getNonFilteredFileExtensions(),
                                                                             filteringDeploymentDescriptors,
-                                                                            this.artifactFactory );
+                                                                            this.artifactFactory, resourceEncoding);
         for ( WarPackagingTask warPackagingTask : packagingTasks )
         {
             warPackagingTask.performPackaging( context );
@@ -531,6 +539,8 @@ public abstract class AbstractWarMojo
 
         private final ArtifactFactory artifactFactory;
 
+        private final String resourceEncoding;
+
         private final WebappStructure webappStructure;
 
         private final File webappDirectory;
@@ -545,8 +555,8 @@ public abstract class AbstractWarMojo
 
         public DefaultWarPackagingContext( File webappDirectory, final WebappStructure webappStructure,
                                            final OverlayManager overlayManager, List<FileUtils.FilterWrapper> filterWrappers,
-                                           List<String>  nonFilteredFileExtensions, boolean filteringDeploymentDescriptors,
-                                           ArtifactFactory artifactFactory )
+                                           List<String> nonFilteredFileExtensions, boolean filteringDeploymentDescriptors,
+                                           ArtifactFactory artifactFactory, String resourceEncoding )
         {
             this.webappDirectory = webappDirectory;
             this.webappStructure = webappStructure;
@@ -556,6 +566,7 @@ public abstract class AbstractWarMojo
             this.filteringDeploymentDescriptors = filteringDeploymentDescriptors;
             this.nonFilteredFileExtensions = nonFilteredFileExtensions == null ? Collections.<String>emptyList()
                                                                               : nonFilteredFileExtensions;
+            this.resourceEncoding = resourceEncoding;
             // This is kinda stupid but if we loop over the current overlays and we request the path structure
             // it will register it. This will avoid wrong warning messages in a later phase
             for ( String overlayId : overlayManager.getOverlayIds() )
@@ -673,6 +684,11 @@ public abstract class AbstractWarMojo
         {
             return session;
         }
+
+        public String getResourceEncoding()
+        {
+            return resourceEncoding;
+        }
     }
 
     public MavenProject getProject()

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java?rev=1388368&r1=1388367&r2=1388368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java Fri Sep 21 08:23:24 2012
@@ -36,6 +36,8 @@ import org.codehaus.plexus.archiver.mana
 import org.codehaus.plexus.interpolation.InterpolationException;
 import org.codehaus.plexus.util.DirectoryScanner;
 import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.XmlStreamReader;
 
 /**
  * @author Stephane Nicoll
@@ -206,19 +208,29 @@ public abstract class AbstractWarPackagi
         if ( context.getWebappStructure().registerFile( sourceId, targetFilename ) )
         {
             final File targetFile = new File( context.getWebappDirectory(), targetFilename );
+            final String encoding;
             try
             {
+                if ( isXmlFile( file ) )
+                {
+                    // For xml-files we extract the encoding from the files
+                    encoding = getEncoding( file );
+                }
+                else
+                {
+                    // For all others we use the configured encoding
+                    encoding = context.getResourceEncoding();
+                }
                 // fix for MWAR-36, ensures that the parent dir are created first
                 targetFile.getParentFile().mkdirs();
-                // TODO: add encoding support (null mean platform encoding)
-                context.getMavenFileFilter().copyFile( file, targetFile, true, context.getFilterWrappers(), null );
+                context.getMavenFileFilter().copyFile( file, targetFile, true, context.getFilterWrappers(), encoding );
             }
             catch ( MavenFilteringException e )
             {
                 throw new MojoExecutionException( e.getMessage(), e );
             }
             // Add the file to the protected list
-            context.getLog().debug( " + " + targetFilename + " has been copied (filtered)." );
+            context.getLog().debug( " + " + targetFilename + " has been copied (filtered encoding='" + encoding + "')." );
             return true;
         }
         else
@@ -229,7 +241,6 @@ public abstract class AbstractWarPackagi
         }
     }
 
-
     /**
      * Unpacks the specified file to the specified directory.
      *
@@ -299,6 +310,27 @@ public abstract class AbstractWarPackagi
     }
 
     /**
+     * Get the encoding from an XML-file.
+     *
+     * @param webXml the XML-file
+     * @return The encoding of the XML-file, or UTF-8 if it's not specified in the file
+     * @throws java.io.IOException if an error occurred while reading the file
+     */
+    protected String getEncoding( File webXml )
+        throws IOException
+    {
+        XmlStreamReader xmlReader = new XmlStreamReader( webXml );
+        try
+        {
+            return xmlReader.getEncoding();
+        }
+        finally
+        {
+            IOUtil.close( xmlReader );
+        }
+    }
+
+    /**
      * Returns the file to copy. If the includes are <tt>null</tt> or empty, the
      * default includes are used.
      *
@@ -363,4 +395,18 @@ public abstract class AbstractWarPackagi
         }
 
     }
+
+    /**
+     * Returns <code>true</code> if the <code>File</code>-object is a file (not
+     * a directory) that is not <code>null</code> and has a file name that ends
+     * in ".xml".
+     *
+     * @param file The file to check
+     * @return <code>true</code> if the file is an xml-file, otherwise <code>false</code>
+     * @since 2.3
+     */
+    private boolean isXmlFile( File file )
+    {
+        return file != null && file.isFile() && file.getName().endsWith( ".xml" );
+    }
 }

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java?rev=1388368&r1=1388367&r2=1388368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java Fri Sep 21 08:23:24 2012
@@ -195,4 +195,12 @@ public interface WarPackagingContext
      * @since 2.2
      */
     MavenSession getSession();
+
+    /**
+     * Returns the encoding to use for resources.
+     *
+     * @return the resource encoding
+     * @since 2.3
+     */
+    String getResourceEncoding();
 }

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java?rev=1388368&r1=1388367&r2=1388368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java Fri Sep 21 08:23:24 2012
@@ -26,9 +26,7 @@ import org.apache.maven.plugin.war.Overl
 import org.apache.maven.plugin.war.util.PathSet;
 import org.apache.maven.shared.filtering.MavenFilteringException;
 import org.codehaus.plexus.util.DirectoryScanner;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.xml.XmlStreamReader;
 
 import java.io.File;
 import java.io.IOException;
@@ -282,27 +280,6 @@ public class WarProjectPackagingTask
     }
 
     /**
-     * Get the encoding from an XML-file.
-     *
-     * @param webXml the XML-file
-     * @return The encoding of the XML-file, or UTF-8 if it's not specified in the file
-     * @throws IOException if an error occurred while reading the file
-     */
-    private String getEncoding( File webXml )
-        throws IOException
-    {
-        XmlStreamReader xmlReader = new XmlStreamReader( webXml );
-        try
-        {
-            return xmlReader.getEncoding();
-        }
-        finally
-        {
-            IOUtil.close( xmlReader );
-        }
-    }
-
-    /**
      * Copies webapp webResources from the specified directory.
      *
      * @param context  the WAR packaging context to use