Spring Boot消费WebService – 如何在不同包中使用Gradle拥有多个xsd和wsdl?

huangapple go评论115阅读模式
英文:

Spring Boot Consuming WebService - How to have multiple xsd and wsdl on different packages with gradle?

问题

我正在按照这个指南进行操作:
https://spring.io/guides/gs/consuming-web-service/

这个jaxb任务看起来是这样的:

task genJaxb {
  ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
  ext.classesDir = "${buildDir}/classes/jaxb"
  ext.schema = "http://localhost:8080/ws/countries.wsdl"

  outputs.dir classesDir

  doLast() {
    project.ant {
      taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
          classpath: configurations.jaxb.asPath
      mkdir(dir: sourcesDir)
      mkdir(dir: classesDir)

      xjc(destdir: sourcesDir, schema: schema,
          package: "com.example.consumingwebservice.wsdl") {
          arg(value: "-wsdl")
        produces(dir: sourcesDir, includes: "**/*.java")
      }

      javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
          debugLevel: "lines,vars,source",
          classpath: configurations.jaxb.asPath) {
        src(path: sourcesDir)
        include(name: "**/*.java")
        include(name: "*.java")
      }

      copy(todir: classesDir) {
          fileset(dir: sourcesDir, erroronmissingdir: false) {
          exclude(name: "**/*.java")
      }
    }
  }
}

如果只有一个wsdl,甚至多个wsdl(稍作调整)在同一个包中工作得很好。

我需要处理一些XSD和WSDL,并将它们放在不同的包中,但在同一个源中进行编译。

例如:

  • wsdl1.wsdl -> com.example.wsdl1
  • wsdl2.wsdl -> com.example.wsdl2
  • xsd_folder1/*.xsd -> com.example.xsd
  • xsd_folder2/*.xsd -> com.example.other_xsd

然后在同一个项目中编译。

我该如何实现这个目标?

英文:

I am following this guide:
https://spring.io/guides/gs/consuming-web-service/

The jaxb task looks like this:

task genJaxb {
  ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
  ext.classesDir = "${buildDir}/classes/jaxb"
  ext.schema = "http://localhost:8080/ws/countries.wsdl"

  outputs.dir classesDir

  doLast() {
    project.ant {
      taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
          classpath: configurations.jaxb.asPath
      mkdir(dir: sourcesDir)
      mkdir(dir: classesDir)

        xjc(destdir: sourcesDir, schema: schema,
            package: "com.example.consumingwebservice.wsdl") {
            arg(value: "-wsdl")
          produces(dir: sourcesDir, includes: "**/*.java")
        }

        javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
            debugLevel: "lines,vars,source",
            classpath: configurations.jaxb.asPath) {
          src(path: sourcesDir)
          include(name: "**/*.java")
          include(name: "*.java")
          }

        copy(todir: classesDir) {
            fileset(dir: sourcesDir, erroronmissingdir: false) {
            exclude(name: "**/*.java")
        }
      }
    }
  }
}

It's working great if there is only one wsdl or even multiple wsdl (with a few tweaks) on the same package.

I need to process some XSD and WSDL and place them on different packages, but compile in the same source.

Example:

  • wsdl1.wsdl -> com.example.wsdl1
  • wsdl2.wsdl -> com.example.wsdl2
  • xsd_folder1/*.xsd -> com.example.xsd
  • xsd_folder2/*.xsd -> com.example.other_xsd

Then compile in the same project.

How can I achieve this?

答案1

得分: 3

我遇到了相同的问题。我成功让它工作了,虽然不是我想要的方式,但它完成了任务。也许对你有帮助。否则,我自己也对这个(干净的)答案很感兴趣。

configurations {
    jaxb
}

task genJaxb {
    ext.sourcesDirWsdl = "${projectDir}/src/main/generated/wsdl"
    ext.classesDirWsdl = "${buildDir}/classes/jaxb/wsdl"
    ext.schemaDirWsdl = "${projectDir}/src/main/resources/wsdl"

    ext.sourcesDirXsd = "${projectDir}/src/main/generated/xsd"
    ext.classesDirXsd = "${buildDir}/classes/jaxb/xsd"
    ext.schemaDirXsd = "${projectDir}/src/main/resources/xsd"

    outputs.dir classesDirWsdl
    outputs.dir classesDirXsd

    doLast() {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                    classpath: configurations.jaxb.asPath
            mkdir(dir: sourcesDirWsdl)
            mkdir(dir: classesDirWsdl)
            mkdir(dir: sourcesDirXsd)
            mkdir(dir: classesDirXsd)

            xjc(destdir: sourcesDirWsdl) {
                schema(dir: schemaDirWsdl, includes: "*.wsdl")
                arg(value: "-wsdl")
                produces(dir: sourcesDirWsdl, includes: "**/*.java")
            }
            xjc(destdir: sourcesDirXsd) {
                schema(dir: schemaDirXsd, includes: "*.xsd")
                arg(value: "-wsdl")
                produces(dir: sourcesDirXsd, includes: "**/*.java")
            }

            javac(destdir: classesDirWsdl, source: 1.8, target: 1.8,
                    debug: true, debugLevel: "lines,vars,source",
                    includeantruntime: false,
                    classpath: configurations.jaxb.asPath) {
                src(path: sourcesDirWsdl)
                include(name: "**/*.java")
                include(name: "*.java")
            }
            javac(destdir: classesDirXsd, source: 1.8, target: 1.8,
                    debug: true, debugLevel: "lines,vars,source",
                    includeantruntime: false,
                    classpath: configurations.jaxb.asPath) {
                src(path: sourcesDirXsd)
                include(name: "**/*.java")
                include(name: "*.java")
            }

            copy(todir: classesDirWsdl) {
                fileset(dir: sourcesDirWsdl, erroronmissingdir: false) {
                    exclude(name: "**/*.java")
                }
            }
            copy(todir: classesDirXsd) {
                fileset(dir: sourcesDirXsd, erroronmissingdir: false) {
                    exclude(name: "**/*.java")
                }
            }
        }
    }
}

然后在你的dependencies中添加以下内容:

implementation('org.springframework.boot:spring-boot-starter-web-services') {
    exclude(group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat')
}
implementation('org.springframework.ws:spring-ws-core')

implementation('wsdl4j:wsdl4j:1.6.3')
jaxb("org.glassfish.jaxb:jaxb-xjc:2.3.2")
compile(files(genJaxb.classesDirWsdl, genJaxb.classesDirXsd).builtBy(genJaxb))

编辑:我曾经在xjc方法中以package: com.example.somepck开头,但它总是被覆盖,所以我将它移到了目录中。正如我所说,这并不是很干净,但对我来说完成了任务。也许你可以使用它,如果有必要的话进行调整。如果你有任何改进意见,请告诉我。

英文:

i had the same issue. I kinda made it work, not like i wanted to, but it does the job. Maybe it is going to help you. Otherwise i am interested in this (clean) answer myself.

configurations {
jaxb
}
task genJaxb {
ext.sourcesDirWsdl = "${projectDir}/src/main/generated/wsdl"
ext.classesDirWsdl = "${buildDir}/classes/jaxb/wsdl"
ext.schemaDirWsdl = "${projectDir}/src/main/resources/wsdl"
ext.sourcesDirXsd = "${projectDir}/src/main/generated/xsd"
ext.classesDirXsd = "${buildDir}/classes/jaxb/xsd"
ext.schemaDirXsd = "${projectDir}/src/main/resources/xsd"
outputs.dir classesDirWsdl
outputs.dir classesDirXsd
doLast() {
project.ant {
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
classpath: configurations.jaxb.asPath
mkdir(dir: sourcesDirWsdl)
mkdir(dir: classesDirWsdl)
mkdir(dir: sourcesDirXsd)
mkdir(dir: classesDirXsd)
xjc(destdir: sourcesDirWsdl) {
schema(dir: schemaDirWsdl, includes: "*.wsdl")
arg(value: "-wsdl")
produces(dir: sourcesDirWsdl, includes: "**/*.java")
}
xjc(destdir: sourcesDirXsd) {
schema(dir: schemaDirXsd, includes: "*.xsd")
arg(value: "-wsdl")
produces(dir: sourcesDirXsd, includes: "**/*.java")
}
javac(destdir: classesDirWsdl, source: 1.8, target: 1.8,
debug: true, debugLevel: "lines,vars,source",
includeantruntime: false,
classpath: configurations.jaxb.asPath) {
src(path: sourcesDirWsdl)
include(name: "**/*.java")
include(name: "*.java")
}
javac(destdir: classesDirXsd, source: 1.8, target: 1.8,
debug: true, debugLevel: "lines,vars,source",
includeantruntime: false,
classpath: configurations.jaxb.asPath) {
src(path: sourcesDirXsd)
include(name: "**/*.java")
include(name: "*.java")
}
copy(todir: classesDirWsdl) {
fileset(dir: sourcesDirWsdl, erroronmissingdir: false) {
exclude(name: "**/*.java")
}
}
copy(todir: classesDirXsd) {
fileset(dir: sourcesDirXsd, erroronmissingdir: false) {
exclude(name: "**/*.java")
}
}
}
}
}

and you put in your dependencies

implementation('org.springframework.boot:spring-boot-starter-web-services') {
exclude(group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat')
}
implementation('org.springframework.ws:spring-ws-core')
implementation('wsdl4j:wsdl4j:1.6.3')
jaxb("org.glassfish.jaxb:jaxb-xjc:2.3.2")
compile(files(genJaxb.classesDirWsdl, genJaxb.classesDirXsd).builtBy(genJaxb))

Edit: I had started with a package: com.example.somepck in the xjc method, but i got it always overriden, so i moved it to the directory. As i said, it is not that clean, but for me it did the job. Maybe you can use it and tweak it if necessary.
If you have some imporvement, please let me know.

huangapple
  • 本文由 发表于 2020年1月6日 20:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定