Author: aramirez
Date: Thu Mar 23 01:26:07 2006
New Revision: 388119

URL: http://svn.apache.org/viewcvs?rev=388119&view=rev
Log:
PR: MASSEMBLY-54

- added new tag ("<filtered>") in assembly descriptor for FileItem which will 
filter
the file and add the filtered file to the assembly.

Added:
    
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/interpolation/ReflectionProperties.java
    
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/
    
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java
Modified:
    
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractAssemblyMojo.java
    maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/component.mdo
    maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/descriptor.mdo

Modified: 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractAssemblyMojo.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractAssemblyMojo.java?rev=388119&r1=388118&r2=388119&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractAssemblyMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AbstractAssemblyMojo.java
 Thu Mar 23 01:26:07 2006
@@ -29,6 +29,8 @@
 import org.apache.maven.plugin.assembly.filter.AssemblyScopeArtifactFilter;
 import 
org.apache.maven.plugin.assembly.interpolation.AssemblyInterpolationException;
 import org.apache.maven.plugin.assembly.interpolation.AssemblyInterpolator;
+import org.apache.maven.plugin.assembly.interpolation.ReflectionProperties;
+import org.apache.maven.plugin.assembly.utils.PropertyUtils;
 import org.apache.maven.plugins.assembly.model.Assembly;
 import org.apache.maven.plugins.assembly.model.Component;
 import org.apache.maven.plugins.assembly.model.DependencySet;
@@ -53,6 +55,7 @@
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.InterpolationFilterReader;
 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
@@ -66,6 +69,9 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.FileInputStream;
+import java.io.PrintWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -73,6 +79,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -198,6 +205,13 @@
     private MavenArchiveConfiguration archive;
 
     /**
+     * @parameter expression="${project.build.filters}"
+     */
+    protected List filters;
+
+    private Properties filterProperties;
+
+    /**
      * Create the binary distribution.
      *
      * @throws org.apache.maven.plugin.MojoExecutionException
@@ -324,7 +338,7 @@
                 {
                     Manifest manifest = null;
                     File manifestFile = archive.getManifestFile();
-                    
+
                     if ( manifestFile != null )
                     {
                         try
@@ -739,7 +753,7 @@
                             try
                             {
                                 unpack( artifact.getFile(), tempLocation );
-                                
+
                                 /*
                                  * If the assembly is 'jar-with-dependencies', 
remove the security files in all dependencies
                                  * that will prevent the uberjar to execute.  
Please see MASSEMBLY-64 for details.
@@ -749,12 +763,12 @@
                                     String[] securityFiles = { "*.RSA", 
"*.DSA", "*.SF", "*.rsa", "*.dsa", "*.sf" };
                                     
org.apache.maven.shared.model.fileset.FileSet securityFileSet = new 
org.apache.maven.shared.model.fileset.FileSet();
                                     
securityFileSet.setDirectory(tempLocation.getAbsolutePath() + "/META-INF/" );
-                                    
+
                                     for ( int sfsi = 0; sfsi < 
securityFiles.length; sfsi++ )
                                     {
                                         securityFileSet.addInclude( 
securityFiles[sfsi] );
                                     }
-                                    
+
                                     FileSetManager fsm = new FileSetManager( 
getLog() );
                                     try
                                     {
@@ -939,16 +953,36 @@
      *
      */
     protected void processFileList( Archiver archiver, List fileList, boolean 
includeBaseDirecetory )
-        throws ArchiverException, IOException
+        throws ArchiverException, IOException, MojoExecutionException
     {
+        File source = null;
+        File filteredFile = null;
+        File sourceFileItem = null;
+
         for ( Iterator i = fileList.iterator(); i.hasNext(); )
         {
             FileItem fileItem = (FileItem) i.next();
 
-            File source = new File( fileItem.getSource() );
+            if( fileItem.isFiltered() )
+            {
+                sourceFileItem = new File( fileItem.getSource() );
+
+                try
+                {
+                    filteredFile = filterFile( sourceFileItem );
+
+                    fileItem.setSource( filteredFile.getAbsolutePath() );
+                }
+                catch( Exception e )
+                {
+                    throw new MojoExecutionException( "Failed to interpolate 
resource " + sourceFileItem.getName(), e );
+                }
+            }
 
             String outputDirectory = fileItem.getOutputDirectory();
 
+            source = new File( fileItem.getSource() );
+
             if ( outputDirectory == null )
             {
                 outputDirectory = "";
@@ -971,7 +1005,19 @@
 
             outputDirectory = getOutputDirectory( outputDirectory, 
includeBaseDirecetory );
 
+            // omit the last char if ends with / or \\
+            if( outputDirectory.endsWith( "/" ) || outputDirectory.endsWith( 
"\\" ) )
+            {
+                outputDirectory = outputDirectory.substring( 0, 
outputDirectory.length() - 1 );
+            }
+
             archiver.addFile( source, outputDirectory + "/" + destName, 
Integer.parseInt( fileItem.getFileMode() ) );
+
+            // return to original source
+            if( fileItem.isFiltered() )
+            {
+                fileItem.setSource( sourceFileItem.getAbsolutePath() );
+            }
         }
     }
 
@@ -1242,5 +1288,78 @@
 
         assembly.addFileSet( siteFileSet );
     }
+    private void initializeFiltering()
+        throws MojoExecutionException
+    {
+        getLog().info( "Initializing assembly filters..." );
+
+        // System properties
+        filterProperties = new Properties( System.getProperties() );
+
+        // Project properties
+        filterProperties.putAll( project.getProperties() );
+
+        if( filters != null && !filters.isEmpty() )
+        {
+            for ( Iterator i = filters.iterator(); i.hasNext(); )
+            {
+                String filtersfile = (String) i.next();
+
+                try
+                {
+                    Properties properties = PropertyUtils.loadPropertyFile( 
new File( filtersfile ), true, true );
+
+                    filterProperties.putAll( properties );
+                }
+                catch ( IOException e )
+                {
+                    throw new MojoExecutionException( "Error loading property 
file '" + filtersfile + "'", e );
+                }
+            }
+        }
+    }
+
+    private File filterFile( File file )
+        throws IOException, MojoExecutionException
+    {
+        initializeFiltering();
+
+        BufferedReader fileReader = new BufferedReader( new FileReader( file ) 
);
+        //Writer fileWriter = new FileWriter( file );
 
+        // support ${token}
+        Reader reader = new InterpolationFilterReader( fileReader, 
filterProperties, "${", "}" );
+
+        boolean isPropertiesFile = false;
+
+        if ( file.isFile() && file.getName().endsWith( ".properties" ) )
+        {
+            isPropertiesFile = true;
+        }
+        reader = new InterpolationFilterReader( reader, new 
ReflectionProperties( project, isPropertiesFile ), "${", "}" );
+
+        File tempFilterFile = new File( tempRoot + "/" + file.getName() );
+
+        tempFilterFile.getParentFile().mkdirs();
+        tempFilterFile.createNewFile();
+
+        Writer fileWriter = new FileWriter( tempFilterFile );
+
+        String line = null;
+
+        BufferedReader in = new BufferedReader( reader );
+
+        while( ( line = in.readLine() ) != null )
+        {
+            fileWriter.write( line );
+            fileWriter.write( System.getProperty( "line.separator" ) );
+        }
+
+        fileWriter.flush();
+        fileWriter.close();
+        in.close();
+        fileReader.close();
+
+        return tempFilterFile;
+    }
 }

Added: 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/interpolation/ReflectionProperties.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/interpolation/ReflectionProperties.java?rev=388119&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/interpolation/ReflectionProperties.java
 (added)
+++ 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/interpolation/ReflectionProperties.java
 Thu Mar 23 01:26:07 2006
@@ -0,0 +1,72 @@
+package org.apache.maven.plugin.assembly.interpolation;
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
+
+import java.util.Properties;
+
+
+/**
+ * @author Andreas Hoheneder (ahoh_at_inode.at)
+ * @version $Id: ReflectionProperties.java 367792 2006-01-10 20:56:22Z 
evenisse $
+ */
+public class ReflectionProperties
+    extends Properties
+{
+
+    private MavenProject project;
+
+    boolean escapedBackslashesInFilePath;
+
+    public ReflectionProperties( MavenProject aProject, boolean 
escapedBackslashesInFilePath ) 
+    {
+       super();
+
+       project = aProject;
+
+       this.escapedBackslashesInFilePath = escapedBackslashesInFilePath;
+    }
+    
+    public Object get( Object key )
+    {
+        Object value = null;
+        try 
+        {
+            value = ReflectionValueExtractor.evaluate( "" + key , project );
+
+            if ( escapedBackslashesInFilePath && value != null &&
+                "java.lang.String".equals( value.getClass().getName() ) )
+            {
+                String val = (String) value;
+
+                // Check if it's a windows path
+                if ( val.indexOf( ":\\" ) == 1 )
+                {
+                    value = StringUtils.replace( (String)value, "\\", "\\\\" );
+                    value = StringUtils.replace( (String)value, ":", "\\:" );
+                }
+            }
+        }
+        catch ( Exception e ) 
+        {
+            //TODO: remove the try-catch block when 
ReflectionValueExtractor.evaluate() throws no more exceptions
+        } 
+        return value;
+    }
+}

Added: 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java?rev=388119&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java
 (added)
+++ 
maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java
 Thu Mar 23 01:26:07 2006
@@ -0,0 +1,147 @@
+package org.apache.maven.plugin.assembly.utils;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+
+import org.codehaus.plexus.util.IOUtil;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Properties;
+
+
+/**
+ * @author <a href="mailto:[EMAIL PROTECTED]">Kenney Westerhof</a>
+ * @version $Id: PropertyUtils.java 191749 2005-06-22 01:21:07Z brett $
+ */
+public final class PropertyUtils
+{
+    private PropertyUtils()
+    {
+        // prevent instantiation
+    }
+
+    /**
+     * Reads a property file, resolving all internal variables.
+     *
+     * @param propfile The property file to load
+     * @param fail wheter to throw an exception when the file cannot be loaded 
or to return null
+     * @param useSystemProps wheter to incorporate System.getProperties 
settings into the returned Properties object.
+     * @return the loaded and fully resolved Properties object
+     */
+    public static Properties loadPropertyFile( File propfile, boolean fail, 
boolean useSystemProps )
+        throws IOException
+    {
+        Properties props = new Properties();
+
+        if ( useSystemProps )
+        {
+            props = new Properties( System.getProperties() );
+        }
+
+        if ( propfile.exists() )
+        {
+            FileInputStream inStream = new FileInputStream( propfile );
+            try
+            {
+                props.load( inStream );
+            }
+            finally
+            {
+                IOUtil.close( inStream );
+            }
+        }
+        else if ( fail )
+        {
+            throw new FileNotFoundException( propfile.toString() );
+        }
+
+        for ( Enumeration n = props.propertyNames(); n.hasMoreElements(); )
+        {
+            String k = (String) n.nextElement();
+            props.setProperty( k, getPropertyValue( k, props ) );
+        }
+
+        return props;
+    }
+
+
+    /**
+     * Retrieves a property value, replacing values like ${token}
+     * using the Properties to look them up.
+     *
+     * It will leave unresolved properties alone, trying for System
+     * properties, and implements reparsing (in the case that
+     * the value of a property contains a key), and will
+     * not loop endlessly on a pair like
+     * test = ${test}.
+     */
+    private static String getPropertyValue( String k, Properties p )
+    {
+        // This can also be done using InterpolationFilterReader,
+        // but it requires reparsing the file over and over until
+        // it doesn't change.
+
+        String v = p.getProperty( k );
+        String ret = "";
+        int idx, idx2;
+
+        while ( ( idx = v.indexOf( "${" ) ) >= 0 )
+        {
+            // append prefix to result
+            ret += v.substring( 0, idx );
+
+            // strip prefix from original
+            v = v.substring( idx + 2 );
+
+            // if no matching } then bail
+            if ( ( idx2 = v.indexOf( '}' ) ) < 0 )
+            {
+                break;
+            }
+
+            // strip out the key and resolve it
+            // resolve the key/value for the ${statement}
+            String nk = v.substring( 0, idx2 );
+            v = v.substring( idx2 + 1 );
+            String nv = p.getProperty( nk );
+
+            // try global environment..
+            if ( nv == null )
+            {
+                nv = System.getProperty( nk );
+            }
+
+            // if the key cannot be resolved,
+            // leave it alone ( and don't parse again )
+            // else prefix the original string with the
+            // resolved property ( so it can be parsed further )
+            // taking recursion into account.
+            if ( nv == null || nv.equals( k ) )
+            {
+                ret += "${" + nk + "}";
+            }
+            else
+            {
+                v = nv + v;
+            }
+        }
+        return ret + v;
+    }
+}

Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/component.mdo
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/component.mdo?rev=388119&r1=388118&r2=388119&view=diff
==============================================================================
--- maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/component.mdo 
(original)
+++ maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/component.mdo Thu 
Mar 23 01:26:07 2006
@@ -36,7 +36,7 @@
             <multiplicity>*</multiplicity>
           </association>
           <description>
-            Specify assembly parameters for groups of files. 
+            Specify assembly parameters for groups of files.
           </description>
         </field>
         <field>
@@ -63,7 +63,7 @@
         </field>
       </fields>
     </class>
-    
+
     <class>
       <name>SetBase</name>
       <version>1.0.0</version>
@@ -74,7 +74,7 @@
           <type>String</type>
           <description>
             Specifies the output directory relative to the root
-            of the root directory of the assembly.  For example,
+            of the root directory of the assembly. For example,
             "log" will put the specified files in the log directory.
           </description>
         </field>
@@ -148,7 +148,7 @@
           <version>1.0.0</version>
           <type>String</type>
           <description>
-            Absolute or relative from the module's directory.  For
+            Absolute or relative from the module's directory. For
             example, "src/main/bin" would select this subdirectory
             of the project in which this dependency is defined.
           </description>
@@ -198,7 +198,7 @@
           <required>false</required>
           <description>
             Specifies the output directory relative to the root
-            of the root directory of the assembly.  For example,
+            of the root directory of the assembly. For example,
             "log" will put the specified files in the log directory.
           </description>
         </field>
@@ -207,7 +207,7 @@
           <version>1.0.0</version>
           <type>String</type>
           <description>
-            Destination file name in outputDirectory. 
+            Destination file name in outputDirectory.
             Default is the same name as the source's file.
           </description>
         </field>
@@ -244,6 +244,14 @@
             ]]>
           </description>
         </field>
+        <field>
+          <name>filtered</name>
+          <version>1.0.0</version>
+          <type>boolean</type>
+          <description>
+            Flag used to determine if the file is filtered.
+          </description>
+        </field>
       </fields>
     </class>
 
@@ -259,7 +267,7 @@
           <type>String</type>
           <defaultValue>${artifactId}-${version}.${extension}</defaultValue>
           <description>
-            Specify the mapping pattern for all dependencies included 
+            Specify the mapping pattern for all dependencies included
             in this assembly.
             Default is ${artifactId}-${version}.${extension}.
           </description>
@@ -270,7 +278,7 @@
           <defaultValue>false</defaultValue>
           <description>
             If set to true, this property will unpack all dependencies
-            into the specified output directory.  When set to false
+            into the specified output directory. When set to false
             dependencies will be includes as archives (jars).
             Default value is false.
           </description>
@@ -288,7 +296,7 @@
         </field>
       </fields>
     </class>
-    
+
   </classes>
 </model>
 

Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/descriptor.mdo
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/descriptor.mdo?rev=388119&r1=388118&r2=388119&view=diff
==============================================================================
--- maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/descriptor.mdo 
(original)
+++ maven/plugins/trunk/maven-assembly-plugin/src/main/mdo/descriptor.mdo Thu 
Mar 23 01:26:07 2006
@@ -333,6 +333,14 @@
             ]]>
           </description>
         </field>
+        <field>
+          <name>filtered</name>
+          <version>1.0.0</version>
+          <type>boolean</type>
+          <description>
+            Flag used to determine if the file is filtered.
+          </description>
+        </field>
       </fields>
     </class>
 


Reply via email to