diff --git a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java index ac3294ad..d7f8eaf5 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java @@ -107,7 +107,7 @@ public void execute() throws MojoExecutionException { } StringWriter w = new StringWriter(); - String encoding = output != null ? project.getModel().getModelEncoding() : System.getProperty("file.encoding"); + String encoding = encodingOrDefault(project.getModel().getModelEncoding()); XMLWriter writer = new PrettyPrintXMLWriter( w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), encoding, null); @@ -213,6 +213,13 @@ private static void cleanModel(Model pom) { pom.setProperties(properties); } + static String encodingOrDefault(String encoding) { + if (encoding == null || encoding.isEmpty()) { + return "UTF-8"; + } + return encoding; + } + private static class InputLocationStringFormatter extends InputLocation.StringFormatter { @Override public String toString(InputLocation location) { diff --git a/src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java b/src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java index 03924248..a8e20e7a 100644 --- a/src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java @@ -75,8 +75,6 @@ void setup() throws IOException { model.setProperties(originalProperties); - when(project.getModel()).thenReturn(model); - Path outputPath = Files.createTempFile(tempDir, "maven-help-plugin-test-", ".xml"); mavenSession.getUserProperties().setProperty("outputPath", outputPath.toString()); } @@ -89,6 +87,7 @@ void testExecuteDoesNotModifyProjectModel(EffectivePomMojo mojo) throws Exceptio Properties expectedProperties = new Properties(); expectedProperties.putAll(originalProperties); + when(project.getModel()).thenReturn(model); setVariableValueToObject(mojo, "projects", Collections.singletonList(project)); setVariableValueToObject(mojo, "mojoExecution", mojoExecution); @@ -104,4 +103,16 @@ void testExecuteDoesNotModifyProjectModel(EffectivePomMojo mojo) throws Exceptio model.getProperties(), "effective-pom must not modify the properties of the project model"); } + + @Test + void encodingOrDefaultFallsBackToUtf8() { + assertEquals("UTF-8", EffectivePomMojo.encodingOrDefault(null)); + assertEquals("UTF-8", EffectivePomMojo.encodingOrDefault("")); + } + + @Test + void encodingOrDefaultKeepsExplicitValue() { + assertEquals("UTF-8", EffectivePomMojo.encodingOrDefault("UTF-8")); + assertEquals("ISO-8859-1", EffectivePomMojo.encodingOrDefault("ISO-8859-1")); + } }