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 versionXJC Utils lineXML Binding API
4.x4.xJakarta (jakarta.xml.bind)
3.x3.xjavax.xml.bind

The current release is 4.2.0. All versions are listed on Maven Central.

The plugin and the extensions use different groupIds:

ArtifactCoordinates
Maven pluginorg.apache.cxf:cxf-xjc-plugin
XJC extensionsorg.apache.cxf.xjcplugins:cxf-xjc-*
Runtime supportorg.apache.cxf.xjc-utils:cxf-xjc-runtime

Goals

GoalDescription
xsdtojavaDefaults into the generate-sources phase and outputs to ${project.build.directory}/generated/src/main/java
xsdtojava-testsDefaults into the generate-test-sources phase and outputs to ${project.build.directory}/generated/src/test/java

Parameters

ParameterDescription
extensionsList of Maven artifacts to add to the extension classpath. Each item in the list is a string of groupId:artifactId:version.
xsdOptionsList of XsdOption objects. See below.
sourceRoot / testSourceRootFor the two goals above, override the default output directory.
encodingEncoding 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.
javaExecutableThe Java executable to use when fork is true. Defaults to ${java.home}/bin/java.
additionalJvmArgsJVM arguments (e.g. -Xmx512m) used when fork is true. Property: cxf.xjc.jvmArgs.
markerDirectoryDirectory 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.

OptionDescription
xsdThe location of the schema to process.
xsdDirA directory of schemas to process. Every *.xsd file in it is processed. If set, it is used instead of xsd.
bindingFileThe location of the JAXB binding file to customize the output.
bindingFilesA list of bindingFile entries, for when more than one binding file is needed (since 3.2.3).
packagenameSpecifies 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.
catalogThe location of a catalog file for mapping schema locations.
extensionArgsList of additional arguments passed to XJC, e.g. -Xlocator, or -Xdv to activate one of the extensions below.
dependenciesList of files that are examined to determine if subsequent runs of the plugin must re-generate code.
deleteDirsList 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.

ArtifactSwitchDescription
cxf-xjc-dv-Xdv, -Xdv:optionalInitializes fields mapped from elements with their schema default values
cxf-xjc-ts-XtsAdds 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, -XbgiGenerates getXXX() instead of isXXX() for Boolean properties, or both
cxf-xjc-javadoc-XjavadocGenerates Javadoc from xsd:documentation
cxf-xjc-pl-Xproperty-listenerAdds a PropertyChangeListener to all set methods
cxf-xjc-wsdlextension-XwsdlextensionAdds WSDL extension methods to generated root classes
cxf-xjc-bug671-Xbug671Maps package names that contain Java keywords
cxf-xjc-bug986-Xbug986Removes @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>