The CXF XJC Maven Plugin
The CXF XJC Maven Plugin is a wrapper around the JAXB XJC tool to handle the XSD -> Java tasks. It is part of Apache CXF XJC Utils, which also provides a set of XJC extensions (see below).
Versions
CXF XJC Utils is released independently of Apache CXF, and its version numbers do not follow CXF releases. Use the latest release in the same major line as your CXF version:
| CXF version | XJC Utils line | XML Binding API |
|---|
| 4.x | 4.x | Jakarta (jakarta.xml.bind) |
| 3.x | 3.x | javax.xml.bind |
The current release is 4.2.0. All versions are listed on Maven Central.
The plugin and the extensions use different groupIds:
| Artifact | Coordinates |
|---|
| Maven plugin | org.apache.cxf:cxf-xjc-plugin |
| XJC extensions | org.apache.cxf.xjcplugins:cxf-xjc-* |
| Runtime support | org.apache.cxf.xjc-utils:cxf-xjc-runtime |
Goals
| Goal | Description |
|---|
xsdtojava | Defaults into the generate-sources phase and outputs to ${project.build.directory}/generated/src/main/java |
xsdtojava-tests | Defaults into the generate-test-sources phase and outputs to ${project.build.directory}/generated/src/test/java |
Parameters
| Parameter | Description |
|---|
extensions | List of Maven artifacts to add to the extension classpath. Each item in the list is a string of groupId:artifactId:version. |
xsdOptions | List of XsdOption objects. See below. |
sourceRoot / testSourceRoot | For the two goals above, override the default output directory. |
encoding | Encoding of the generated source files, passed to XJC as -encoding. Defaults to ${project.build.sourceEncoding}. |
fork | (boolean) Run XJC in a separate process. Defaults to false. |
javaExecutable | The Java executable to use when fork is true. Defaults to ${java.home}/bin/java. |
additionalJvmArgs | JVM arguments (e.g. -Xmx512m) used when fork is true. Property: cxf.xjc.jvmArgs. |
markerDirectory | Directory where the plugin stores the marker files it uses to decide whether code needs regenerating. Defaults to ${project.build.directory}/cxf-xsd-plugin-markers. Property: cxf.markerDirectory. |
XsdOption
The XsdOption object is used to pass specific options and configuration for each XSD file that is to be processed.
| Option | Description |
|---|
xsd | The location of the schema to process. |
xsdDir | A directory of schemas to process. Every *.xsd file in it is processed. If set, it is used instead of xsd. |
bindingFile | The location of the JAXB binding file to customize the output. |
bindingFiles | A list of bindingFile entries, for when more than one binding file is needed (since 3.2.3). |
packagename | Specifies the package name to use for the generated code. |
extension | (boolean) Passes -extension to XJC, which enables vendor extensions and binding customizations that go beyond the JAXB specification. |
catalog | The location of a catalog file for mapping schema locations. |
extensionArgs | List of additional arguments passed to XJC, e.g. -Xlocator, or -Xdv to activate one of the extensions below. |
dependencies | List of files that are examined to determine if subsequent runs of the plugin must re-generate code. |
deleteDirs | List of directories that are removed after generation. If you have schemas that generate code that has already been generated, possibly in a different Maven module, you can use this to have it removed. The preferred method, however, is a binding file with the "skip" flag set on those namespaces. |
XJC Extensions
To use an extension, add its coordinates to extensions and its switch to extensionArgs. All extensions are in the org.apache.cxf.xjcplugins group.
| Artifact | Switch | Description |
|---|
cxf-xjc-dv | -Xdv, -Xdv:optional | Initializes fields mapped from elements with their schema default values |
cxf-xjc-ts | -Xts | Adds a toString() method to generated classes (requires cxf-xjc-runtime at runtime). Note that this prints every field, including sensitive data such as passwords or tokens. |
cxf-xjc-boolean | -Xbg, -Xbgi | Generates getXXX() instead of isXXX() for Boolean properties, or both |
cxf-xjc-javadoc | -Xjavadoc | Generates Javadoc from xsd:documentation |
cxf-xjc-pl | -Xproperty-listener | Adds a PropertyChangeListener to all set methods |
cxf-xjc-wsdlextension | -Xwsdlextension | Adds WSDL extension methods to generated root classes |
cxf-xjc-bug671 | -Xbug671 | Maps package names that contain Java keywords |
cxf-xjc-bug986 | -Xbug986 | Removes @XmlSchemaType(anySimpleType) from fields that should not have it |
The same extensions can be used with the cxf-codegen-plugin (WSDL to Java). Add the artifact as a plugin dependency and pass the switch prefixed with -xjc, e.g. -xjc-Xdv.
Example
<properties>
<cxf-xjc-utils.version>4.2.0</cxf-xjc-utils.version>
</properties>
...
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>${cxf-xjc-utils.version}</version>
<configuration>
<extensions>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:${cxf-xjc-utils.version}</extension>
</extensions>
</configuration>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>xsdtojava</goal>
</goals>
<configuration>
<sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
<xsdOptions>
<xsdOption>
<xsd>classpath:/schemas/wsdl/http.xsd</xsd>
<bindingFile>classpath:/schemas/wsdl/http.xjb</bindingFile>
<catalog>${basedir}/src/main/build-resources/catalog.cat</catalog>
</xsdOption>
<xsdOption>
<xsd>classpath:/schemas/wsdl/ws-addr.xsd</xsd>
<packagename>org.apache.cxf.ws.addressing</packagename>
</xsdOption>
<xsdOption>
<xsd>classpath:/schemas/wsdl/addressing.xsd</xsd>
<bindingFile>classpath:/schemas/wsdl/addressing.xjb</bindingFile>
</xsdOption>
<xsdOption>
<xsd>classpath:/schemas/configuration/security.xsd</xsd>
<bindingFiles>
<bindingFile>classpath:/schemas/configuration/security.xjb</bindingFile>
<bindingFile>classpath:/schemas/configuration/security2.xjb</bindingFile>
</bindingFiles>
<catalog>${basedir}/src/main/build-resources/catalog.cat</catalog>
<extension>true</extension>
<extensionArgs>
<extensionArg>-Xdv</extensionArg>
</extensionArgs>
</xsdOption>
<xsdOption>
<xsd>classpath:/schemas/wsdl/ws-addr-wsdl.xsd</xsd>
<bindingFile>classpath:/schemas/wsdl/ws-addr-wsdl.xjb</bindingFile>
<extension>true</extension>
</xsdOption>
<xsdOption>
<xsd>classpath:/schemas/wsdl/addressing200403.xsd</xsd>
<bindingFile>classpath:/schemas/wsdl/addressing200403.xjb</bindingFile>
</xsdOption>
</xsdOptions>
</configuration>
</execution>
</executions>
</plugin>