diff --git a/pom.xml b/pom.xml index 92ebaed..82e10c6 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ maven-filtering - 3.1.2-SNAPSHOT + 3.2.0-SNAPSHOT Apache Maven Filtering A component to assist in filtering of resource files with properties from a Maven project. 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 void filterResources( MavenResourcesExecution mavenResourcesExecution ) 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 void filterResources( MavenResourcesExecution mavenResourcesExecution ) } + /** + * 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 true if the file name has an extension of "properties", otherwise false + * @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 @@ -57,6 +57,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 @@ -183,6 +188,28 @@ public void setEncoding( String encoding ) this.encoding = encoding; } + /** + * 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} */ 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 @@ Collections. emptyList(), Collections. emptyList(), 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. emptyList(), Collections. 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.