Apache CXF -- Maven cxf-codegen-plugin (WSDL to Java) (original) (raw)

Introduction

CXF includes a Maven plugin which can generate java artifacts from WSDL. Here is a simple example:

org.apache.cxf cxf-codegen-plugin ${cxf.version} generate-sources generate-sources ${project.build.directory}/generated-sources/cxf ${basedir}/src/main/resources/myService.wsdl wsdl2java

In this example we're running the wsdl2java goal in the generate-sources phase. By running mvn generate-sources, CXF will generate artifacts in the directory that you specify. Each element corresponds to a WSDL that you're generating artifacts for. The WSDL location is specified via the option. Following Maven standard directory layout, if you're planning on packaging the WSDL in the JAR you're creating, you'll want the WSDL above in /src/main/resources/ (alternatively in a subfolder underneath it if desired to avoid placing resources in the root of a JAR); else use the /src/main/config folder to keep the WSDL out of the JAR.

The following example shows some customization options. By default, the codegen plugin follows the Maven convention of "target/generated-sources/cxf" for the output folder for the generated classes. You can override this value using as shown below, but note this is usually not necessary, the default is fine for most people and can make it easier for some IDE's to detect the generated source code. Other configuration arguments can be included inside the element. These pass arguments to the tooling and correspond to the options outlined on the WSDL to Java page.

... ${project.build.directory}/generated-code/mywebservice ${basedir}/src/main/resources/wsdl/myService.wsdl -impl -verbose ...

See this blog entry for a full service and client example that uses the cxf-codegen-plugin.

Example 1: Passing in a JAX-WS Binding file

${basedir}/src/main/resources/wsdl/myService.wsdl ${basedir}/src/main/resources/async_binding.xml

In this example we're specifying that we want CXF to use our JAX-WS binding file. Binding files are a way to customize the output of the artifacts that CXF generates. For instance, it allows you to change the package name CXF uses.

Example 2: Specify the data binding

${basedir}/src/main/resources/wsdl/myService.wsdl -databinding jibx

In this example we're specifying that we want CXF to use our data binding jibx. You can also using the data binding of xmlbeans, domsources, sdo etc.

Example 3: Specifying a service to generate artifacts for

${basedir}/src/main/resources/wsdl/myService.wsdl MyWSDLService

In this example we're specifying that we only want to generate artifacts for the service named "MyWSDLService" in the WSDL.

To avoid copy/paste in multiple you can also declare a element.

Example 4: Using defaultOption to avoid repetition

${basedir}/src/main/jaxb/bindings.xml true ${basedir}/src/main/resources/wsdl/myService.wsdl MyWSDLService ${basedir}/src/main/resources/wsdl/myOtherService.wsdl MyOtherWSDLService

and correspond to the options outlined on the WSDL to Java page, you may look at https://github.com/apache/cxf/blob/master/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/wsdl2java/Option.java for a more detailed description of those parameters.

At least, you can declare a common wsdlRoot folder where you store your WSDL files and use includes/excludes patterns to select the files to get used by the code generator

Example 5: Using wsdlRoot with includes/excludes patterns

${basedir}/src/main/jaxb/bindings.xml true ${basedir}/src/main/resources/wsdl *Service.wsdl

wsdlRoot default value is src/main/resources/wsdl so you may omit this declaration.

Example 6: Loading a wsdl from the maven repository

There is a wsdlOption configuration which can be used to load a wsdl file from the maven repository.

org.apache.pizza PizzaService 1.0.0

This will load the wsdl /org/apache/pizza/PizzaService-1.0.0.wsdl into your local maven repository and generate java code from it.

Example 7: Using xjc extensions

Standard JAXB command-line customizations can be added via elements, either one per line or comma separated. CXF also offers some JAXB extensions for the code generation. They have to be added as dependencies and then activated by using an extraarg with content -xjc-X

artifact id description extension id
cxf-xjc-boolean Adds getters for booleans boolean
cxf-xjc-bug671 Workaroung for JAXB bug 671 bug671
cxf-xjc-dv Default value support dv
cxf-xjc-ts Adds toString to objects ts
cxf-xjc-wsdlextension WsdlExtension support wsdlextension
jaxb-fluent-api((warning) not part of CXF:group id is net.java.dev.jaxb2-commons) Fluent API for setters fluent-api

An example showing attachment of a JAXB binding file and the CXF toString() extension is below:

org.apache.cxf cxf-codegen-plugin ${cxf.version} generate-sources generate-sources ${basedir}/src/main/resources/wsdl/myService.wsdl -xjc-Xts wsdl2java org.apache.cxf.xjcplugins cxf-xjc-ts ${cxf-xjc.version}

In addition you need to add the cxf-xjc-runtime as a dependency to your project:

org.apache.cxf.xjc-utils cxf-xjc-runtime ${cxf-xjc.version}

Example 8 - Using JAXB/JAX-WS 2.2 with Java 6

Java 6 includes JAXB/JAX-WS 2.1 API's and a 2.1 implementations. However, sometimes it's desirable to use JAXB or JAX-WS 2.2 instead to obtain various bug fixes and enhancements. Using 2.2 with Java 6 and Maven can be a bit tricky as it requires endorsing the API jars which requires configuration of a bunch of plugins, requires use of "forking", etc... First off, both Surefire and the Compiler plugins need to be setup to point at an endorsed dir:

org.apache.maven.plugins maven-compiler-plugin ${project.build.directory}/endorsed org.apache.maven.plugins maven-surefire-plugin once -Djava.endorsed.dirs=${project.build.directory}/endorsed

You will then need to use the maven-dependency-plugin to copy the needed artifacts into the endorsed.dir:

org.apache.maven.plugins maven-dependency-plugin generate-sources copy javax.xml.bind jaxb-api 2.2 javax.xml.ws jaxws-api 2.2 ${project.build.directory}/endorsed

Finally, you need to do similar setup for the CXF Codegen plugin so it picks up the 2.2 API's and runtimes:

org.apache.cxf cxf-codegen-plugin ${cxf.version} once -Djava.endorsed.dirs=${project.build.directory}/endorsed com.sun.xml.bind jaxb-impl 2.2.11 com.sun.xml.bind jaxb-xjc 2.2.11

Reading external DTDs

More recent versions of XML Schema will throw an exception by default if the schema has an external DTD. For example:

[Fatal Error] xmldsig-core-schema.xsd:10:5: External DTD: Failed to read external DTD 'XMLSchema.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property.

If you wish to allow external DTDs then it's possible to do this via the the ' configuration switch as follows:

org.apache.cxf cxf-codegen-plugin ${cxf.version} generate-sources once ${basedir}/src/main/resources/org/apache/cxf/wsn/wsdl/wsn.wsdl -verbose -Djavax.xml.accessExternalDTD=all wsdl2java

Other configuration options

The cxf-codegen-plugin has some additional configuration options that may be useful:

false/always/once Forks a separate JVM for the code generation
.... Additional JVM args set on the forked process if fork is not false
UTF-8 (new in 2.6.1, requires configuring plugin to use very latest JAXB 2.2.11 impl jars)