Skip to content

Commit

Permalink
[SHARED-934] Allow using a different encoding when filtering properti…
Browse files Browse the repository at this point in the history
…es files.
  • Loading branch information
dennisl committed Jul 16, 2020
1 parent 64de258 commit 6852c50
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}

Expand All @@ -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 <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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,4 +975,68 @@ Collections.<String> emptyList(), Collections.<String> 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.<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" ) ) );
}
}
19 changes: 19 additions & 0 deletions src/test/units-files/MRESOURCES-171/test.properties
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions src/test/units-files/MRESOURCES-171/test.txt
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 6852c50

Please sign in to comment.