`

maven-cxf-codegen-plugin-wsdl-to-java

 
阅读更多

From:http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html

 

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/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 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 Java page.

...
<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

<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>

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: Specify the data binding

<configuration>
  <wsdlOptions>
    <wsdlOption>
      <wsdl>${basedir}/src/main/resources/wsdl/myService.wsdl</wsdl>
      <extraargs>
        <extraarg>-databinding</extraarg>
        <extraarg>jibx</extraarg>
      </extraargs>
    </wsdlOption>
  </wsdlOptions>
</configuration>

In this example we're specifying that we want CXF to use our data binding jibx. You can also using the data binding of xmlbeans, domsources, sdo etc.

Example 3: Specifying a service to generate artifacts for

<configuration>
  <wsdlOptions>
    <wsdlOption>
      <wsdl>${basedir}/src/main/resources/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 4: Using defaultOption to avoid repetition

<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> and <wsdlOption> correspond to the options outlined on the WSDL to Java page, you may look athttp://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 5: Using wsdlRoot with includes/excludes patterns

<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>

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

Example 6: 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 7: Using xjc extensions

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

Fluent API for setters

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

fluent-api

An example showing attachment of a JAXB binding file and the CXF toString() extension is below:

<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</extraarg>
            <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.version}</version>
     </dependency>
</dependencies>
</plugin>

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 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:

<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:

<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:

<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</version>
        </dependency>
        <dependency>
           <groupId>com.sun.xml.bind</groupId>
           <artifactId>jaxb-xjc</artifactId>
           <version>2.2</version>
        </dependency>
     </dependencies>
</plugin>

Other configuration options

The cxf-codegen-plugin has some additional configuration options that may be useful:

<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 impl jars)

分享到:
评论

相关推荐

    apache-cxf-3.2.7.zip

    还有`cxf-java2ws-plugin`,可以将Java接口转换为WSDL,方便服务发布。 5. **CXF的主要特点**: - **兼容性**:CXF支持多种Web服务规范,包括JAX-WS、JAX-RS、MTOM、SAAJ等。 - **注解驱动**:通过注解,开发者...

    apache-cxf-2.7.7以及cxf客户端所需要的jar包

    java -jar cxf-codegen-plugin.jar wsdl2java -d /output/directory http://example.com/service?wsdl ``` 这里,`-d`参数指定了生成代码的目录,`http://example.com/service?wsdl`是WSDL文件的URL。 此外,压缩包...

    apache-cxf-2.4.1.rar_apache-cxf-2.4.1_cxf_web service

    6. **工具集**:CXF提供了丰富的开发工具,包括代码生成器、WS-Security配置工具、WSDL-to-Java和Java-to-WSDL工具等,帮助开发者更高效地工作。 7. **国际化与本地化支持**:CXF框架内置了对多语言环境的支持,...

    CXF契约优先开发方式之客户端实现(client)

    以下是如何使用maven-cxf-codegen-plugin生成客户端代码的步骤: 1. 在`pom.xml`中添加maven-cxf-codegen-plugin插件配置: ```xml &lt;plugin&gt; &lt;groupId&gt;org.apache.cxf &lt;artifactId&gt;cxf-codegen-plugin ${cxf...

    wsdl2java源码-cxf-spring-boot:cxf-spring-boot

    wsdl2java源码Apache CXF 是一个开源服务框架,可帮助使用前端编程 API(如 JAX-WS)构建和开发服务。 在本教程中,我们将了解如何将 CXF 与 Spring Boot 集成以构建和运行 Hello World SOAP 服务。 在整个示例中,...

    apache-cxf-2.6.2(含源码)

    CXF提供了多种工具,如wsdl2java用于生成Java代码,cxf-codegen-plugin用于Maven项目,使得开发过程更加自动化。 总的来说,"apache-cxf-2.6.2(含源码)"这个压缩包为开发者提供了一个完整的CXF学习和开发环境。无论...

    maven cxf 项目搭建,可以直接访问

    4. 生成服务端点:使用CXF的插件(如cxf-codegen-plugin)生成服务端点类。 5. 配置CXF:在`src/main/webapp/WEB-INF/web.xml`中配置CXF Servlet。 6. 打包部署:使用Maven的`package`目标打包为WAR文件,然后部署到...

    wsdl2java源码-springboot-cxf-demo:基于ApacheCXF框架,并以“Contract-First”模式进行服务客

    本例为Maven工程,借助cxf-codegen-plugin插件,将在代码编译期间生成Java代码,开发工具IDE需要设置target\generated\cxf为源代码路径 启动服务端,进入CxfServer目录, 运行端口8484,可在application.properties...

    基于maven的cxf+spring简单demo

    最后,通过Maven的cxf-codegen-plugin插件生成客户端和服务端的Stubs,并运行项目。访问指定的URL(如http://localhost:8080/hello?wsdl),就可以看到服务的WSDL描述。 【标签】"cxf"和"spring"表明这个项目主要...

    利用CXF的wsdl2java来建立Web Service.

    java -jar cxf-codegen-plugin-X.X.X.jar wsdl2java -wsdl &lt;path_to_wsd_file&gt; ``` 这里的`X.X.X`应替换为你的CXF版本号,`&lt;path_to_wsd_file&gt;`是WSDL文件的路径。 4. **生成的代码**: `wsdl2java`工具会生成一...

    cxf spring maven 实例

    6. **Maven插件**:CXF提供了Maven插件,如`cxf-codegen-plugin`,可以自动生成服务客户端和服务器端代码,简化开发工作。Maven的生命周期也包含了编译、测试、打包等阶段,确保项目构建的一致性。 7. **测试Web...

    CXF实例一 Maven构建

    我们可以通过在pom.xml中配置CXF插件,然后执行`mvn cxf-codegen-plugin:wsdl2java`命令生成客户端代码。 总的来说,【CXF实例一 Maven构建】是一个涵盖了Maven基本用法和CXF服务开发的教程。通过这个实例,开发者...

    WSDL2Java工具

    2. 运行命令行工具:使用CXF提供的命令行工具`cxf-codegen-plugin`,通常在Maven的`bin`目录下。 3. 输入命令:例如,`mvn cxf-codegen-plugin:generate`,并指定WSDL文件路径,以及其他参数如包名、目标位置等。 4....

    java中webService生成客户端

    cxf-codegen-plugin -wsdlLocation=http://example.com/wsdl/service.wsdl -d src/main/java ``` 这里,`http://example.com/wsdl/service.wsdl`替换为你的WSDL文件地址,`src/main/java`是你希望生成代码的目标...

    csf maven 实例

    3. **Maven插件**:在Maven中,我们可以使用CXF相关的插件如`cxf-codegen-plugin`,这个插件可以从WSDL文件生成Java服务接口和服务实现。同时,`maven-jar-plugin`和`maven-war-plugin`用于打包服务到JAR或WAR文件中...

    CXF学习-环境搭建

    - 使用CXF的工具(如`cxf-codegen-plugin`)从接口生成服务端点类和服务部署文件(如wsdl)。 - 同时,也可以生成客户端代理类,用于调用服务。 7. **部署和测试服务** - 在Tomcat或其他支持Servlet的服务器上...

    使用DOS 生成webservice 客户端代码 (CXF)

    例如,对于CXF,你可以使用`cxf-codegen-plugin`,这是一个Maven插件,可以自动生成Java客户端代码。以下是基本步骤: 1. **设置环境变量**:确保你的PATH环境变量包含了CXF的bin目录,这样你可以在任何地方运行CXF...

    基于CXF的webService本地数据交互----PC端与Android端(二)

    - cxf-codegen-plugin:Maven插件,用于自动执行wsdl2java任务。 - CXF的JAXB和XML绑定工具:用于处理数据序列化和反序列化。 总结,通过Apache CXF,我们可以轻松地在PC端和Android端之间建立Web Service通信,...

    cxf开发webservice客户端

    - **Maven插件**:在Maven项目中,可以在pom.xml中配置CXF的maven-cxf-codegen-plugin插件,自动化生成客户端代码。 4. **配置客户端** 生成的客户端代码包括服务代理类和服务接口。你需要配置服务的URL和其他...

Global site tag (gtag.js) - Google Analytics