This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new 3e38c39b60 Fix #11899: Default addLocationInformation to false in
Settings and Toolchains XML writers (#11984)
3e38c39b60 is described below
commit 3e38c39b60199cbf2eef32a8b401752cd1551f34
Author: Abhishek Chauhan <[email protected]>
AuthorDate: Wed May 20 10:23:13 2026 +0530
Fix #11899: Default addLocationInformation to false in Settings and
Toolchains XML writers (#11984)
* Fix gh-11899: Default addLocationInformation to false in Settings and
Toolchains XML writers
DefaultSettingsXmlFactory.write() and DefaultToolchainsXmlFactory.write()
always emitted InputLocation comments (e.g. <!-- /path/to/settings.xml:163
-->)
because they created bare StaxWriter instances whose addLocationInformation
field defaults to true, and never called setAddLocationInformation(false).
Align both factories with DefaultModelXmlFactory.write(), which correctly
defaults location tracking to false and only enables it when an explicit
inputLocationFormatter is provided via XmlWriterRequest.
Fixes #11899
* Add tests for DefaultSettingsXmlFactory location tracking behavior
Co-Authored-By: Claude Opus 4.6 <[email protected]>
* Add DefaultToolchainsXmlFactory test and strengthen assertions
Co-Authored-By: Claude Opus 4.6 <[email protected]>
---------
Co-authored-by: Abhishek Chauhan <[email protected]>
Co-authored-by: Guillaume Nodet <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
---
.../maven/impl/DefaultSettingsXmlFactory.java | 16 ++-
.../maven/impl/DefaultToolchainsXmlFactory.java | 16 ++-
.../maven/impl/DefaultSettingsXmlFactoryTest.java | 108 +++++++++++++++++++++
.../impl/DefaultToolchainsXmlFactoryTest.java | 108 +++++++++++++++++++++
4 files changed, 244 insertions(+), 4 deletions(-)
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 0aecc0460b..25c3ba5d2f 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<Settings> request)
throws XmlWriterException
throw new IllegalArgumentException("writer or outputStream must be
non null");
}
try {
+ SettingsStaxWriter xmlWriter = new SettingsStaxWriter();
+ xmlWriter.setAddLocationInformation(false);
+
+ Function<Object, String> formatter =
request.getInputLocationFormatter();
+ if (formatter != null) {
+ xmlWriter.setAddLocationInformation(true);
+ Function<InputLocation, String> 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 89452d1367..3400cda47b 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<PersistedToolchains>
request) throws XmlWrite
throw new IllegalArgumentException("writer or outputStream must be
non null");
}
try {
+ MavenToolchainsStaxWriter xmlWriter = new
MavenToolchainsStaxWriter();
+ xmlWriter.setAddLocationInformation(false);
+
+ Function<Object, String> formatter =
request.getInputLocationFormatter();
+ if (formatter != null) {
+ xmlWriter.setAddLocationInformation(true);
+ Function<InputLocation, String> 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);
diff --git
a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultSettingsXmlFactoryTest.java
b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultSettingsXmlFactoryTest.java
new file mode 100644
index 0000000000..38f761e90f
--- /dev/null
+++
b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultSettingsXmlFactoryTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.impl;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.function.Function;
+
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.services.xml.XmlWriterRequest;
+import org.apache.maven.api.settings.Settings;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DefaultSettingsXmlFactoryTest {
+
+ private DefaultSettingsXmlFactory factory;
+
+ @BeforeEach
+ void setUp() {
+ factory = new DefaultSettingsXmlFactory();
+ }
+
+ @Test
+ void testWriteWithoutFormatterDisablesLocationTracking() throws Exception {
+ String xml = """
+ <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0">
+ <localRepository>/path/to/local/repo</localRepository>
+ <mirrors>
+ <mirror>
+ <mirrorOf>external:http:*</mirrorOf>
+ <name>Pseudo repository</name>
+ <url>http://0.0.0.0/</url>
+ <id>pseudo</id>
+ </mirror>
+ </mirrors>
+ </settings>""";
+
+ Settings settings = factory.read(XmlReaderRequest.builder()
+ .reader(new StringReader(xml))
+ .strict(true)
+ .build());
+
+ StringWriter out = new StringWriter();
+ factory.write(XmlWriterRequest.<Settings>builder()
+ .writer(out)
+ .content(settings)
+ .build());
+
+ String result = out.toString();
+ assertTrue(result.contains("<localRepository>"), "Expected settings
content to be written to output");
+ assertFalse(result.contains("<!--"), "No XML comments should be
present when no formatter is provided");
+ }
+
+ @Test
+ void testWriteWithFormatterEnablesLocationTracking() throws Exception {
+ String xml = """
+ <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0">
+ <localRepository>/path/to/local/repo</localRepository>
+ <mirrors>
+ <mirror>
+ <mirrorOf>external:http:*</mirrorOf>
+ <name>Pseudo repository</name>
+ <url>http://0.0.0.0/</url>
+ <id>pseudo</id>
+ </mirror>
+ </mirrors>
+ </settings>""";
+
+ Settings settings = factory.read(XmlReaderRequest.builder()
+ .reader(new StringReader(xml))
+ .location("settings.xml")
+ .strict(true)
+ .build());
+
+ StringWriter out = new StringWriter();
+ Function<Object, String> formatter = o -> "LOC_MARK";
+
+ factory.write(XmlWriterRequest.<Settings>builder()
+ .writer(out)
+ .content(settings)
+ .inputLocationFormatter(formatter)
+ .build());
+
+ String result = out.toString();
+ assertTrue(result.contains("<localRepository>"), "Expected settings
content to be written to output");
+ assertTrue(result.contains("LOC_MARK"), "Expected formatter marker in
output when tracking is enabled");
+ }
+}
diff --git
a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultToolchainsXmlFactoryTest.java
b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultToolchainsXmlFactoryTest.java
new file mode 100644
index 0000000000..181213a3c1
--- /dev/null
+++
b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultToolchainsXmlFactoryTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.impl;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.function.Function;
+
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.services.xml.XmlWriterRequest;
+import org.apache.maven.api.toolchain.PersistedToolchains;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DefaultToolchainsXmlFactoryTest {
+
+ private DefaultToolchainsXmlFactory factory;
+
+ @BeforeEach
+ void setUp() {
+ factory = new DefaultToolchainsXmlFactory();
+ }
+
+ @Test
+ void testWriteWithoutFormatterDisablesLocationTracking() throws Exception {
+ String xml = """
+ <toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0">
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>17</version>
+ </provides>
+ <configuration>
+ <jdkHome>/usr/lib/jvm/java-17</jdkHome>
+ </configuration>
+ </toolchain>
+ </toolchains>""";
+
+ PersistedToolchains toolchains =
factory.read(XmlReaderRequest.builder()
+ .reader(new StringReader(xml))
+ .strict(true)
+ .build());
+
+ StringWriter out = new StringWriter();
+ factory.write(XmlWriterRequest.<PersistedToolchains>builder()
+ .writer(out)
+ .content(toolchains)
+ .build());
+
+ String result = out.toString();
+ assertTrue(result.contains("<type>jdk</type>"), "Expected toolchains
content to be written to output");
+ assertFalse(result.contains("<!--"), "No XML comments should be
present when no formatter is provided");
+ }
+
+ @Test
+ void testWriteWithFormatterEnablesLocationTracking() throws Exception {
+ String xml = """
+ <toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0">
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>17</version>
+ </provides>
+ <configuration>
+ <jdkHome>/usr/lib/jvm/java-17</jdkHome>
+ </configuration>
+ </toolchain>
+ </toolchains>""";
+
+ PersistedToolchains toolchains =
factory.read(XmlReaderRequest.builder()
+ .reader(new StringReader(xml))
+ .location("toolchains.xml")
+ .strict(true)
+ .build());
+
+ StringWriter out = new StringWriter();
+ Function<Object, String> formatter = o -> "LOC_MARK";
+
+ factory.write(XmlWriterRequest.<PersistedToolchains>builder()
+ .writer(out)
+ .content(toolchains)
+ .inputLocationFormatter(formatter)
+ .build());
+
+ String result = out.toString();
+ assertTrue(result.contains("<type>jdk</type>"), "Expected toolchains
content to be written to output");
+ assertTrue(result.contains("LOC_MARK"), "Expected formatter marker in
output when tracking is enabled");
+ }
+}