Skip to content

Commit ca10920

Browse files
Write git.json file with UTF-8 encoding.
Resolves #130
1 parent 02ce239 commit ca10920

File tree

2 files changed

+3
-64
lines changed

2 files changed

+3
-64
lines changed

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.base.Function;
2323
import com.google.common.base.Predicate;
2424
import com.google.common.base.Predicates;
25-
import com.google.common.base.Strings;
2625
import com.google.common.collect.Lists;
2726
import com.google.common.io.Closeables;
2827
import com.google.common.io.Files;
@@ -272,14 +271,6 @@ public class GitCommitIdMojo extends AbstractMojo {
272271
@SuppressWarnings("UnusedDeclaration")
273272
private List<String> excludeProperties = Collections.emptyList();
274273

275-
/**
276-
* Specifies the character encoding for the properties file that is generated by this plugin
277-
*
278-
* @parameter expression="${encoding}" default-value="${project.reporting.outputEncoding}
279-
* @since 2.1.11
280-
*/
281-
private String encoding;
282-
283274
/**
284275
* The properties we store our data in and then expose them
285276
*/
@@ -499,7 +490,7 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
499490
try {
500491
Files.createParentDirs(gitPropsFile);
501492

502-
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), determineEncoding());
493+
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charset.forName("UTF-8"));
503494
if ("json".equalsIgnoreCase(format)) {
504495
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
505496
ObjectMapper mapper = new ObjectMapper();
@@ -516,20 +507,6 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
516507
}
517508
}
518509

519-
private String determineEncoding() {
520-
if (Strings.isNullOrEmpty(this.encoding) )
521-
{
522-
getLog().warn(
523-
"File encoding has not been set, using platform encoding " + Charset.defaultCharset().name()
524-
+ ", i.e. build is platform dependent!" );
525-
return Charset.defaultCharset().name();
526-
}
527-
else
528-
{
529-
return this.encoding;
530-
}
531-
}
532-
533510
@VisibleForTesting
534511
File craftPropertiesOutputFile(File base, String propertiesFilename){
535512
File returnPath = new File(base, propertiesFilename);

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -208,42 +208,6 @@ public void shouldGenerateCustomPropertiesFileProperties(boolean useNativeGit) t
208208
@Test
209209
@Parameters(method = "useNativeGit")
210210
public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws Exception {
211-
// given
212-
mavenSandbox.withParentProject("my-pom-project", "pom")
213-
.withChildProject("my-jar-module", "jar")
214-
.withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID)
215-
.create(CleanUp.CLEANUP_FIRST);
216-
217-
MavenProject targetProject = mavenSandbox.getChildProject();
218-
219-
String targetFilePath = "target/classes/custom-git.properties";
220-
File expectedFile = new File(targetProject.getBasedir(), targetFilePath);
221-
222-
setProjectToExecuteMojoIn(targetProject);
223-
alterMojoSettings("generateGitPropertiesFile", true);
224-
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
225-
alterMojoSettings("format", "json");
226-
alterMojoSettings("useNativeGit", useNativeGit);
227-
228-
// when
229-
try {
230-
mojo.execute();
231-
232-
// then
233-
assertThat(expectedFile).exists();
234-
String json = Files.toString(expectedFile, Charset.defaultCharset());
235-
ObjectMapper om = new ObjectMapper();
236-
Map<String, String> map = new HashMap<String, String>();
237-
map = om.readValue(expectedFile, map.getClass());
238-
assertThat(map.size() > 10);
239-
} finally {
240-
FileUtils.forceDelete(expectedFile);
241-
}
242-
}
243-
244-
@Test
245-
@Parameters(value = {"UTF-8", "ISO-8859-1"})
246-
public void shouldGenerateCustomPropertiesFileJsonWithCustomEncoding(String encoding) throws Exception {
247211
// given
248212
mavenSandbox.withParentProject("my-pom-project", "pom")
249213
.withChildProject("my-jar-module", "jar")
@@ -254,21 +218,19 @@ public void shouldGenerateCustomPropertiesFileJsonWithCustomEncoding(String enco
254218

255219
String targetFilePath = "target/classes/custom-git.properties";
256220
File expectedFile = new File(targetProject.getBasedir(), targetFilePath);
257-
Charset charset = Charset.forName(encoding);
258221

259222
setProjectToExecuteMojoIn(targetProject);
260223
alterMojoSettings("generateGitPropertiesFile", true);
261224
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
262225
alterMojoSettings("format", "json");
263-
alterMojoSettings("useNativeGit", true);
264-
alterMojoSettings("encoding", charset.name());
226+
alterMojoSettings("useNativeGit", useNativeGit);
265227
// when
266228
try {
267229
mojo.execute();
268230

269231
// then
270232
assertThat(expectedFile).exists();
271-
String json = Files.toString(expectedFile, charset);
233+
String json = Files.toString(expectedFile, Charset.forName("UTF-8"));
272234
ObjectMapper om = new ObjectMapper();
273235
Map<String, String> map = new HashMap<String, String>();
274236
map = om.readValue(json, map.getClass());

0 commit comments

Comments
 (0)