44
44
import java .io .*;
45
45
import java .net .InetAddress ;
46
46
import java .net .UnknownHostException ;
47
+ import java .nio .charset .Charset ;
47
48
import java .nio .charset .StandardCharsets ;
48
49
import java .text .SimpleDateFormat ;
49
50
import java .util .Date ;
@@ -101,7 +102,7 @@ public class GitCommitIdMojo extends AbstractMojo {
101
102
/**
102
103
* The Maven Session Object.
103
104
*/
104
- @ Parameter (required = true , readonly = true )
105
+ @ Parameter (property = "session" , required = true , readonly = true )
105
106
private MavenSession session ;
106
107
107
108
/**
@@ -304,6 +305,11 @@ public class GitCommitIdMojo extends AbstractMojo {
304
305
*/
305
306
private Properties properties ;
306
307
308
+ /**
309
+ * Charset to read-write project sources.
310
+ */
311
+ private Charset sourceCharset = StandardCharsets .UTF_8 ;
312
+
307
313
@ NotNull
308
314
private final LoggerBridge log = new MavenLoggerBridge (this , false );
309
315
@@ -313,6 +319,14 @@ public void execute() throws MojoExecutionException {
313
319
// Set the verbose setting: now it should be correctly loaded from maven.
314
320
log .setVerbose (verbose );
315
321
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
+
316
330
if (skip ) {
317
331
log .info ("skip is enabled, skipping execution!" );
318
332
return ;
@@ -616,7 +630,7 @@ void maybeGeneratePropertiesFile(@NotNull Properties localProperties, File base,
616
630
boolean threw = true ;
617
631
618
632
try {
619
- outputWriter = new OutputStreamWriter (new FileOutputStream (gitPropsFile ), StandardCharsets . UTF_8 );
633
+ outputWriter = new OutputStreamWriter (new FileOutputStream (gitPropsFile ), sourceCharset );
620
634
if (isJsonFormat ) {
621
635
log .info ("Writing json file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), project .getName ());
622
636
ObjectMapper mapper = new ObjectMapper ();
@@ -666,7 +680,7 @@ private boolean directoryExists(@Nullable File fileLocation) {
666
680
}
667
681
668
682
@ SuppressWarnings ( "resource" )
669
- static Properties readJsonProperties (@ NotNull File jsonFile ) throws CannotReadFileException {
683
+ private Properties readJsonProperties (@ NotNull File jsonFile ) throws CannotReadFileException {
670
684
final HashMap <String , Object > propertiesMap ;
671
685
672
686
{
@@ -678,7 +692,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFi
678
692
final FileInputStream fis = new FileInputStream (jsonFile );
679
693
closeable = fis ;
680
694
681
- final InputStreamReader reader = new InputStreamReader (fis , StandardCharsets . UTF_8 );
695
+ final InputStreamReader reader = new InputStreamReader (fis , sourceCharset );
682
696
closeable = reader ;
683
697
684
698
final ObjectMapper mapper = new ObjectMapper ();
@@ -706,7 +720,7 @@ static Properties readJsonProperties(@NotNull File jsonFile) throws CannotReadFi
706
720
}
707
721
708
722
@ SuppressWarnings ( "resource" )
709
- static Properties readProperties (@ NotNull File propertiesFile ) throws CannotReadFileException {
723
+ private Properties readProperties (@ NotNull File propertiesFile ) throws CannotReadFileException {
710
724
Closeable closeable = null ;
711
725
712
726
try {
@@ -715,7 +729,7 @@ static Properties readProperties(@NotNull File propertiesFile) throws CannotRead
715
729
final FileInputStream fis = new FileInputStream (propertiesFile );
716
730
closeable = fis ;
717
731
718
- final InputStreamReader reader = new InputStreamReader (fis , StandardCharsets . UTF_8 );
732
+ final InputStreamReader reader = new InputStreamReader (fis , sourceCharset );
719
733
closeable = reader ;
720
734
721
735
final Properties retVal = new Properties ();
0 commit comments