简体   繁体   English

如何使用 gradle 从 WSDL 和 XSD 生成类,相当于 maven-jaxb2-plugin

[英]Howto generate classes from WSDL and XSD with gradle, equivalent to maven-jaxb2-plugin

I want to switch my Maven2 build file to gradle.我想将我的 Maven2 构建文件切换到 gradle。 Generating the java classes from WSDL + XSDs with gradle seems to be not documented further there is no gradle plugin for this.似乎没有进一步记录使用 gradle 从 WSDL + XSD 生成 java 类,没有为此的 gradle 插件。 I use the following configuration with maven and search the equivalent for gradle.我将以下配置与 maven 一起使用,并搜索 gradle 的等效项。

<!-- plugin for generating the classes from the WSDL+XSD -->
<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <version>0.7.3</version>
  <executions>
    <execution>
      <id>app1-stub-generation</id>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <schemaDirectory>${project.build.directory}/wsdl/app1</schemaDirectory>
        <schemaIncludes>
          <include>*.xsd</include>
        </schemaIncludes>
        <generatePackage>org.app1.ws.generated</generatePackage>
        <generateDirectory>${project.build.directory}/generated-sources/app1</generateDirectory>
        <strict>true</strict>
      </configuration>
    </execution>
    <execution>
      <id>app2-v1-stub-generation</id>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <schemaDirectory>src/main/resources/wsdl</schemaDirectory>
        <schemaIncludes>
          <include>v1/*.xsd</include>
        </schemaIncludes>
        <generatePackage>org.app2.ws.generated.v1</generatePackage>
        <generateDirectory>${project.build.directory}/generated-sources/v1</generateDirectory>
        <strict>true</strict>
      </configuration>
    </execution>
    <execution>
      <id>app2-v2-stub-generation</id>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <schemaDirectory>src/main/resources/wsdl</schemaDirectory>
        <schemaIncludes>
          <include>v2/*.xsd</include>
        </schemaIncludes>
        <generatePackage>org.app2.ws.generated.v2</generatePackage>
        <generateDirectory>${project.build.directory}/generated-sources/v2</generateDirectory>
        <strict>true</strict>
      </configuration>
    </execution>
  </executions>
</plugin> 

i solved it...我解决了...

configurations {
    jaxb
}

dependencies {
    jaxb group: 'com.sun.xml.bind', name: 'jaxb-xjc', version: '2.2.4-1'
}

task jaxb () {
    // output directory
    jaxbTargetDir = file( "${buildDir}/generated-sources" )
    jaxbTargetDirV19 = file( jaxbTargetDir.path + '/v19' )
    jaxbTargetDirV110 = file( jaxbTargetDir.path + '/v110' )
    jaxbTargetDirOtherWs = file( jaxbTargetDir.path + '/otherWs' )

    // perform actions
    doLast {
        jaxbTargetDirV19.mkdirs()
        jaxbTargetDirV110.mkdirs()
        jaxbTargetDirOtherWs.mkdirs()

        ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.jaxb.asPath)
        ant.jaxbTargetDirV19 = jaxbTargetDirV19
        ant.jaxbTargetDirV110 = jaxbTargetDirV110
        ant.jaxbTargetDirOtherWs = jaxbTargetDirOtherWs

        // My-Webservice v1.10
        ant.xjc(
                destdir: '${jaxbTargetDirV110}',
                package: 'mypackage.ws.generated.v110',
                schema: 'src/main/resources/wsdl/v1.10/MyServiceSchema.xsd'
        )

        // My-Webservice v1.9
        ant.xjc(
                destdir: '${jaxbTargetDirV19}',
                package: 'mypackage.ws.generated.v19',
                schema: 'src/main/resources/wsdl/v1.9/MyServiceSchema.xsd'
        )

        // OtherWs-Webservice
        ant.xjc(
                destdir: '${jaxbTargetDirOtherWs}',
                package: 'mypackage.otherws.generated',
                schema: 'src/main/resources/wsdl/OtherWsServiceSchema.xsd'
        )
    }
}
compileJava.dependsOn jaxb

If you can't find a Gradle plugin for a particular need (and don't want to write your own plugin), look out for an Ant task.如果您找不到满足特定需求的 Gradle 插件(并且不想编写自己的插件),请寻找 Ant 任务。 Here is one for JAXB: XJC Ant Task .这是 JAXB 的一个: XJC Ant Task

Any Ant task can be used as-is from Gradle (see Using Ant from Gradle ).任何 Ant 任务都可以从 Gradle 中按原样使用(请参阅从 Gradle 中使用 Ant )。 In the future, Gradle will also support the execution of Maven plugins.未来,Gradle 还将支持 Maven 插件的执行。

Use the plugin as described here: https://github.com/nilsmagnus/wsdl2java使用此处描述的插件: https ://github.com/nilsmagnus/wsdl2java

Note: as of 2020, this advice is no longer applicable, as the recommended plugin has been discontinued and no longer works with newer versions of Gradle.注意:自 2020 年起,此建议不再适用,因为推荐的插件已停产,并且不再适用于较新版本的 Gradle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM