在gradle中设置任务以从经过身份验证的WSDL下载并生成Java类

时间:2018-09-19 19:07:13

标签: authentication gradle jaxb wsdl2java

build.gradle文件中有下一个任务,可以下载WSDL并生成类。

我尝试下载的WSDL已通过基本身份验证进行了身份验证。我有用户名和密码凭据。

如何设置此任务以包含用户名和密码?

我已从https://spring.io/guides/gs/consuming-web-service/

复制了此任务
task genJaxb {
   ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
   ext.classesDir = "${buildDir}/classes/jaxb"
   ext.schema = 'https://IP/path/wsdl.asmx?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: "package.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")
            }
        }
    }
}
}

预先感谢

1 个答案:

答案 0 :(得分:0)

我不确定ant的 XJCTask 是否支持基本身份验证来检索源Wsdl。在您的情况下,我将分两步进行:

  1. 一个任务,该任务使用具有基本身份验证的 ant.get 任务在本地下载wsdl
  2. 一个任务,该任务使用从第一个任务下载的本地模式调用 ant.xjc

一些准则:

下载任务

从网络中检索Wsdl的虚拟示例(即使此Web服务器不需要它,也具有基本身份验证)

ext {
    wsdlSrcUrl = 'http://www.thomas-bayer.com/axis2/services/BLZService?wsdl'
    wsdlLocalPath = "$buildDir/BLZService.wsdl"
    wsdlUser= 'testUser'
    wsdlPassword = 'userPass'
}
task downloadWsdsl {
    doLast {
        file(buildDir).mkdir()
        ant.get(src: wsdlSrcUrl, dest: wsdlLocalPath, username: wsdlUser, password: wsdlPassword)
    }
}

Codegen任务

此任务取决于 downloadWsdsl 任务

task genJaxb {
    dependsOn downloadWsdsl

    ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
    ext.classesDir = "${buildDir}/classes/jaxb"
    ext.schema = wsdlLocalPath

    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: "hello.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")
                }
            }
        }
    }
}