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

发布于 2024-12-16 01:29:23 字数 2423 浏览 3 评论 0原文

我想将 Maven2 构建文件切换到 gradle。 使用 gradle 从 WSDL + XSD 生成 java 类似乎没有进一步记录,没有相应的 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 want to switch my Maven2 build file to gradle.
Generating the java classes from WSDL + XSDs with gradle seems to be not documented further there is no gradle plugin for this.
I use the following configuration with maven and search the equivalent for 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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪眸﹌ 2024-12-23 01:29:23

我解决了...

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

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
指尖微凉心微凉 2024-12-23 01:29:23

如果您找不到满足特定需求的 Gradle 插件(并且不想编写自己的插件),请寻找 Ant 任务。以下是 JAXB 的一个:XJC Ant 任务

任何 Ant 任务都可以从 Gradle 中按原样使用(请参阅从 Gradle 中使用 Ant)。未来,Gradle还将支持Maven插件的执行。

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. Here is one for JAXB: XJC Ant Task.

Any Ant task can be used as-is from Gradle (see Using Ant from Gradle). In the future, Gradle will also support the execution of Maven plugins.

静赏你的温柔 2024-12-23 01:29:23

按照此处所述使用插件: https://github.com/nilsmagnus/wsdl2java

注意:截至2020 年,此建议不再适用,因为推荐的插件已停止使用,并且不再适用于较新版本的 Gradle。

Use the plugin as described here: 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.

巨坚强 2024-12-23 01:29:23

我发现 xjc- Generation-gradle-plugin 易于使用,并且有据可查。

如果您将 .wsdl 文件放入 src/main/schemas/xjc 中,那么您可以使用 build.gradle 中的此配置从中生成 Java 类em>:

plugins {
   id 'com.github.edeandrea.xjc-generation' version '1.6'
}

ext {
    jaxbVersion = '2.2.11'
}

dependencies {
    implementation "javax.xml.bind:jaxb-api:$jaxbVersion"

    xjc "javax.xml.bind:jaxb-api:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-impl:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-xjc:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-core:$jaxbVersion"
    xjc 'javax.activation:activation:1.1.1'
}

xjcGeneration {
    defaultAdditionalXjcOptions = ['encoding': 'UTF-8']

    schemas {
        wsdlSchema {
            schemaFile = 'my.wsdl'
            javaPackageName = 'com.example'
            sourceSet = 'main'
        }
    }
}

在此示例中,.wsdl 文件为 src/main/schemas/xjc/my.wsdl,并且类在build/ generated-sources/main/xjc/com.example 目录。

如果您的 WSDL 类型部分包含重复类型,那么您将看到如下所示的架构编译错误:

[ant:xjc] [ERROR] A class/interface with the same name "com.example.MyClass" is already in use. Use a class customization to resolve this conflict.

要避免这些错误,请将此设置添加到xjcGeneration 任务:

    defaultAdditionalXjcCommandLineArgs = ["-XautoNameResolution": ""]

I found the xjc-generation-gradle-plugin to be easy to use and well-documented.

If you put your .wsdl file into src/main/schemas/xjc then you can generate java classes from it with this config in your build.gradle:

plugins {
   id 'com.github.edeandrea.xjc-generation' version '1.6'
}

ext {
    jaxbVersion = '2.2.11'
}

dependencies {
    implementation "javax.xml.bind:jaxb-api:$jaxbVersion"

    xjc "javax.xml.bind:jaxb-api:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-impl:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-xjc:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-core:$jaxbVersion"
    xjc 'javax.activation:activation:1.1.1'
}

xjcGeneration {
    defaultAdditionalXjcOptions = ['encoding': 'UTF-8']

    schemas {
        wsdlSchema {
            schemaFile = 'my.wsdl'
            javaPackageName = 'com.example'
            sourceSet = 'main'
        }
    }
}

In this example the .wsdl file is src/main/schemas/xjc/my.wsdl and the classes are generated in the build/generated-sources/main/xjc/com.example directory.

If your WSDL types section contains repeated types then you will see schema compilation errors like this:

[ant:xjc] [ERROR] A class/interface with the same name "com.example.MyClass" is already in use. Use a class customization to resolve this conflict.

To avoid these errors add this setting to the xjcGeneration task:

    defaultAdditionalXjcCommandLineArgs = ["-XautoNameResolution": ""]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文