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 4fa115c..f585562 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 new file mode 100644 index 0000000..73de3cf --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.help; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class EffectivePomMojoTest { + + @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")); + } +}