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.
...
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> ... |
...
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 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.
...
There is a <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> <groupId>org.apache <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.
...
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-Xts</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.apache.cxf.xjcplugins</groupId> <artifactId>cxf-xjc-ts</artifactId> <version>${cxf-xjc.version}</version> </dependency> </dependencies> </plugin> |
...
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>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<fork>once</fork>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/org/apache/cxf/wsn/wsdl/wsn.wsdl</wsdl>
<extraargs>
<extraarg>-verbose</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
<additionalJvmArgs>-Djavax.xml.accessExternalDTD=all</additionalJvmArgs>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin> |
Other configuration options
...