You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2020/02/13 22:11:14 UTC
[maven-filtering] 04/44: add a plexus component which aplly
filtering on List of org.apache.maven.model.Resource
This is an automated email from the ASF dual-hosted git repository.
hboutemy pushed a commit to annotated tag maven-filtering-1.0-alpha-1
in repository https://gitbox.apache.org/repos/asf/maven-filtering.git
commit 790d3a224209599cc76694127a391039cac0c6e3
Author: Oliver Lamy <ol...@apache.org>
AuthorDate: Mon Jan 28 23:21:03 2008 +0000
add a plexus component which aplly filtering on List of org.apache.maven.model.Resource
git-svn-id: https://svn.apache.org/repos/asf/maven/sandbox/trunk/shared/maven-filtering@616103 13f79535-47bb-0310-9956-ffa450edef68
---
.../shared/filtering/DefaultMavenFileFilter.java | 38 +++---
.../filtering/DefaultMavenResourcesFiltering.java | 133 +++++++++++++++++++++
.../maven/shared/filtering/MavenFileFilter.java | 11 +-
.../shared/filtering/MavenResourcesFiltering.java | 49 ++++++++
.../maven/shared/filtering/PropertyUtils.java | 4 +-
.../DefaultMavenResourcesFilteringTest.java | 116 ++++++++++++++++++
.../maven/shared/filtering/StubMavenProject.java | 46 +++++++
.../empty-maven-resources-filtering.txt | 18 +++
.../maven-resources-filtering.txt | 22 ++++
9 files changed, 413 insertions(+), 24 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java
index b6551dc..fe58371 100755
--- a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java
+++ b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java
@@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
@@ -35,7 +36,8 @@ import org.codehaus.plexus.util.InterpolationFilterReader;
* @since 22 janv. 08
* @version $Id$
*
- * @plexus.component role="org.apache.maven.shared.filtering.MavenFileFilter" role-hint="default"
+ * @plexus.component role="org.apache.maven.shared.filtering.MavenFileFilter"
+ * role-hint="default"
*/
public class DefaultMavenFileFilter
implements MavenFileFilter
@@ -82,17 +84,18 @@ public class DefaultMavenFileFilter
/**
* @see org.apache.maven.shared.filtering.MavenFileFilter#getDefaultFilterWrappers(org.apache.maven.project.MavenProject, java.util.List)
*/
- public List getDefaultFilterWrappers( final MavenProject mavenProject, List filters, final boolean escapedBackslashesInFilePath )
+ public List getDefaultFilterWrappers( final MavenProject mavenProject, List filters,
+ final boolean escapedBackslashesInFilePath )
throws MavenFilteringException
{
-
+
final Properties filterProperties = new Properties();
// System properties
filterProperties.putAll( System.getProperties() );
// Project properties
- filterProperties.putAll( mavenProject.getProperties() );
+ filterProperties.putAll( mavenProject.getProperties() == null ? Collections.EMPTY_MAP : mavenProject.getProperties() );
// Take a copy of filterProperties to ensure that evaluated filterTokens are not propagated
// to subsequent filter files. NB this replicates current behaviour and seems to make sense.
@@ -116,21 +119,25 @@ public class DefaultMavenFileFilter
}
}
}
-
- List defaultFilterWrappers = new ArrayList(3);
-
+
+ List defaultFilterWrappers = new ArrayList( 3 );
+
// support ${token}
- FileUtils.FilterWrapper one = new FileUtils.FilterWrapper() {
- public Reader getReader(Reader reader) {
- return new InterpolationFilterReader(reader, filterProperties, "${", "}");
+ FileUtils.FilterWrapper one = new FileUtils.FilterWrapper()
+ {
+ public Reader getReader( Reader reader )
+ {
+ return new InterpolationFilterReader( reader, filterProperties, "${", "}" );
}
};
defaultFilterWrappers.add( one );
-
+
// support @token@
- FileUtils.FilterWrapper second = new FileUtils.FilterWrapper() {
- public Reader getReader(Reader reader) {
- return new InterpolationFilterReader(reader, filterProperties, "@", "@");
+ FileUtils.FilterWrapper second = new FileUtils.FilterWrapper()
+ {
+ public Reader getReader( Reader reader )
+ {
+ return new InterpolationFilterReader( reader, filterProperties, "@", "@" );
}
};
defaultFilterWrappers.add( second );
@@ -145,8 +152,7 @@ public class DefaultMavenFileFilter
}
};
defaultFilterWrappers.add( third );
-
-
+
return defaultFilterWrappers;
}
diff --git a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
new file mode 100755
index 0000000..be4e863
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
@@ -0,0 +1,133 @@
+/*
+ * 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.
+ */
+package org.apache.maven.shared.filtering;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.maven.model.Resource;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.DirectoryScanner;
+
+/**
+ * @author <a href="mailto:olamy@apache.org">olamy</a>
+ * @since 28 janv. 08
+ * @version $Id$
+ *
+ * @plexus.component role="org.apache.maven.shared.filtering.MavenResourcesFiltering"
+ * role-hint="default"
+ */
+public class DefaultMavenResourcesFiltering
+ implements MavenResourcesFiltering
+{
+
+ private static final String[] EMPTY_STRING_ARRAY = {};
+
+ private static final String[] DEFAULT_INCLUDES = {"**/**"};
+
+ /**
+ * @plexus.requirement
+ * role-hint="default"
+ */
+ private MavenFileFilter mavenFileFilter;
+
+ public void filterResources( List resources, File outputDirectory, MavenProject mavenProject, String encoding,
+ List fileFilters )
+ throws MavenFilteringException
+ {
+ for ( Iterator i = resources.iterator(); i.hasNext(); )
+ {
+ Resource resource = (Resource) i.next();
+
+ String targetPath = resource.getTargetPath();
+
+ File resourceDirectory = new File( resource.getDirectory() );
+ if ( !resourceDirectory.isAbsolute() )
+ {
+ resourceDirectory = new File( mavenProject.getBasedir(), resourceDirectory.getPath() );
+ }
+
+ if ( !resourceDirectory.exists() )
+ {
+ // TODO how to log here ?
+ continue;
+ }
+
+ // this part is required in case the user specified "../something" as destination
+ // see MNG-1345
+ if ( !outputDirectory.exists() )
+ {
+ if ( !outputDirectory.mkdirs() )
+ {
+ throw new MavenFilteringException( "Cannot create resource output directory: " + outputDirectory );
+ }
+ }
+
+ DirectoryScanner scanner = new DirectoryScanner();
+
+ scanner.setBasedir( resourceDirectory );
+ if ( resource.getIncludes() != null && !resource.getIncludes().isEmpty() )
+ {
+ scanner.setIncludes( (String[]) resource.getIncludes().toArray( EMPTY_STRING_ARRAY ) );
+ }
+ else
+ {
+ scanner.setIncludes( DEFAULT_INCLUDES );
+ }
+
+ if ( resource.getExcludes() != null && !resource.getExcludes().isEmpty() )
+ {
+ scanner.setExcludes( (String[]) resource.getExcludes().toArray( EMPTY_STRING_ARRAY ) );
+ }
+
+ scanner.addDefaultExcludes();
+ scanner.scan();
+
+ List includedFiles = Arrays.asList( scanner.getIncludedFiles() );
+
+ List filterWrappers = mavenFileFilter.getDefaultFilterWrappers( mavenProject, fileFilters, true );
+
+ for ( Iterator j = includedFiles.iterator(); j.hasNext(); )
+ {
+ String name = (String) j.next();
+
+ String destination = name;
+
+ if ( targetPath != null )
+ {
+ destination = targetPath + "/" + name;
+ }
+
+ File source = new File( resourceDirectory, name );
+
+ File destinationFile = new File( outputDirectory, destination );
+
+ if ( !destinationFile.getParentFile().exists() )
+ {
+ destinationFile.getParentFile().mkdirs();
+ }
+ mavenFileFilter.copyFile( source, destinationFile, resource.isFiltering(), filterWrappers, encoding );
+ }
+ }
+
+ }
+
+}
diff --git a/src/main/java/org/apache/maven/shared/filtering/MavenFileFilter.java b/src/main/java/org/apache/maven/shared/filtering/MavenFileFilter.java
index e8f32ff..a0c29ac 100755
--- a/src/main/java/org/apache/maven/shared/filtering/MavenFileFilter.java
+++ b/src/main/java/org/apache/maven/shared/filtering/MavenFileFilter.java
@@ -43,18 +43,18 @@ public interface MavenFileFilter
* @param filters {@link List} of properties file
* @throws IOException
*/
- public void copyFile( File from, final File to, boolean filtering, MavenProject mavenProject,
- List/* File */filters, boolean escapedBackslashesInFilePath, String encoding )
+ public void copyFile( File from, final File to, boolean filtering, MavenProject mavenProject, List filters,
+ boolean escapedBackslashesInFilePath, String encoding )
throws MavenFilteringException;
/**
* @param from
* @param to
* @param filtering
- * @param filterWrappers
+ * @param filterWrappers {@link List} of FileUtils.FilterWrapper
* @throws MavenFilteringException
*/
- public void copyFile( File from, final File to, boolean filtering, List /*FileUtils.FilterWrapper*/filterWrappers, String encoding )
+ public void copyFile( File from, final File to, boolean filtering, List filterWrappers, String encoding )
throws MavenFilteringException;
/**
@@ -73,7 +73,6 @@ public interface MavenFileFilter
* @return {@link List} of FileUtils.FilterWrapper
*
*/
- public List/*FileUtils.FilterWrapper*/getDefaultFilterWrappers( MavenProject mavenProject, List/* File */filters,
- boolean escapedBackslashesInFilePath )
+ public List getDefaultFilterWrappers( MavenProject mavenProject, List filters, boolean escapedBackslashesInFilePath )
throws MavenFilteringException;
}
diff --git a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesFiltering.java b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesFiltering.java
new file mode 100755
index 0000000..bae9373
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesFiltering.java
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+package org.apache.maven.shared.filtering;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.maven.model.Resource;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * @author <a href="mailto:olamy@apache.org">olamy</a>
+ * @since 28 janv. 08
+ * @version $Id$
+ */
+public interface MavenResourcesFiltering
+{
+
+ /**
+ * @param resources {@link List} of {@link Resource}
+ * @param outputDirectory parent destination directory
+ * @param mavenProject
+ * @param encoding
+ * @param fileFilters {@link List} of Properties file
+ * @throws MavenFilteringException
+ * @throws IOException
+ */
+ public void filterResources( List resources, File outputDirectory, MavenProject mavenProject, String encoding,
+ List fileFilters )
+ throws MavenFilteringException;
+
+}
diff --git a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java
index 424d689..3f1e1c9 100755
--- a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java
+++ b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java
@@ -72,7 +72,7 @@ public final class PropertyUtils
}
final Properties combinedProps = new Properties();
- combinedProps.putAll( baseProps );
+ combinedProps.putAll( baseProps == null ? new Properties() : baseProps );
combinedProps.putAll( fileProps );
// The algorithm iterates only over the fileProps which is all that is required to resolve
@@ -185,7 +185,7 @@ public final class PropertyUtils
// 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 ) )
+ if ( nv == null || nv.equals( k ) || k.equals( nk ) )
{
ret += "${" + nk + "}";
}
diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
new file mode 100755
index 0000000..aacc834
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ */
+package org.apache.maven.shared.filtering;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.maven.model.Resource;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.util.FileUtils;
+
+/**
+ * @author <a href="mailto:olamy@apache.org">olamy</a>
+ * @since 28 janv. 08
+ * @version $Id$
+ */
+public class DefaultMavenResourcesFilteringTest
+ extends PlexusTestCase
+{
+
+ File outputDirectory = new File(getBasedir(), "target/DefaultMavenResourcesFilteringTest");
+
+
+
+ protected void setUp()
+ throws Exception
+ {
+ super.setUp();
+ if ( outputDirectory.exists() )
+ {
+ FileUtils.forceDelete( outputDirectory );
+ }
+ outputDirectory.mkdirs();
+ }
+
+ public void testSimpleFiltering()
+ throws Exception
+ {
+ StubMavenProject mavenProject = new StubMavenProject();
+ mavenProject.setVersion( "1.0" );
+ mavenProject.setGroupId( "org.apache" );
+
+ Properties projectProperties = new Properties();
+ projectProperties.put( "foo", "bar" );
+ mavenProject.setProperties( projectProperties );
+ MavenResourcesFiltering mavenResourcesFiltering = (MavenResourcesFiltering) lookup( MavenResourcesFiltering.class
+ .getName() );
+
+ Resource resource = new Resource();
+ List resources = new ArrayList();
+ resources.add( resource );
+ resource.setDirectory( getBasedir() + "/src/test/units-files/maven-resources-filtering" );
+ resource.setFiltering( true );
+ mavenResourcesFiltering.filterResources( resources, outputDirectory, mavenProject, null, null );
+
+ assertEquals( 2, outputDirectory.listFiles().length );
+ Properties result = PropertyUtils.loadPropertyFile( new File(outputDirectory, "empty-maven-resources-filtering.txt"), null );
+ assertTrue (result.isEmpty());
+
+ result = PropertyUtils.loadPropertyFile( new File(outputDirectory, "maven-resources-filtering.txt"), null );
+ assertFalse( result.isEmpty() );
+
+ assertEquals("1.0", result.get( "version" ));
+ assertEquals("org.apache", result.get( "groupId" ));
+ assertEquals("bar", result.get( "foo" ));
+ }
+
+ public void testNoFiltering()
+ throws Exception
+ {
+ StubMavenProject mavenProject = new StubMavenProject();
+ mavenProject.setVersion( "1.0" );
+ mavenProject.setGroupId( "org.apache" );
+
+ MavenResourcesFiltering mavenResourcesFiltering = (MavenResourcesFiltering) lookup( MavenResourcesFiltering.class
+ .getName() );
+
+ Resource resource = new Resource();
+ List resources = new ArrayList();
+ resources.add( resource );
+ resource.setDirectory( getBasedir() + "/src/test/units-files/maven-resources-filtering" );
+ resource.setFiltering( false );
+ mavenResourcesFiltering.filterResources( resources, outputDirectory, mavenProject, null, null );
+
+ assertEquals( 2, outputDirectory.listFiles().length );
+ Properties result = PropertyUtils.loadPropertyFile( new File( outputDirectory,
+ "empty-maven-resources-filtering.txt" ), null );
+ assertTrue( result.isEmpty() );
+
+ result = PropertyUtils.loadPropertyFile( new File( outputDirectory, "maven-resources-filtering.txt" ), null );
+ assertFalse( result.isEmpty() );
+
+ assertEquals( "${pom.version}", result.get( "version" ) );
+ assertEquals( "${pom.groupId}", result.get( "groupId" ) );
+ assertEquals( "${foo}", result.get( "foo" ) );
+ }
+}
diff --git a/src/test/java/org/apache/maven/shared/filtering/StubMavenProject.java b/src/test/java/org/apache/maven/shared/filtering/StubMavenProject.java
new file mode 100755
index 0000000..b7ce5ab
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/filtering/StubMavenProject.java
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+package org.apache.maven.shared.filtering;
+
+import java.util.Properties;
+
+import org.apache.maven.project.MavenProject;
+
+/**
+ * @author <a href="mailto:olamy@apache.org">olamy</a>
+ * @since 28 janv. 08
+ * @version $Id$
+ */
+public class StubMavenProject
+ extends MavenProject
+{
+ private Properties properties;
+
+ public Properties getProperties()
+ {
+ return this.properties;
+ }
+
+ public void setProperties( Properties properties )
+ {
+ this.properties = properties;
+ }
+
+
+}
diff --git a/src/test/units-files/maven-resources-filtering/empty-maven-resources-filtering.txt b/src/test/units-files/maven-resources-filtering/empty-maven-resources-filtering.txt
new file mode 100755
index 0000000..eb3e59f
--- /dev/null
+++ b/src/test/units-files/maven-resources-filtering/empty-maven-resources-filtering.txt
@@ -0,0 +1,18 @@
+#/*
+# * 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.
+# */
\ No newline at end of file
diff --git a/src/test/units-files/maven-resources-filtering/maven-resources-filtering.txt b/src/test/units-files/maven-resources-filtering/maven-resources-filtering.txt
new file mode 100755
index 0000000..022a0a0
--- /dev/null
+++ b/src/test/units-files/maven-resources-filtering/maven-resources-filtering.txt
@@ -0,0 +1,22 @@
+#/*
+# * 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.
+# */
+version=${pom.version}
+groupId=${pom.groupId}
+foo=${foo}
+none=none filtered
\ No newline at end of file