Table of Contents |
---|
Introduction
CXF includes a Maven plugin which can generate java artifacts from WSDL. Here is a simple example:
Code Block | ||
---|---|---|
| ||
<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-sources/cxf</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/resources/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 generating artifacts for. The WSDL location is specified via the <wsdl> 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 <sourceRoot> 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 <wsdlOption> element. These pass arguments to the tooling and correspond to the options outlined on the WSDL To to Java page.
Code Block | ||
---|---|---|
| ||
... <configuration> <sourceRoot>${project.build.directory}/generated-code/mywebservice</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl> <!-- you can set the options of wsdl2java command by using the <extraargs> --> <extraargs> <extraarg>-impl</extraarg> <extraarg>-verbose</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> ... |
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
Code Block | ||
---|---|---|
| ||
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/async_binding.xml</bindingFile>
</bindingFiles>
</wsdlOption>
</wsdlOptions>
</configuration>
|
...
Example 2: Specify the data binding
Code Block | ||
---|---|---|
| ||
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<extraargs>
<extraarg>-databinding</extraarg>
<extraarg>jibx</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
|
...
Example 3: Specifying a service to generate artifacts for
Code Block | ||
---|---|---|
| ||
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<serviceName>MyWSDLService</serviceName>
</wsdlOption>
</wsdlOptions>
</configuration>
|
...
To avoid copy/paste in multiple <wsdlOption> you can also declare a <defaultOption> <defaultOptions> element.
Example 4: Using defaultOption to avoid repetition
Code Block | ||
---|---|---|
| ||
<configuration>
<defaultOptions>
<bindingFiles>
<bindingFile>${basedir}/src/main/jaxb/bindings.xml</bindingFile>
</bindingFiles>
<noAddressBinding>true</noAddressBinding>
</defaultOptions>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
<serviceName>MyWSDLService</serviceName>
</wsdlOption>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/myOtherService.wsdl</wsdl>
<serviceName>MyOtherWSDLService</serviceName>
</wsdlOption>
</wsdlOptions>
</configuration>
|
<defaultOption> <defaultOptions> and <wsdlOption> correspond to the options outlined on the WSDL To to Java page, you may look at httpat https://svngithub.com/apache.org/reposcxf/asfblob/cxf/trunkmaster/maven-plugins/codegen-plugin/src/main/java/org/apache/cxf/maven_plugin/wsdl2java/Option.java for a more detailed description of those parameters.
...
Example 5: Using wsdlRoot with includes/excludes patterns
Code Block | ||
---|---|---|
| ||
<configuration>
<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>
|
...
Example 6: Loading a wsdl from the maven repository
For CXF 2.3 and latter there There is a new config element <wsdlArtifact> wsdlOption configuration which can be used to load a wsdl file from the maven repository.
Code Block | ||
---|---|---|
| ||
<configuration> <configuration><wsdlOptions> <wsdlOptions> <wsdlOption> <wsdlOption><wsdlArtifact> <wsdlArtifact> <groupId>org.apache.pizza</groupId> <artifactId>PizzaService< <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.
...
Standard JAXB command-line customizations can be added via <extraarg> 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<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 |
jaxb-fluent-api ( 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:
Code Block | ||
---|---|---|
| ||
<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> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl> <extraargs> <extraarg>-xjc-b,binding.xjb<Xts</extraarg> <extraarg>-xjc-Xts</extraarg></extraargs> </extraargs> wsdlOption> </wsdlOption>wsdlOptions> </wsdlOptions>configuration> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.apache.cxf<cxf.xjcplugins</groupId> <artifactId>cxf-xjc-ts</artifactId> <version>${cxf-xjc.version}</version> </dependency> </dependencies> </plugin> |
Example 8 - Using JAXB/JAX-WS 2.2 with Java 6
In addition you need to add the cxf-xjc-runtime as a dependency to your project:
Code Block | ||
---|---|---|
| ||
<dependency>
<groupId>org.apache.cxf.xjc-utils</groupId>
<artifactId>cxf-xjc-runtime</artifactId>
<version>${cxf-xjc.version}</version>
</dependency> |
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 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:
Code Block | ||
---|---|---|
| ||
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<endorseddirs>${project.build.directory}/endorsed</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<argLine>-Djava.endorsed.dirs=${project.build.directory}/endorsed</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
|
You will then need to use the maven-dependency-plugin to copy the needed artifacts into the endorsed.dir:
Code Block | ||
---|---|---|
| ||
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2</version> </artifactItem> <artifactItem> <groupId>javax.xml.ws</groupId> <artifactId>jaxws-api</artifactId> <version>2.2</version> </artifactItem> </artifactItems> <outputDirectory>${project.build.directory}/endorsed</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> |
Finally, you need to do similar setup for the CXF Codegen plugin so it picks up the 2.2 API's and runtimes:
Code Block | ||
---|---|---|
| ||
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<configuration>
<fork>once</fork>
<additionalJvmArgs>-Djava.endorsed.dirs=${project.build.directory}/endorsed</additionalJvmArgs>
<!-- rest of the normal codegen configuration options -->
</configuration>
<dependencies>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2.11</version>
</dependency>
</dependencies>
</plugin>
|
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 <additionalJvmArgs>' configuration switch as follows:
Code Block |
---|
<plugin> <groupId>org.apache.cxf</groupId> <artifactItem><artifactId>cxf-codegen-plugin</artifactId> <groupId>javax.xml.ws</groupId> <version>${cxf.version}</version> <executions> <execution> <artifactId>jaxws<phase>generate-api<sources</artifactId>phase> <configuration> <version>2.2</version> <fork>once</fork> </artifactItem><wsdlOptions> </artifactItems> <wsdlOption> <outputDirectory>${project.build.directory}/endorsed</outputDirectory> </configuration> <wsdl>${basedir}/src/main/resources/org/apache/cxf/wsn/wsdl/wsn.wsdl</wsdl> </execution> </executions> </plugin> </plugins> |
Finally, you need to do similar setup for the CXF Codegen plugin so it picks up the 2.2 API's and runtimes:
Code Block | ||
---|---|---|
xml | xml | <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.version}</version> <configuration> <extraargs> <extraarg>-verbose</extraarg> <fork>once</fork> <additionalJvmArgs>-Djava.endorsed.dirs=${project.build.directory}/endorsed</additionalJvmArgs> <!-- rest of the normal codegen configuration options --> /extraargs> </configuraion> <dependencies> </wsdlOption> <dependency> <groupId>com.sun.xml.bind</groupId> </wsdlOptions> <artifactId>jaxb-impl</artifactId> <additionalJvmArgs>-Djavax.xml.accessExternalDTD=all</additionalJvmArgs> <version>2.2</version> </dependency>configuration> <dependency> <goals> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc<<goal>wsdl2java</artifactId>goal> <version>2.2<</version>goals> </dependency>execution> </dependencies>executions> </plugin> |
Other configuration options
...
<fork>false/always/once</fork> | Forks a separate JVM for the code generation |
<additionalJvmArgs>.... | Additional JVM args set on the forked process if fork is not false |
<encoding>UTF-8</encoding> | (new in 2.6.1, requires configuring plugin to use very latest JAXB 2.2.11 impl jars) |