Skip to content

Commit 2e9482b

Browse files
committed
adhere to project encoding while reading-writing property files
1 parent 01ee123 commit 2e9482b

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.io.*;
4545
import java.net.InetAddress;
4646
import java.net.UnknownHostException;
47+
import java.nio.charset.Charset;
4748
import java.nio.charset.StandardCharsets;
4849
import java.text.SimpleDateFormat;
4950
import java.util.Date;
@@ -101,7 +102,7 @@ public class GitCommitIdMojo extends AbstractMojo {
101102
/**
102103
* The Maven Session Object.
103104
*/
104-
@Parameter(required = true, readonly = true)
105+
@Parameter(property = "session", required = true, readonly = true)
105106
private MavenSession session;
106107

107108
/**
@@ -304,6 +305,11 @@ public class GitCommitIdMojo extends AbstractMojo {
304305
*/
305306
private Properties properties;
306307

308+
/**
309+
* Charset to read-write project sources.
310+
*/
311+
private Charset sourceCharset = StandardCharsets.UTF_8;
312+
307313
@NotNull
308314
private final LoggerBridge log = new MavenLoggerBridge(this, false);
309315

@@ -313,6 +319,14 @@ public void execute() throws MojoExecutionException {
313319
// Set the verbose setting: now it should be correctly loaded from maven.
314320
log.setVerbose(verbose);
315321

322+
// read source encoding from project properties for those who still doesn't use UTF-8
323+
String sourceEncoding = project.getProperties().getProperty("project.build.sourceEncoding");
324+
if (null != sourceEncoding) {
325+
sourceCharset = Charset.forName(sourceEncoding);
326+
} else {
327+
sourceCharset = Charset.defaultCharset();
328+
}
329+
316330
if (skip) {
317331
log.info("skip is enabled, skipping execution!");
318332
return;
@@ -616,7 +630,7 @@ void maybeGeneratePropertiesFile(@NotNull Properties localProperties, File base,
616630
boolean threw = true;
617631

618632
try {
619-
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), StandardCharsets.UTF_8);
633+
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), sourceCharset);
620634
if (isJsonFormat) {
621635
log.info("Writing json file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), project.getName());
622636
ObjectMapper mapper = new ObjectMapper();
@@ -666,7 +680,7 @@ private boolean directoryExists(@Nullable File fileLocation) {
666680
}
667681

668682
@SuppressWarnings( "resource" )
669-
static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFileException {
683+
private Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFileException {
670684
final HashMap<String, Object> propertiesMap;
671685

672686
{
@@ -678,7 +692,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFi
678692
final FileInputStream fis = new FileInputStream(jsonFile);
679693
closeable = fis;
680694

681-
final InputStreamReader reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
695+
final InputStreamReader reader = new InputStreamReader(fis, sourceCharset);
682696
closeable = reader;
683697

684698
final ObjectMapper mapper = new ObjectMapper();
@@ -706,7 +720,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFi
706720
}
707721

708722
@SuppressWarnings( "resource" )
709-
static Properties readProperties(@NotNull File propertiesFile) throws CannotReadFileException {
723+
private Properties readProperties(@NotNull File propertiesFile) throws CannotReadFileException {
710724
Closeable closeable = null;
711725

712726
try {
@@ -715,7 +729,7 @@ static Properties readProperties(@NotNull File propertiesFile) throws CannotRead
715729
final FileInputStream fis = new FileInputStream(propertiesFile);
716730
closeable = fis;
717731

718-
final InputStreamReader reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
732+
final InputStreamReader reader = new InputStreamReader(fis, sourceCharset);
719733
closeable = reader;
720734

721735
final Properties retVal = new Properties();

0 commit comments

Comments
 (0)