I'm writing a custom Maven plugin for building a Flatpak from a Java project. In a Maven mojo I understand we use @Parameter annotations to parse pom.xml file configurations. For example:
pom.xml:
<configuration>
<param1>VALUE</param1>
</configuration>
In MyMojo.java:
@Parameter
private String param1;
But how would I set a value that includes HTML tags? For example:
<configuration>
<param1><p>VALUE</p></param1>
</configuration>
Trying this, I get the error:
Basic element 'param1' must not contain child elements.
So I wrap the <p>VALUE</p> in a CDATA block:
<configuration>
<param1><![CDATA[<p>VALUE</p>]]></param1>
</configuration>
This works up to the point where the MetaInfo object is written to file:
private void writeMetaInfo(MetaInfo metaInfo, Writer writer) throws IOException {
XmlMapper mapper = new XmlMapper();
new PrintWriter(writer, true).println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
// metaInfo value is correct before writing - it has <p> not <p>
mapper.writerWithDefaultPrettyPrinter().writeValue(writer, metaInfo);
}
The result is an XML file with:
<param1><p>VALUE</p></param1>
...with the leading angle brackets replaced with their entity, <. This doesn't work for the Flatpak MetaInfo file I'm building. It needs the <p> and </p>.
So it seems to be a problem with the XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper from com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.20.1).
I found this question which seems to indicate just using the CDATA block should work.
Any ideas?
-X... should be something about configurations and value passed to your params[DEBUG] (s) description = <p>Initial release</p> [DEBUG] (f) releases = [flatpak.maven.plugin.models.Release@799fb45e][DEBUG] (f) releases = [flatpak.maven.plugin.models.Release@799fb45e]. It's a complex object, aList<Release>each of which has aStringfield calleddescription. The output shows unencoded p-tags. I've debugged to the point where the XmlMapper writes and , before the write, thereleaseinstance has a description without the encoding.XmlMapperclass? That said, what you see is the correct behaviour for writing a value with<into XML. If you want something else, then you will need to do more work, so it actually writes an XML fragment instead of a value (how or if you can do so depends on the actual XML library).com.fasterxml.jackson.dataformat.xml.XmlMapperfromcom.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.20.1. But I can try another solution if it helps. Can you point me at some code that writes an XML fragment when it's the value?