diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSettingsXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSettingsXmlFactory.java index 0aecc0460ba2..25c3ba5d2f2a 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSettingsXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSettingsXmlFactory.java @@ -22,6 +22,7 @@ import java.io.OutputStream; import java.io.Reader; import java.io.Writer; +import java.util.function.Function; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.di.Named; @@ -31,6 +32,7 @@ import org.apache.maven.api.services.xml.XmlReaderRequest; import org.apache.maven.api.services.xml.XmlWriterException; import org.apache.maven.api.services.xml.XmlWriterRequest; +import org.apache.maven.api.settings.InputLocation; import org.apache.maven.api.settings.InputSource; import org.apache.maven.api.settings.Settings; import org.apache.maven.settings.v4.SettingsStaxReader; @@ -79,10 +81,20 @@ public void write(XmlWriterRequest request) throws XmlWriterException throw new IllegalArgumentException("writer or outputStream must be non null"); } try { + SettingsStaxWriter xmlWriter = new SettingsStaxWriter(); + xmlWriter.setAddLocationInformation(false); + + Function formatter = request.getInputLocationFormatter(); + if (formatter != null) { + xmlWriter.setAddLocationInformation(true); + Function adapter = formatter::apply; + xmlWriter.setStringFormatter(adapter); + } + if (writer != null) { - new SettingsStaxWriter().write(writer, content); + xmlWriter.write(writer, content); } else { - new SettingsStaxWriter().write(outputStream, content); + xmlWriter.write(outputStream, content); } } catch (Exception e) { throw new XmlWriterException("Unable to write settings: " + getMessage(e), getLocation(e), e); diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultToolchainsXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultToolchainsXmlFactory.java index 89452d1367a1..3400cda47bd8 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultToolchainsXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultToolchainsXmlFactory.java @@ -23,6 +23,7 @@ import java.io.Reader; import java.io.Writer; import java.util.Objects; +import java.util.function.Function; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.di.Named; @@ -32,6 +33,7 @@ import org.apache.maven.api.services.xml.XmlReaderRequest; import org.apache.maven.api.services.xml.XmlWriterException; import org.apache.maven.api.services.xml.XmlWriterRequest; +import org.apache.maven.api.toolchain.InputLocation; import org.apache.maven.api.toolchain.InputSource; import org.apache.maven.api.toolchain.PersistedToolchains; import org.apache.maven.toolchain.v4.MavenToolchainsStaxReader; @@ -81,10 +83,20 @@ public void write(XmlWriterRequest request) throws XmlWrite throw new IllegalArgumentException("writer or outputStream must be non null"); } try { + MavenToolchainsStaxWriter xmlWriter = new MavenToolchainsStaxWriter(); + xmlWriter.setAddLocationInformation(false); + + Function formatter = request.getInputLocationFormatter(); + if (formatter != null) { + xmlWriter.setAddLocationInformation(true); + Function adapter = formatter::apply; + xmlWriter.setStringFormatter(adapter); + } + if (writer != null) { - new MavenToolchainsStaxWriter().write(writer, content); + xmlWriter.write(writer, content); } else { - new MavenToolchainsStaxWriter().write(outputStream, content); + xmlWriter.write(outputStream, content); } } catch (Exception e) { throw new XmlWriterException("Unable to write toolchains: " + getMessage(e), getLocation(e), e);