Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encoding parameter to read and write properties files #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -44,13 +45,52 @@
public abstract class AbstractWritePropertiesMojo
extends AbstractMojo
{
/**
* Default encoding for the output file. Package private for testing.
*/
static final String DEFAULT_ENCODING = "ISO-8859-1";

@Parameter( defaultValue = "${project}", required = true, readonly = true )
private MavenProject project;

@Parameter( required = true, property = "properties.outputFile" )
private File outputFile;

/**
* The encoding to use when writing the properties file.
*/
@Parameter( required = false, defaultValue = DEFAULT_ENCODING )
private String encoding = DEFAULT_ENCODING;

/**
* Default scope for test access.
*
* @param project The test project.
*/
void setProject( MavenProject project )
{
this.project = project;
}

/**
* Default scope for test access.
*
* @param encoding to write the output file in
*/
void setEncoding( String encoding )
{
this.encoding = encoding;
}

/**
* Default scope for test access
*
* @param outputFile the outputFile to set
*/
void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}

/**
* @param properties {@link Properties}
* @param file {@link File}
Expand All @@ -59,6 +99,8 @@ public abstract class AbstractWritePropertiesMojo
protected void writeProperties( Properties properties, File file )
throws MojoExecutionException
{
getLog().debug( String.format( "Writing properties to %s using encoding %s", file.toString(), this.encoding ) );

try
{
storeWithoutTimestamp( properties, file, "Properties" );
Expand All @@ -79,7 +121,7 @@ protected void writeProperties( Properties properties, File file )
private void storeWithoutTimestamp( Properties properties, File outputFile, String comments )
throws IOException
{
try ( PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" ); StringWriter sw = new StringWriter() )
try ( PrintWriter pw = new PrintWriter( outputFile, this.encoding ); StringWriter sw = new StringWriter() )
{
properties.store( sw, comments );
comments = '#' + comments;
Expand Down Expand Up @@ -122,6 +164,21 @@ protected void validateOutputFile()
}
}

/**
* @throws MojoExecutionException {@link MojoExecutionException}
*/
protected void validateEncoding()
throws MojoExecutionException
{
try
{
Charset.forName(this.encoding);
}
catch(IllegalArgumentException e)
{
throw new MojoExecutionException(String.format("Invalid encoding '%s'", this.encoding), e);
}
}
/**
* @return {@link MavenProject}
*/
Expand Down
50 changes: 39 additions & 11 deletions src/main/java/org/codehaus/mojo/properties/ReadPropertiesMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Properties;

Expand All @@ -50,6 +52,11 @@
public class ReadPropertiesMojo
extends AbstractMojo
{
/**
* Default encoding for the input properties file/url. Package private for testing.
*/
static final String DEFAULT_ENCODING = "ISO-8859-1";

@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;

Expand Down Expand Up @@ -122,6 +129,16 @@ public void setKeyPrefix( String keyPrefix )
@Parameter( defaultValue = "false", property = "prop.skipLoadProperties" )
private boolean skipLoadProperties;

/**
* The encoding of the properties files.
*/
@Parameter( required = false, defaultValue = DEFAULT_ENCODING )
private String encoding = DEFAULT_ENCODING;

void setEncoding ( String encoding ) {
this.encoding = encoding;
}

/**
* Used for resolving property placeholders.
*/
Expand Down Expand Up @@ -152,6 +169,14 @@ private void checkParameters()
throw new MojoExecutionException( "Set files or URLs but not both - otherwise "
+ "no order of precedence can be guaranteed" );
}
try
{
Charset.forName(this.encoding);
}
catch(IllegalArgumentException e)
{
throw new MojoExecutionException(String.format("Invalid encoding '%s'", this.encoding), e);
}
}

private void loadFiles()
Expand Down Expand Up @@ -190,23 +215,26 @@ private void loadProperties( Resource resource )
{
try
{
getLog().debug( "Loading properties from " + resource );
getLog().debug( String.format( "Loading properties from %s using encoding %s", resource.toString(), this.encoding ) );

try ( InputStream stream = resource.getInputStream() )
{
if ( keyPrefix != null )
try (InputStreamReader streamReader = new InputStreamReader( stream, this.encoding ) )
{
Properties properties = new Properties();
properties.load( stream );
Properties projectProperties = project.getProperties();
for ( String key : properties.stringPropertyNames() )
if ( keyPrefix != null )
{
projectProperties.put( keyPrefix + key, properties.get( key ) );
Properties properties = new Properties();
properties.load( streamReader );
Properties projectProperties = project.getProperties();
for ( String key : properties.stringPropertyNames() )
{
projectProperties.put( keyPrefix + key, properties.get( key ) );
}
}
else
{
project.getProperties().load( streamReader );
}
}
else
{
project.getProperties().load( stream );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void execute()
throws MojoExecutionException
{
validateOutputFile();
validateEncoding();
List<Profile> list = getProject().getActiveProfiles();
if ( getLog().isInfoEnabled() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void execute()
throws MojoExecutionException
{
validateOutputFile();
validateEncoding();
Properties projProperties = new Properties();
projProperties.putAll( getProject().getProperties() );

Expand Down
Loading