CXF includes a Maven plugin which can generate java artifacts from WSDL. Here is a simple example:
<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.version}</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin>
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 <sourceRoot> directory that you specify. Each <wsdlOption> element corresponds to a WSDL that you're generated artifacts for. In the above example we're generating we're specifying the WSDL location via the <wsdl> option.
Other configuration arguments can be include inside the <wsdlOption> element. These pass arguments to the tooling and correspond to the options outlined on the WSDL To Java page.
For CXF 2.1.4 and latter you don't need anymore to specify the <phase>, as generate-sources is the default.
For CXF 2.2 and latter you don't even need to specify the <sourceRoot> to match maven convention for using target/generated-sources/cxf as output folder for generated classes.
Example 1: Passing in a JAX-WS Binding file
<configuration> <sourceRoot>${basedir}/target/generated/cxf</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl> <bindingFiles> <bindingFile>${basedir}/src/main/resources/wsdl/async_binding.xml</bindingFile> </bindingFiles> </wsdlOption> </wsdlOptions> </configuration>
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: Specifying a service to generate artifacts for
<configuration> <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl> <serviceName>MyWSDLService</serviceName> </wsdlOption> </wsdlOptions> </configuration>
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 <wsdlOption> you can also declare a <defaultOption> element.
Example 3: Using defaultOption to avoid repetition
<configuration> <sourceRoot>${basedir}/target/generated/cxf</sourceRoot> <defaultOptions> <bindingFiles> <bindingFile>${basedir}/src/main/jaxb/bindings.xml</bindingFile> </bindingFiles> <noAddressBinding>true</noAddressBinding> </defaultOptions> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl> <serviceName>MyWSDLService</serviceName> </wsdlOption> <wsdlOption> <wsdl>${basedir}/src/main/wsdl/myOtherService.wsdl</wsdl> <serviceName>MyOtherWSDLService</serviceName> </wsdlOption> </wsdlOptions> </configuration>
<defaultOption> and <wsdlOption> correspond to the options outlined on the WSDL To Java page, you may look at http://svn.apache.org/repos/asf/cxf/trunk/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/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 4: Using wsdlRoot with includes/excludes patterns
<configuration> <sourceRoot>${basedir}/target/generated/cxf</sourceRoot> <defaultOptions> <bindingFiles> <bindingFile>${basedir}/src/main/jaxb/bindings.xml</bindingFile> </bindingFiles> <noAddressBinding>true</noAddressBinding> </defaultOptions> <wsdlRoot>${basedir}/src/main/resources/wsdl</wsdlRoot> <includes> <include>*Service.wsdl</include> </includes> </configuration>
wsdlRoot default value is src/main/resources/wsdl
so you may omit this declaration.
Example 5: Loading a wsdl from the maven repository
For CXF 2.3 and latter there is a new config element <wsdlArtifact> which can be used to load a wsdl file from the maven repository.
<configuration> <wsdlOptions> <wsdlOption> <wsdlArtifact> <groupId>org.apache.pizza</groupId> <artifactId>PizzaService</artifactId> <version>1.0.0</version> </wsdlArtifact> </wsdlOption> </wsdlOptions> </configuration>
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 6: Using xjc extensions
CXF offers some extensions for the code generation. They have to be added as depdendencies and then activated by using an extraarg with content -xjc-X<extension id>
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 |
<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.version}</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/test/resources/org/apache/camel/converter/jaxb/soap/CustomerService.wsdl</wsdl> <extraargs> <extraarg>-xjc-Xts</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-xjc-ts</artifactId> <version>${cxf.version}</version> </dependency> </dependencies> </plugin>