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 4fa115c2..e79e135c 100644 --- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java +++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.StringWriter; +import java.nio.charset.Charset; import java.util.Collections; import java.util.List; import java.util.Properties; @@ -107,7 +108,8 @@ public void execute() throws MojoExecutionException { } StringWriter w = new StringWriter(); - String encoding = output != null ? project.getModel().getModelEncoding() : System.getProperty("file.encoding"); + String encoding = encodingOrDefault( + output != null ? project.getModel().getModelEncoding() : System.getProperty("file.encoding")); XMLWriter writer = new PrettyPrintXMLWriter( w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), encoding, null); @@ -213,6 +215,13 @@ private static void cleanModel(Model pom) { pom.setProperties(properties); } + static String encodingOrDefault(String encoding) { + if (encoding == null || encoding.isEmpty()) { + return Charset.defaultCharset().name(); + } + 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 00000000..33dbbf04 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/help/EffectivePomMojoTest.java @@ -0,0 +1,41 @@ +/* + * 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; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class EffectivePomMojoTest { + + @Test + void encodingFallbackNeverNull() { + String fromNull = EffectivePomMojo.encodingOrDefault(null); + assertNotNull(fromNull); + assertFalse(fromNull.isEmpty()); + + String fromBlank = EffectivePomMojo.encodingOrDefault(""); + assertNotNull(fromBlank); + assertFalse(fromBlank.isEmpty()); + + assertEquals("UTF-8", EffectivePomMojo.encodingOrDefault("UTF-8")); + } +}