On Wed, 24 Aug 2005, Brett Porter wrote:

> I think this is reasonable, but I'd like to allow multiple sources
> (properties, and multiple files). The existence of the filtering element
> can be used to trigger it.

I don't think it's a good idea to specify the filter files at the
resource level (assuming the <filtering> below is a child element of
<resource>). Specifying tokens however is a good idea.

I think you should be able to use different filter properties files using
profiles. The resources plugin can check for the presence of all tokens
and bail if some are missing. So:

<resource>
  <filtering>
    <filterTokens>
      <token>tokenname</token>
      <token>anothertokenname</token>
    </filtertokens>
  </filtering>
   ....
</resource>

Specifying the filter files themselves above is only nice if you use
the same token values in different resource files. However, that's not
the most common usecase for using filters: it's for building for different
environments. According to the m2 philosphy you should have different
projects, but it's not possible to filter resources from another project
(unless you use relative paths to resource dirs). So having profiles
with different filter files should solve this (that's what profiles
are for anyway).

That leaves one thing open: where to specify the filter filenames?
I suggest we supply it in the maven-resources-plugin configuration.

WDYT?

-- Kenney

 >
> <filtering>
>   <filterTokens>
>     <token>value</token>
>   </filterTokens>
>   <filterFiles>
>     <filterFile>foo.properties</filterFile>
>     <filterFile>bar.properties</filterFile>
>   </filterFiles>
> </filtering>
>
> Dowside is that for a single file it is quite verbose.
>
> Any other suggestions? Any problems with having filtering here in the
> first place?
>
> - Brett
>
> Carsten Ziegeler wrote:
>
> >In my opinion the current configuration of resources wrt filtering is a
> >little bit too complicated and not as user friendly as it could be :)
> >I think the most natural way is to configure the filters directly on the
> >resources. This allows to easily configure different resource sets with
> >different filtering behaviour:
> >
> ><!-- XML Files filtered -->
> ><resource>
> >  <directory>src/resources</directory>
> >  <includes>
> >    <include>**/*.xml</include>
> >    <exclude>**/description.xml</exclude>
> >  </includes>
> >  <filtering>true</filtering>
> > <filteringPropertiesFile>myfilters.properties</filteringPropertiesFile>
> ></resource>
> ><!-- XML description files are filtered differently -->
> ><resource>
> >  <directory>src/resources</directory>
> >  <includes>
> >    <include>**/description.xml</include>
> >  </includes>
> >  <filtering>true</filtering>
> > <filteringPropertiesFile>descfilter.properties</filteringPropertiesFile>
> ></resource>
> ><!-- Binary stuff, not filtered: -->
> ><resource>
> >  <directory>src/resources</directory>
> >  <includes>
> >    <excludes>**/*.xml</excludes>
> >  </includes>
> ></resource>
> >
> >WDYT?
> >
> >Attached is a patch that should provide this feature - but I don't have
> >tested it yet and I'm not sure if the properties file is resolved in the
> >correct location. But it would be a start :) (At least it compiles and
> >the resource plugin still works the way it is today.)
> >
> >
> >
> >------------------------------------------------------------------------
> >
> >Index: 
> >D:/dev/workspace/maven-components/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
> >===================================================================
> >--- 
> >D:/dev/workspace/maven-components/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
> >        (revision 239437)
> >+++ 
> >D:/dev/workspace/maven-components/maven-plugins/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
> >        (working copy)
> >@@ -97,7 +97,7 @@
> >     public void execute()
> >         throws MojoExecutionException
> >     {
> >-        initializeFiltering();
> >+        filterProperties = initializeFiltering(filtering, 
> >filterPropertiesFile);
> >         copyResources( resources, outputDirectory );
> >     }
> >
> >@@ -110,7 +110,8 @@
> >             {
> >                 Map.Entry entry = (Map.Entry) i.next();
> >                 File source = (File) entry.getKey();
> >-                String destination = (String) entry.getValue();
> >+                CopyDescription description = 
> >(CopyDescription)entry.getValue();
> >+                String destination = description.destination;
> >
> >                 File destinationFile = new File( outputDirectory, 
> > destination );
> >
> >@@ -119,7 +120,7 @@
> >                     destinationFile.getParentFile().mkdirs();
> >                 }
> >
> >-                copyFile( source, destinationFile );
> >+                copyFile( source, destinationFile, description );
> >             }
> >         }
> >         catch ( Exception e )
> >@@ -165,6 +166,8 @@
> >
> >             scanner.scan();
> >
> >+            ResourceFilter filter = new ResourceFilter();
> >+            filter.resource = resource;
> >             List includedFiles = Arrays.asList( scanner.getIncludedFiles() 
> > );
> >             for ( Iterator j = includedFiles.iterator(); j.hasNext(); )
> >             {
> >@@ -177,48 +180,68 @@
> >                     entryName = targetPath + "/" + name;
> >                 }
> >
> >-                resourceEntries.put( new File( resource.getDirectory(), 
> >name ), entryName );
> >+                resourceEntries.put( new File( resource.getDirectory(), 
> >name ), new CopyDescription(entryName, filter) );
> >             }
> >         }
> >
> >         return resourceEntries;
> >     }
> >
> >-    private void initializeFiltering()
> >+    private Properties initializeFiltering(boolean doFiltering, File 
> >propertiesFile)
> >         throws MojoExecutionException
> >     {
> >-        if ( filtering )
> >+        if ( doFiltering )
> >         {
> >             try
> >             {
> >-                filterProperties = PropertyUtils.loadPropertyFile( 
> >filterPropertiesFile, true, true );
> >+                return PropertyUtils.loadPropertyFile( propertiesFile, 
> >true, true );
> >             }
> >             catch ( IOException e )
> >             {
> >                 throw new MojoExecutionException( "Error loading property 
> > file '" + filterPropertiesFile + "'", e );
> >             }
> >         }
> >+        return null;
> >     }
> >
> >-    private void copyFile( File from, File to )
> >-        throws IOException
> >+    private void copyFile( File from, File to, CopyDescription desc )
> >+        throws IOException, MojoExecutionException
> >     {
> >-        if ( !filtering )
> >+        boolean doFiltering = filtering || 
> >desc.filter.resource.isFiltering();
> >+        if ( !doFiltering )
> >         {
> >             FileUtils.copyFile( from, to );
> >         }
> >         else
> >         {
> >+            // first initialize properties for this resource
> >+            if ( desc.filter.properties == null )
> >+            {
> >+                if ( filterPropertiesFile != null && filterProperties == 
> >null )
> >+                {
> >+                    filterProperties = initializeFiltering( true, 
> >filterPropertiesFile );
> >+                }
> >+                if ( desc.filter.resource.getFilterPropertiesFile() != null 
> >)
> >+                {
> >+                    Properties resourceProps = initializeFiltering( true, 
> >new File(desc.filter.resource.getFilterPropertiesFile()) );
> >+                    resourceProps.putAll(filterProperties);
> >+                    desc.filter.properties = resourceProps;
> >+                }
> >+                else
> >+                {
> >+                    desc.filter.properties = filterProperties;
> >+                }
> >+            }
> >             // 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, "${", "}" );
> >+                Reader reader = new InterpolationFilterReader( fileReader, 
> >desc.filter.properties, "${", "}" );
> >
> >                 // support @token@
> >-                reader = new InterpolationFilterReader( reader, 
> >filterProperties, "@", "@" );
> >+                reader = new InterpolationFilterReader( reader, 
> >desc.filter.properties, "@", "@" );
> >
> >                 reader = new InterpolationFilterReader( reader, new 
> > ReflectionProperties( project ), "${", "}" );
> >
> >@@ -233,4 +256,17 @@
> >             }
> >         }
> >     }
> >+
> >+    static protected final class CopyDescription {
> >+        public CopyDescription(String dest, ResourceFilter f) {
> >+            this.destination = dest;
> >+            this.filter = f;
> >+        }
> >+        public String destination;
> >+        public ResourceFilter filter;
> >+    }
> >+    static protected final class ResourceFilter {
> >+        public Resource resource;
> >+        public Properties properties;
> >+    }
> > }
> >Index: D:/dev/workspace/maven-components/maven-model/maven.mdo
> >===================================================================
> >--- D:/dev/workspace/maven-components/maven-model/maven.mdo  (revision 
> >239437)
> >+++ D:/dev/workspace/maven-components/maven-model/maven.mdo  (working copy)
> >@@ -1932,11 +1932,19 @@
> >         </field>
> >         <field>
> >           <name>filtering</name>
> >-          <version>3.0.0</version>
> >+          <version>3.0.0+</version>
> >           <description><![CDATA[Boolean. Describe if resources are filtered 
> > or not.]]></description>
> >           <type>boolean</type>
> >           <defaultValue>false</defaultValue>
> >         </field>
> >+        <field>
> >+          <name>filterPropertiesFile</name>
> >+          <version>4.0.0</version>
> >+          <description><![CDATA[
> >+            The filename for a properties file containing filters to be 
> >applied if filtering is turned on.
> >+          ]]></description>
> >+          <type>String</type>
> >+        </field>
> >       </fields>
> >     </class>
> >     <class>
> >
> >
> >
> >------------------------------------------------------------------------
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>

--
Kenney Westerhof
http://www.neonics.com
GPG public key: http://www.gods.nl/~forge/kenneyw.key

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to