I want to generate java classes with the jaxb2-maven-plugin. I am using the following configuration:
pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>SomeID</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<extension>true</extension>
<clearOutputDir>true</clearOutputDir>
<sources>
<source>src/main/xsd/schema.xsd</source>
</sources>
<noGeneratedHeaderComments>true</noGeneratedHeaderComments>
</configuration>
</execution>
</executions>
</plugin>
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://my.target.namespace/uri"
xmlns="http://my.target.namespace/uri"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:h="http://my.uri.for.prefix.h"
xmlns:f="http://my.target.namespace/uri">
<xsd:import namespace="http://my.uri.for.prefix.h" schemaLocation="schema2.xsd"/>
<xsd:complexType name="FooType">
<xsd:sequence>
<xsd:element ref="h:something" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="FooType" type="FooType" />
</xsd:schema>
The Jaxb2 plugin is generating me the following package-info.java:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;
But, what I want to get is this:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", xmlns = {
@XmlNs(prefix="f", namespaceURI="http://my.target.namespace/uri"),
@XmlNs(prefix="h", namespaceURI="http://my.uri.for.prefix.h")
}, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;
import javax.xml.bind.annotation.XmlNs;
The prefixes are missing in my generated file. How to do that? I tried already to create a binding file but this didn't worked how I expected.
Please see this answer on how to solve this problem: https://stackoverflow.com/a/10812236/1389219
The answer is very well written and easy to follow. Basically you will have to:
jaxb2-maven-plugin
in favour of maven-jaxb2-plugin
.jaxb2-namespace-prefix
dependency and provide the <arg>-Xnamespace-prefix</arg>
.bindings.xml
file which is only a few lines long.Your POM file will become more verbose, but it is worth it to have a package-info.java
generated the way you require.
As a bonus, there are a heap of additional plugins and dependencies related to maven-jaxb2-plugin
that provide extra features. One that I found helpful was jaxb2-rich-contract-plugin
that gave the ability to generate builders and make the generated classes immutable*.
* Well, not strictly speaking immutable (as it just changes the setter methods to be package private), but enough to make them feel safer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With