diff --git a/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java b/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java new file mode 100644 index 0000000000..935429ccd8 --- /dev/null +++ b/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java @@ -0,0 +1,147 @@ +package org.apache.maven.plugin.resources; + +/* + * 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 Kenney Westerhof + * @version $Id$ + */ +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; + } +} diff --git a/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java b/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java index aa38c15879..4261a4f21a 100644 --- a/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java +++ b/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java @@ -21,12 +21,21 @@ import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.util.DirectoryScanner; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.InterpolationFilterReader; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.TreeMap; /** @@ -56,6 +65,22 @@ public class ResourcesMojo */ private List resources; + /** + * Wheter to apply filters during transfer. + * + * @parameter + */ + private boolean filtering = false; + + /** + * The name of the filter property file to use. + * + * @parameter expression="${basedir}/filter.properties" + */ + private File filterPropertiesFile; + + private Properties filterProperties; + private static final String[] EMPTY_STRING_ARRAY = {}; private static final String[] DEFAULT_INCLUDES = {"**/**"}; @@ -63,6 +88,7 @@ public class ResourcesMojo public void execute() throws MojoExecutionException { + initializeFiltering(); copyResources( resources, outputDirectory ); } @@ -84,7 +110,7 @@ public class ResourcesMojo destinationFile.getParentFile().mkdirs(); } - FileUtils.copyFile( source, destinationFile ); + copyFile( source, destinationFile ); } } catch ( Exception e ) @@ -95,7 +121,6 @@ public class ResourcesMojo } private Map getJarResources( List resources ) - throws Exception { Map resourceEntries = new TreeMap(); @@ -150,4 +175,51 @@ public class ResourcesMojo return resourceEntries; } + private void initializeFiltering() + throws MojoExecutionException + { + if ( filtering ) + { + try + { + filterProperties = PropertyUtils.loadPropertyFile( filterPropertiesFile, true, true ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Error loading property file '" + filterPropertiesFile + "'", e ); + } + } + } + + private void copyFile( File from, File to ) + throws IOException + { + if ( !filtering ) + { + FileUtils.copyFile( from, to ); + } + else + { + // buffer so it isn't reading a byte at a time! + Reader fileReader = new BufferedReader( new FileReader( from ) ); + Writer fileWriter = null; + try + { + // support ${token} + Reader reader = new InterpolationFilterReader( fileReader, filterProperties, "${", "}" ); + + // support @token@ + reader = new InterpolationFilterReader( reader, filterProperties, "@", "@" ); + + fileWriter = new FileWriter( to ); + + IOUtil.copy( reader, fileWriter ); + } + finally + { + IOUtil.close( fileReader ); + IOUtil.close( fileWriter ); + } + } + } }