You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 21 Next »

Maven POM Information

To use CXF within Maven, you'll need to declare the CXF dependencies in your POM. The CXF groupId is "org.apache.cxf". Here is a small example:


<properties>
  <cxf.version>2.1.4</cxf.version>
<properties>

<dependencies>
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-frontend-jaxws</artifactId>
		<version>${cxf.version}</version>
	</dependency>
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-transports-http</artifactId>
		<version>${cxf.version}</version>
	</dependency>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-transports-http-jetty</artifactId>
		<version>${cxf.version}</version>
	</dependency>
</dependencies>

For 2.0.6 and later versions, you don't need to specify any repositories as the artifacts are available in the central repository. For versions prior to 2.0.6, you'll also need to add the Apache Incubator Maven repository:

<repositories>
	<repository>
		<id>apache-snapshots</id>
		<name>Apache SNAPSHOT Repository</name>
		<url>http://people.apache.org/repo/m2-snapshot-repository/</url>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</repository>
	<repository>
		<id>apache-incubating</id>
		<name>Apache Incubating Repository</name>
		<url>http://people.apache.org/repo/m2-incubating-repository/</url>
	</repository>
        <!-- for jaxb-impl -->
        <repository>
            <id>java.net</id>
            <url>http://download.java.net/maven/1/</url>
            <layout>legacy</layout>
        </repository>
</repositories>

<pluginRepositories>
	<pluginRepository>
		<id>apache-plugin-snapshots</id>
		<name>Apache Maven Plugin Snapshots</name>
		<url>http://people.apache.org/repo/m2-snapshot-repository</url>
		<releases>
			<enabled>false</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</pluginRepository>
        <pluginRepository>
               <id>apache-plugin-incubating</id>
               <name>Apache Plugin Incubating Repository</name>
               <url>http://people.apache.org/repo/m2-incubating-repository/</url>
      	</pluginRepository>
</pluginRepositories>

For information on using Maven with CXF and Tomcat, this blog entry may be helpful.

Maven Plugin

WSDL2Java

CXF includes a Maven plugin which can generate 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>
  <wsdlOptions>
      <defaultOptions>
          <bindingFiles>
              <bindingFile>${basedir}/src/main/jaxb/bindings.xml</bindingFile>
          </bindingFiles>
          <noAddressBinding>true</noAddressBinding>
      </defaultOptions>
      <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 detailled description of thoses 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>
  <wsdlOptions>
      <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>
  </wsdlOptions>
</configuration>

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

Java2WSDL (CXF 2.0.x only. Removed in 2.1 and replaced with Java2WS.)

CXF also includes a Maven plugin which can generate WSDL from Java code. Here is a simple example:

<plugin>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-codegen-plugin</artifactId>
  <version>2.0.9</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>2.0.9</version>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <id>generate-wsdl</id>
      <phase>process-classes</phase>
      <configuration>
        <className>org.example.MyService</className>
      </configuration>
      <goals>
        <goal>java2wsdl</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Here are the options you can use:

<configuration>
   <className>...</className>
   <classpath>...</classpath>
   <outputFile>...</outputFile>
   <serviceName>...</serviceName>
   <soap12>...</soap12>
   <targetNameSpace>...</targetNameSpace>
   <verbose>...</verbose>
   <quiet>...</quiet>
</configuration>

Java2WS

This plugin can generate WSDL, server side code used to start web service and client side code from
a java class.
Here is a simple example:

<plugin>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-java2ws-plugin</artifactId>
	<version>2.1.3</version>
	<dependencies>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>2.1.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-simple</artifactId>
			<version>2.1.3</version>
		</dependency>
	</dependencies>

	<executions>
		<execution>
			<id>process-classes</id>
			<phase>process-classes</phase>
			<configuration>
				<className>org.apache.hello_world.Greeter</className>
				<genWsdl>true</genWsdl>
				<verbose>true</verbose>
			</configuration>
			<goals>
				<goal>java2ws</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Here are the options you can use:

<configuration>
   <className>...</className>
   <classpath>...</classpath>
   <outputFile>...</outputFile>
   <genWsdl>..</genWsdl>
   <genServer>..</genServer>
   <genClient>..</genClient>
   <genWrapperbean>..</genWrapperbean>
   <frontend>...</frontend>
   <databinding>...</databinding>
   <serviceName>...</serviceName>
   <soap12>...</soap12>
   <targetNameSpace>...</targetNameSpace>
   <verbose>...</verbose>
   <quiet>...</quiet>
</configuration>

The outputFile value by default will be:

{project.build.directory}/generated/wsdl/{className}.wsdl

Refer to this link http://cwiki.apache.org/CXF20DOC/java-to-ws.html for other options detailed usage.

  • No labels