This is an automated email from the ASF dual-hosted git repository.
slawekjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-help-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new b7e04b5 Fix #327: evaluate model collections on Maven 4 (#417)
b7e04b5 is described below
commit b7e04b5782de1d8bcee95fac212bcfbb62e8335a
Author: Václav Haisman <[email protected]>
AuthorDate: Sat Nov 14 11:47:24 2026 +0100
Fix #327: evaluate model collections on Maven 4 (#417)
* Add regression test for issue #327
* Fixes #327: Evaluate model collections without reflection
* Flatten Maven 4 model delegates in evaluate output
* Restore short XML aliases in evaluate output
Replace JAR entry path separators with dots without inserting regex
escapes. Skip package-info metadata during alias discovery to avoid
class-loading errors on Maven 4.
Verify short names for model collections, nested delegates, maps, and
settings servers.
* Hide Maven 4 collection classes and caches in evaluate output
Flattening the delegate still exposed the API model's immutable collection
implementations as
class="org.apache.maven.api.model.ImmutableCollections$List2"
attributes on every collection field, XStream back references to the shared
empty-collection singleton (reference="../includes"), string maps written as
<entry> pairs instead of the Maven 3 <key>value</key> form, and the lazily
populated caches pluginMap, reportSetMap and managementKey whenever an
earlier
call had filled them. The mapper now names the immutable collections like
the
default implementations, treats them as value types, and skips the caches; a
converter writes the API model's string maps like Properties. Maven 3 output
is unchanged.
---------
Co-authored-by: Sylwester Lachiewicz <[email protected]>
---
.../invoker.properties | 26 +++
.../evaluate-model-collections-maven4/pom.xml | 58 +++++++
.../test.properties} | 4 +-
.../verify.groovy | 73 ++++++++
.../evaluate-model-collections/invoker.properties | 1 -
.../evaluate-model-collections/verify.groovy | 1 +
.../evaluate-settings-servers/invoker.properties | 12 --
.../evaluate-settings-servers/verify.groovy | 13 +-
.../apache/maven/plugins/help/EvaluateMojo.java | 22 ++-
.../maven/plugins/help/MavenModelConverter.java | 185 +++++++++++++++++++++
.../plugins/help/MavenModelConverterTest.java | 102 ++++++++++++
11 files changed, 474 insertions(+), 23 deletions(-)
diff --git
a/src/it/projects/evaluate-model-collections-maven4/invoker.properties
b/src/it/projects/evaluate-model-collections-maven4/invoker.properties
new file mode 100644
index 0000000..8ddcbd6
--- /dev/null
+++ b/src/it/projects/evaluate-model-collections-maven4/invoker.properties
@@ -0,0 +1,26 @@
+# 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.
+
+# MPH-210 / #327: Maven 4 resource models contain immutable collections and
+# maps that must be serialized without reflection.
+invoker.goals.1 =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
+invoker.goals.2 =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-Dexpression=project.build.resources[0] -Doutput=resource.xml
+invoker.goals.3 =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-Dexpression=project.build -Doutput=build.xml
+invoker.goals.4 =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-Dexpression=project.build.resources[1].includes -Doutput=empty.xml
+invoker.goals.5 =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-Dexpression=project.build.pluginsAsMap -Doutput=plugins.xml
+invoker.goals.6 =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-Dexpression=project.model.profiles -Doutput=profiles.xml
+invoker.maven.version = 4+
diff --git a/src/it/projects/evaluate-model-collections-maven4/pom.xml
b/src/it/projects/evaluate-model-collections-maven4/pom.xml
new file mode 100644
index 0000000..b6c3a81
--- /dev/null
+++ b/src/it/projects/evaluate-model-collections-maven4/pom.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.help</groupId>
+ <artifactId>evaluate-model-collections-maven4</artifactId>
+ <version>1.0</version>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ <targetPath>config</targetPath>
+ <filtering>true</filtering>
+ <includes>
+ <include>**/*.properties</include>
+ <include>**/*.xml</include>
+ </includes>
+ <excludes>
+ <exclude>**/private.properties</exclude>
+ </excludes>
+ </resource>
+ <resource>
+ <directory>src/extra/resources</directory>
+ </resource>
+ </resources>
+ </build>
+
+ <profiles>
+ <profile>
+ <id>it-profile</id>
+ <properties>
+ <profile.key>profile value</profile.key>
+ <another.key>another value</another.key>
+ </properties>
+ </profile>
+ </profiles>
+</project>
diff --git a/src/it/projects/evaluate-model-collections/invoker.properties
b/src/it/projects/evaluate-model-collections-maven4/test.properties
similarity index 87%
copy from src/it/projects/evaluate-model-collections/invoker.properties
copy to src/it/projects/evaluate-model-collections-maven4/test.properties
index 64862c9..7c13693 100644
--- a/src/it/projects/evaluate-model-collections/invoker.properties
+++ b/src/it/projects/evaluate-model-collections-maven4/test.properties
@@ -15,5 +15,5 @@
# specific language governing permissions and limitations
# under the License.
-invoker.goals =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-invoker.maven.version = 4-
+expression = project.build.resources
+output = resources.xml
diff --git a/src/it/projects/evaluate-model-collections-maven4/verify.groovy
b/src/it/projects/evaluate-model-collections-maven4/verify.groovy
new file mode 100644
index 0000000..b7f9909
--- /dev/null
+++ b/src/it/projects/evaluate-model-collections-maven4/verify.groovy
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+
+assert !new File(basedir, 'build.log').readLines().any {
it.startsWith('[ERROR]') }
+
+def parse = { name -> new groovy.xml.XmlSlurper().parse(new File(basedir,
name)) }
+def checkResource = { resource ->
+ assert resource.name() == 'resource'
+ assert resource.directory.text() == new File(basedir,
'src/main/resources').absolutePath
+ assert resource.targetPath.text() == 'config'
+ assert resource.filtering.text() == 'true'
+ assert resource.includes.children()*.text() == ['**/*.properties',
'**/*.xml']
+ assert resource.excludes.children()*.text() == ['**/private.properties']
+}
+def checkResources = { resources ->
+ assert resources.children().size() == 2
+ checkResource(resources.children()[0])
+ def extra = resources.children()[1]
+ assert extra.name() == 'resource'
+ assert extra.directory.text() == new File(basedir,
'src/extra/resources').absolutePath
+ assert extra.includes.children().isEmpty()
+ assert extra.excludes.children().isEmpty()
+}
+
+def resources = parse('resources.xml')
+assert resources.name() == 'resources'
+checkResources(resources)
+checkResource(parse('resource.xml'))
+def build = parse('build.xml')
+assert build.name() == 'build'
+checkResources(build.resources)
+assert parse('empty.xml').children().isEmpty()
+
+def plugins = parse('plugins.xml').entry.plugin
+assert plugins.size() == build.plugins.children().size()
+assert plugins.artifactId*.text().contains('maven-compiler-plugin')
+assert plugins.artifactId*.text().sort() ==
build.plugins.children().artifactId*.text().sort()
+
+def profiles = parse('profiles.xml')
+assert profiles.name() == 'profiles'
+assert profiles.profile.size() == 1
+assert profiles.profile.id.text() == 'it-profile'
+// String maps are written like Maven 3 Properties: one element per key, sorted
+assert profiles.profile.properties.children()*.name() == ['another.key',
'profile.key']
+assert profiles.profile.properties.'profile.key'.text() == 'profile value'
+
+['resources.xml', 'resource.xml', 'build.xml', 'plugins.xml',
'profiles.xml'].each { name ->
+ def nodes = parse(name).depthFirst().toList()
+ // no wrapper, source location or cache fields of the Maven 4 model
+ assert nodes.findAll {
+ it.name() in ['delegate', 'locations', 'importedFrom', 'pluginMap',
'reportSetMap', 'managementKey']
+ }.isEmpty()
+ // no implementation classes of the immutable collections and no XStream
back references
+ assert nodes.findAll { [email protected]() > 0 || [email protected]() > 0
}.isEmpty()
+}
+
+return true
diff --git a/src/it/projects/evaluate-model-collections/invoker.properties
b/src/it/projects/evaluate-model-collections/invoker.properties
index 64862c9..e9c555f 100644
--- a/src/it/projects/evaluate-model-collections/invoker.properties
+++ b/src/it/projects/evaluate-model-collections/invoker.properties
@@ -16,4 +16,3 @@
# under the License.
invoker.goals =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-invoker.maven.version = 4-
diff --git a/src/it/projects/evaluate-model-collections/verify.groovy
b/src/it/projects/evaluate-model-collections/verify.groovy
index cefa26b..f60ce19 100644
--- a/src/it/projects/evaluate-model-collections/verify.groovy
+++ b/src/it/projects/evaluate-model-collections/verify.groovy
@@ -20,4 +20,5 @@
def result = new File(basedir, 'build.log').text
assert result.contains('<resources>')
+assert result.contains('<resource>')
assert result.contains('<directory>' + new File(basedir,
'src/main/resources').absolutePath + '</directory>')
diff --git a/src/it/projects/evaluate-settings-servers/invoker.properties
b/src/it/projects/evaluate-settings-servers/invoker.properties
index a50d2e9..e9c555f 100644
--- a/src/it/projects/evaluate-settings-servers/invoker.properties
+++ b/src/it/projects/evaluate-settings-servers/invoker.properties
@@ -16,15 +16,3 @@
# under the License.
invoker.goals =
${project.groupId}:${project.artifactId}:${project.version}:evaluate
-
-invoker.maven.version = 4-
-
-# Error on Maven 4
-# Caused by: com.thoughtworks.xstream.converters.ConversionException: No
converter available
-# ---- Debugging information ----
-# message : No converter available
-# type : java.util.ImmutableCollections$MapN
-# converter :
com.thoughtworks.xstream.converters.reflection.SerializableConverter
-# message[1] : Unable to make private void
java.util.ImmutableCollections$MapN.readObject(java.io.ObjectInputStream)
throws java.io.IOException,java.lang.ClassNotFoundException accessible: module
java.base does not "opens java.util" to unnamed module @50bf795f
-# converter[1] :
com.thoughtworks.xstream.converters.reflection.ReflectionConverter
-# message[2] : Unable to make field transient java.util.Set
java.util.AbstractMap.keySet accessible: module java.base does not "opens
java.util" to unnamed module @50bf795f
diff --git a/src/it/projects/evaluate-settings-servers/verify.groovy
b/src/it/projects/evaluate-settings-servers/verify.groovy
index 5766b21..a76a4dc 100644
--- a/src/it/projects/evaluate-settings-servers/verify.groovy
+++ b/src/it/projects/evaluate-settings-servers/verify.groovy
@@ -17,10 +17,13 @@
* under the License.
*/
-def result = new File(basedir, 'result.txt').text;
+def result = new groovy.xml.XmlSlurper().parse(new File(basedir, 'result.txt'))
-assert result.contains( '<id>deep-clone</id>' )
-assert result.contains( '<password>server-password</password>' )
-assert result.contains( '<passphrase>server-passphrase</passphrase>' )
+assert result.name() == 'server'
+assert result.id.text() == 'deep-clone'
+assert result.password.text() == 'server-password'
+assert result.passphrase.text() == 'server-passphrase'
+assert result.depthFirst().findAll { it.name() == 'delegate' }.isEmpty()
-return true;
+assert result.depthFirst().findAll { it.name() in ['locations',
'importedFrom'] }.isEmpty()
+assert result.depthFirst().findAll { [email protected]() > 0 ||
[email protected]() > 0 }.isEmpty()
diff --git a/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
b/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
index f59d95c..abb3bcf 100644
--- a/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
@@ -37,8 +37,10 @@ import java.util.jar.JarInputStream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
+import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.converters.collections.PropertiesConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.mapper.MapperWrapper;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
@@ -365,13 +367,27 @@ public class EvaluateMojo extends AbstractHelpMojo {
*/
private XStream getXStream() {
if (xstream == null) {
- xstream = new XStream();
+ xstream = new XStream() {
+ @Override
+ protected MapperWrapper wrapMapper(MapperWrapper next) {
+ return MavenModelConverter.createMapper(next);
+ }
+ };
xstream.registerConverter(new
CollectionConverter(xstream.getMapper()) {
@Override
public boolean canConvert(Class type) {
return Collection.class.isAssignableFrom(type);
}
});
+ xstream.registerConverter(new MapConverter(xstream.getMapper()) {
+ @Override
+ public boolean canConvert(Class type) {
+ return Map.class.isAssignableFrom(type);
+ }
+ });
+ // Registered last so that the Maven 4 converters take precedence
over the generic ones.
+ MavenModelConverter.register(xstream, Model.class);
+ MavenModelConverter.register(xstream, Settings.class);
addAlias(xstream);
// handle Properties a la Maven
@@ -429,9 +445,9 @@ public class EvaluateMojo extends AbstractHelpMojo {
if
(jarEntry.getName().toLowerCase(Locale.ENGLISH).endsWith(".class")) {
String name =
jarEntry.getName().substring(0,
jarEntry.getName().indexOf("."));
- name = name.replace("/", "\\.");
+ name = name.replace('/', '.');
- if (name.contains(packageFilter) && !name.contains("$")) {
+ if (name.contains(packageFilter) && !name.contains("$") &&
!name.endsWith(".package-info")) {
try {
Class<?> clazz = Class.forName(name);
String alias =
StringUtils.lowercaseFirstLetter(clazz.getSimpleName());
diff --git
a/src/main/java/org/apache/maven/plugins/help/MavenModelConverter.java
b/src/main/java/org/apache/maven/plugins/help/MavenModelConverter.java
new file mode 100644
index 0000000..b05c163
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/help/MavenModelConverter.java
@@ -0,0 +1,185 @@
+/*
+ * 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 java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.converters.ConversionException;
+import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.collections.MapConverter;
+import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.mapper.Mapper;
+import com.thoughtworks.xstream.mapper.MapperWrapper;
+
+/** Serializes Maven 4 compatibility models without their wrapper or source
locations. */
+final class MavenModelConverter extends ReflectionConverter {
+ /** API model fields that hold source locations or lazily computed caches
rather than POM content. */
+ private static final Set<String> HIDDEN_FIELDS =
+ new HashSet<>(Arrays.asList("locations", "importedFrom",
"pluginMap", "reportSetMap", "managementKey"));
+
+ private final Class<?> wrapperType;
+ private final Method getDelegate;
+
+ private MavenModelConverter(XStream xstream, Class<?> wrapperType) throws
NoSuchMethodException {
+ super(xstream.getMapper(), xstream.getReflectionProvider());
+ this.wrapperType = wrapperType;
+ this.getDelegate = wrapperType.getMethod("getDelegate");
+ }
+
+ static void register(XStream xstream, Class<?> modelType) {
+ try {
+ Class<?> wrapperType =
+ Class.forName(modelType.getPackage().getName() +
".BaseObject", false, modelType.getClassLoader());
+ xstream.registerConverter(new MavenModelConverter(xstream,
wrapperType));
+ xstream.registerConverter(new
StringMapConverter(xstream.getMapper()));
+ } catch (ClassNotFoundException e) {
+ // Maven 3 models do not wrap API model delegates.
+ } catch (NoSuchMethodException e) {
+ throw new ConversionException("Cannot access Maven model
delegate", e);
+ }
+ }
+
+ static MapperWrapper createMapper(MapperWrapper next) {
+ return new MapperWrapper(next) {
+ @Override
+ public String serializedClass(Class type) {
+ if (isApiCollection(type)) {
+ // Name the immutable implementations like the default
ones, so that
+ // no class attribute is written for collection and map
fields.
+ return
super.serializedClass(defaultImplementationOf(collectionInterface(type)));
+ }
+ if (isApiModel(type)) {
+ try {
+ Class<?> wrapper = Class.forName(
+ type.getName().replace("maven.api.", "maven."),
+ false,
+ MavenModelConverter.class.getClassLoader());
+ if (wrapper.getMethod("getDelegate").getReturnType()
== type) {
+ // Preserve the compatibility class's existing
name or alias for nested delegates.
+ return super.serializedClass(wrapper);
+ }
+ } catch (ClassNotFoundException | NoSuchMethodException e)
{
+ // API-only types have no compatibility wrapper.
+ }
+ }
+ return super.serializedClass(type);
+ }
+
+ @Override
+ public boolean shouldSerializeMember(Class definedIn, String
fieldName) {
+ if (HIDDEN_FIELDS.contains(fieldName) &&
isApiModel(definedIn)) {
+ return false;
+ }
+ return super.shouldSerializeMember(definedIn, fieldName);
+ }
+
+ @Override
+ public boolean isImmutableValueType(Class type) {
+ // The API model shares one instance for every empty
collection; write each
+ // occurrence in place instead of a reference to the first one.
+ return isApiCollection(type) ||
super.isImmutableValueType(type);
+ }
+ };
+ }
+
+ private static boolean isApiCollection(Class<?> type) {
+ return isApiModel(type) && (Collection.class.isAssignableFrom(type) ||
Map.class.isAssignableFrom(type));
+ }
+
+ private static Class<?> collectionInterface(Class<?> type) {
+ if (List.class.isAssignableFrom(type)) {
+ return List.class;
+ } else if (Set.class.isAssignableFrom(type)) {
+ return Set.class;
+ } else if (Map.class.isAssignableFrom(type)) {
+ return Map.class;
+ }
+ return Collection.class;
+ }
+
+ private static boolean isApiModel(Class<?> type) {
+ Package modelPackage = type != null ? type.getPackage() : null;
+ return modelPackage != null
+ && ("org.apache.maven.api.model".equals(modelPackage.getName())
+ ||
"org.apache.maven.api.settings".equals(modelPackage.getName()));
+ }
+
+ @Override
+ public boolean canConvert(Class type) {
+ return wrapperType.isAssignableFrom(type) &&
wrapperType.getPackage().equals(type.getPackage());
+ }
+
+ @Override
+ public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
+ Object delegate;
+ try {
+ delegate = getDelegate.invoke(source);
+ } catch (IllegalAccessException e) {
+ throw new ConversionException("Cannot access Maven model
delegate", e);
+ } catch (InvocationTargetException e) {
+ throw new ConversionException("Cannot read Maven model delegate",
e.getCause());
+ }
+ if (delegate == null) {
+ throw new ConversionException("Maven model delegate is null");
+ }
+ // Write the delegate's fields into the existing wrapper element, also
for nested models.
+ super.marshal(delegate, writer, context);
+ }
+
+ /**
+ * Writes the API model's string maps the way Maven 3 {@code Properties}
fields are written:
+ * one element per key, sorted by key.
+ */
+ private static final class StringMapConverter extends MapConverter {
+ StringMapConverter(Mapper mapper) {
+ super(mapper);
+ }
+
+ @Override
+ public boolean canConvert(Class type) {
+ return isApiModel(type) && Map.class.isAssignableFrom(type);
+ }
+
+ @Override
+ public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
+ Map<?, ?> map = (Map<?, ?>) source;
+ boolean strings = map.entrySet().stream()
+ .allMatch(e -> e.getKey() instanceof String &&
e.getValue() instanceof String);
+ if (!strings) {
+ super.marshal(source, writer, context);
+ return;
+ }
+ for (Map.Entry<?, ?> entry : new TreeMap<>(map).entrySet()) {
+ writer.startNode(entry.getKey().toString());
+ writer.setValue(entry.getValue().toString());
+ writer.endNode();
+ }
+ }
+ }
+}
diff --git
a/src/test/java/org/apache/maven/plugins/help/MavenModelConverterTest.java
b/src/test/java/org/apache/maven/plugins/help/MavenModelConverterTest.java
new file mode 100644
index 0000000..07f1b4d
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugins/help/MavenModelConverterTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+import com.thoughtworks.xstream.mapper.MapperWrapper;
+import org.apache.maven.model.InputLocation;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Resource;
+import org.apache.maven.settings.Settings;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MavenModelConverterTest {
+ private XStream newXStream() {
+ XStream xstream = new XStream(new DomDriver()) {
+ @Override
+ protected MapperWrapper wrapMapper(MapperWrapper next) {
+ return MavenModelConverter.createMapper(next);
+ }
+ };
+ MavenModelConverter.register(xstream, Model.class);
+ MavenModelConverter.register(xstream, Settings.class);
+ return xstream;
+ }
+
+ @Test
+ void preservesNullCollectionElements() {
+ String xml = newXStream().toXML(new ArrayList<>(Arrays.asList(null,
"value")));
+ assertTrue(xml.contains("<null/>"), xml);
+ assertTrue(xml.contains("<string>value</string>"), xml);
+ }
+
+ @Test
+ void preservesOrdinaryMapEntries() {
+ Map<String, String> values = new LinkedHashMap<>();
+ values.put("delegate", "delegate value");
+ values.put("locations", "locations value");
+ values.put("importedFrom", "importedFrom value");
+ String xml = newXStream().toXML(values);
+ values.forEach((key, value) -> {
+ assertTrue(xml.contains("<string>" + key + "</string>"), xml);
+ assertTrue(xml.contains("<string>" + value + "</string>"), xml);
+ });
+ }
+
+ @Test
+ void preservesUnrelatedObjectsWithDelegateGetters() {
+ XStream xstream = newXStream();
+ xstream.alias("unrelated", Unrelated.class);
+ String xml = xstream.toXML(new Unrelated());
+ assertTrue(xml.contains("<delegate>delegate value</delegate>"), xml);
+ assertTrue(xml.contains("<locations>locations value</locations>"),
xml);
+ assertTrue(xml.contains("<importedFrom>importedFrom
value</importedFrom>"), xml);
+ }
+
+ @Test
+ void preservesMaven3ModelFieldsAndLocations() {
+ Resource resource = new Resource();
+ resource.setDirectory("src/main/resources");
+ resource.setLocation("directory", new InputLocation(12, 34));
+ XStream xstream = newXStream();
+ xstream.alias("resource", Resource.class);
+ String xml = xstream.toXML(resource);
+ assertTrue(xml.contains("<directory>src/main/resources</directory>"),
xml);
+ assertTrue(xml.contains("<lineNumber>12</lineNumber>"), xml);
+ assertTrue(xml.contains("<columnNumber>34</columnNumber>"), xml);
+ }
+
+ private static class Unrelated {
+ private final String delegate = "delegate value";
+ private final String locations = "locations value";
+ private final String importedFrom = "importedFrom value";
+
+ public String getDelegate() {
+ throw new AssertionError("Unrelated delegate getter must not be
invoked");
+ }
+ }
+}