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 2020/07/16 07:27:04 UTC
[maven-filtering] branch master updated: [SHARED-934] Allow using a
different encoding when filtering properties files.
This is an automated email from the ASF dual-hosted git repository.
dennisl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-filtering.git
The following commit(s) were added to refs/heads/master by this push:
new 6852c50 [SHARED-934] Allow using a different encoding when filtering properties files.
6852c50 is described below
commit 6852c504dc7495f708daea921b5fad16349eab68
Author: Dennis Lundberg <de...@apache.org>
AuthorDate: Thu Jul 16 09:24:05 2020 +0200
[SHARED-934] Allow using a different encoding when filtering properties files.
---
pom.xml | 2 +-
.../filtering/DefaultMavenResourcesFiltering.java | 50 ++++++++++++++++-
.../shared/filtering/MavenResourcesExecution.java | 27 +++++++++
.../DefaultMavenResourcesFilteringTest.java | 64 ++++++++++++++++++++++
.../units-files/MRESOURCES-171/test.properties | 19 +++++++
src/test/units-files/MRESOURCES-171/test.txt | 18 ++++++
6 files changed, 178 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 92ebaed..82e10c6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,7 +27,7 @@
</parent>
<artifactId>maven-filtering</artifactId>
- <version>3.1.2-SNAPSHOT</version>
+ <version>3.2.0-SNAPSHOT</version>
<name>Apache Maven Filtering</name>
<description>A component to assist in filtering of resource files with properties from a Maven project.</description>
diff --git a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
index ab73c41..a3d8a81 100644
--- a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
+++ b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
@@ -230,9 +230,14 @@ public class DefaultMavenResourcesFiltering
boolean filteredExt =
filteredFileExtension( source.getName(), mavenResourcesExecution.getNonFilteredFileExtensions() );
+ // Determine which encoding to use when filtering this file
+ String encoding = getEncoding( source, mavenResourcesExecution.getEncoding(),
+ mavenResourcesExecution.getPropertiesEncoding() );
+ getLogger().debug( "Using '" + encoding + "' encoding to copy filtered resource '"
+ + source.getName() + "'." );
mavenFileFilter.copyFile( source, destinationFile, resource.isFiltering() && filteredExt,
mavenResourcesExecution.getFilterWrappers(),
- mavenResourcesExecution.getEncoding(),
+ encoding,
mavenResourcesExecution.isOverwrite() );
}
@@ -259,6 +264,49 @@ public class DefaultMavenResourcesFiltering
}
+ /**
+ * Get the encoding to use when filtering the specified file. Properties files can be configured to use a different
+ * encoding than regular files.
+ *
+ * @param file The file to check
+ * @param encoding The encoding to use for regular files
+ * @param propertiesEncoding The encoding to use for properties files
+ * @return The encoding to use when filtering the specified file
+ * @since 3.2.0
+ */
+ static String getEncoding( File file, String encoding, String propertiesEncoding )
+ {
+ if ( isPropertiesFile( file ) )
+ {
+ if ( propertiesEncoding == null )
+ {
+ // Since propertiesEncoding is a new feature, not all plugins will have implemented support for it.
+ // These plugins will have propertiesEncoding set to null.
+ return encoding;
+ }
+ else
+ {
+ return propertiesEncoding;
+ }
+ }
+ else
+ {
+ return encoding;
+ }
+ }
+
+ /**
+ * Determine whether a file is a properties file or not.
+ *
+ * @param file The file to check
+ * @return <code>true</code> if the file name has an extension of "properties", otherwise <code>false</code>
+ * @since 3.2.0
+ */
+ static boolean isPropertiesFile( File file )
+ {
+ return "properties".equals( StringUtils.lowerCase( FileUtils.extension( file.getName() ) ) );
+ }
+
private void handleDefaultFilterWrappers( MavenResourcesExecution mavenResourcesExecution )
throws MavenFilteringException
{
diff --git a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
index 71a2fcf..6d68126 100644
--- a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
+++ b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
@@ -58,6 +58,11 @@ public class MavenResourcesExecution
private String encoding;
/**
+ * @since 3.2.0
+ */
+ private String propertiesEncoding;
+
+ /**
* By default files like {@code .gitignore}, {@code .cvsignore} etc. are excluded which means they will not being
* copied. If you need them for a particular reason you can do that by settings this to {@code false}. This means
* all files like the following will be copied.
@@ -184,6 +189,28 @@ public class MavenResourcesExecution
}
/**
+ * Return the encoding of properties files.
+ *
+ * @return Current encoding of properties files.
+ * @since 3.2.0
+ */
+ public String getPropertiesEncoding()
+ {
+ return propertiesEncoding;
+ }
+
+ /**
+ * Set the value for encoding of properties files.
+ *
+ * @param propertiesEncoding Give the new value for encoding of properties files.
+ * @since 3.2.0
+ */
+ public void setPropertiesEncoding( String propertiesEncoding )
+ {
+ this.propertiesEncoding = propertiesEncoding;
+ }
+
+ /**
* @return List of {@link org.apache.maven.model.Resource}
*/
public List<Resource> getResources()
diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
index 71dfe5a..253634a 100644
--- a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
+++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
@@ -975,4 +975,68 @@ public class DefaultMavenResourcesFilteringTest
assertEquals( "1.0.txt", files[0].getName() );
}
+ /**
+ * MRESOURCES-171: Use correct encoding when filtering properties-files
+ */
+ public void testFilterPropertiesFiles()
+ throws Exception
+ {
+ File baseDir = new File( "/foo/bar" );
+ StubMavenProject mavenProject = new StubMavenProject( baseDir );
+ mavenProject.setVersion( "1.0" );
+ mavenProject.setGroupId( "org.apache" );
+ mavenProject.setName( "test project" );
+
+ MavenResourcesFiltering mavenResourcesFiltering = lookup( MavenResourcesFiltering.class );
+
+ String unitFilesDir = getBasedir() + "/src/test/units-files/MRESOURCES-171";
+
+ Resource resource = new Resource();
+ resource.setDirectory( unitFilesDir );
+ resource.setFiltering( true );
+ resource.setTargetPath( "testFilterPropertiesFiles" );
+
+ MavenResourcesExecution mavenResourcesExecution =
+ new MavenResourcesExecution( Collections.singletonList( resource ), outputDirectory, mavenProject, "UTF-8",
+ Collections.<String> emptyList(), Collections.<String> emptyList(),
+ new StubMavenSession() );
+ mavenResourcesExecution.setPropertiesEncoding( "ISO-8859-1" );
+ mavenResourcesFiltering.filterResources( mavenResourcesExecution );
+
+ File targetPathFile = new File( outputDirectory, "testFilterPropertiesFiles" );
+ assertTrue( FileUtils.contentEquals( new File( unitFilesDir, "test.properties" ),
+ new File( targetPathFile, "test.properties" ) ) );
+ assertTrue( FileUtils.contentEquals( new File( unitFilesDir, "test.txt" ),
+ new File( targetPathFile, "test.txt" ) ) );
+ }
+
+ public void testGetEncoding()
+ {
+ String ISO88591 = "ISO-8859-1";
+ String UTF8 = "UTF-8";
+ File propertiesFile = new File( "file.properties" );
+ File regularFile = new File( "file.xml" );
+
+ // Properties files
+ assertEquals( null, DefaultMavenResourcesFiltering.getEncoding( propertiesFile, null, null ) );
+ assertEquals( UTF8, DefaultMavenResourcesFiltering.getEncoding( propertiesFile, "UTF-8", null ) );
+ assertEquals( ISO88591, DefaultMavenResourcesFiltering.getEncoding( propertiesFile, "UTF-8", ISO88591 ) );
+ // Regular files
+ assertEquals( null, DefaultMavenResourcesFiltering.getEncoding( regularFile, null, null ) );
+ assertEquals( UTF8, DefaultMavenResourcesFiltering.getEncoding( regularFile, "UTF-8", null ) );
+ assertEquals( UTF8, DefaultMavenResourcesFiltering.getEncoding( regularFile, "UTF-8", ISO88591 ) );
+ }
+
+ public void testIsPropertiesFile()
+ {
+ // Properties files
+ assertTrue( DefaultMavenResourcesFiltering.isPropertiesFile( new File( "file.properties" ) ) );
+ assertTrue( DefaultMavenResourcesFiltering.isPropertiesFile( new File( "some/parent/path",
+ "file.properties" ) ) );
+ // Regular files
+ assertFalse( DefaultMavenResourcesFiltering.isPropertiesFile( new File( "file" ) ) );
+ assertFalse( DefaultMavenResourcesFiltering.isPropertiesFile( new File( "some/parent/path", "file" ) ) );
+ assertFalse( DefaultMavenResourcesFiltering.isPropertiesFile( new File( "file.xml" ) ) );
+ assertFalse( DefaultMavenResourcesFiltering.isPropertiesFile( new File( "some/parent/path", "file.xml" ) ) );
+ }
}
diff --git a/src/test/units-files/MRESOURCES-171/test.properties b/src/test/units-files/MRESOURCES-171/test.properties
new file mode 100644
index 0000000..7cc2b46
--- /dev/null
+++ b/src/test/units-files/MRESOURCES-171/test.properties
@@ -0,0 +1,19 @@
+# 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.
+
+actual=No hay conexi�n
+expected=No hay conexi\u00f3n
diff --git a/src/test/units-files/MRESOURCES-171/test.txt b/src/test/units-files/MRESOURCES-171/test.txt
new file mode 100644
index 0000000..281e65c
--- /dev/null
+++ b/src/test/units-files/MRESOURCES-171/test.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.
+
+Some text encoded in UTF-8 containing non-ascii characters conexión.