Red Hat
Apr 3, 2012
by Shane K Johnson

Generating a web service (JAX-WS) client with Maven is a two step process. The first step is generate a WSDL document from the JAX-WS annotated class. The next step is to generate the Java client from the generated WSDL document.

The following Maven plugins are required:

  • cxf-java2ws-plugin (JAX-WS to WSDL)
  • cxf-codegen-plugin (WSDL to Java)
JAX-WS to WSDL

The first step is to generate the WSDL document from the JAX-WS annotated class by configuring cxf-java2ws-plugin with the ‘java2ws’ goal.

Add the cxf-rt-frontend-jaxws dependency and project dependencies required for the JAX-WS annotated class as plugin dependencies, the name of the JAX-WS annotated class, and configure the plugin to generate a WSDL.

The generated WSDL will be placed in the target/generated/wsdl directory.

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-java2ws-plugin</artifactId>
   <version>2.5.1</version>
   <dependencies>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>2.5.1</version>
      </dependency>
      <dependency>
         <groupId>com.company.app</groupId>
         <artifactId>services</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <className>com.company.app.services.WebServiceBean</className>
            <genWsdl>true</genWsdl>
         </configuration>
         <goals>
            <goal>java2ws</goal>
         </goals>
      </execution>
   </executions>
</plugin>
WSDL to Java

The next step is to generate the Java client from the generated WSDL document by configuring cxf-codegen-plugin with the ‘wsdl2java’ goal.

The ‘-p’ argument specifies the package for the generated classes. The reason for configuring this argument is to generate a unique package for the client model and ensure that there is no conflict between the client and service models.

The generated classes will be placed in the target/generated-sources/cxf folder.

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>2.5.1</version>
   <executions>
      <execution>
         <id>process-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>${project.build.directory}/generated/wsdl/WebServiceBean.wsdl</wsdl>
                  <extraargs>
                     <extraarg>-p</extraarg>
                     <extraarg>com.company.app.services.client.model</extraarg>
                  </extraargs>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2java</goal>
         </goals>
      </execution>
   </executions>
</plugin>