我正在编写一个gradle任务,它执行wsimport来生成Web服务工件。我写的任务工作得很好,如下所示。
task wsimport {
ext.destDir = file('gen')
doLast {
ant {
sourceSets.main.output.classesDir.mkdirs()
destDir.mkdirs()
taskdef(name: 'wsimport',
classname: 'com.sun.tools.ws.ant.WsImport',
classpath: 'C:/Users/sbhattar/git/java_commons/common/java/lib/jaxws-2.2.6/jaxws-tools.jar'
)
wsimport(keep: true,
destdir: sourceSets.main.output.classesDir,
sourcedestdir: destDir,
extension: "true",
verbose: "false",
quiet: "false",
xnocompile: "true",
wsdl:"http://spapen2w1.shashwat.com.PlanService_4_0.wsdl") {
xjcarg(value: "-XautoNameResolution")
}
}
}
}
但是由于我需要从多个wsdl生成工件,我想在wsdl参数上传递类似WSDL路径的数组。这是ant.taskdef用于解析wsdl的源代码。我已经尝试过传递wsdl数组,因为arguments[0]
是一个对象,因为数组是字符串的一个实例,它可能有效,但事实并非如此。任何帮助将不胜感激,这将有助于我消除代码重复。此外,我对groovy / gradle非常新。
public void execute(AntBuilder ant, Object... arguments) {
def wsdl = arguments[0]
def extension = arguments[1]
def destinationDir = arguments[2]
def classpath = arguments[3]
def episodes = arguments[4]
log.info("parsing wsdl '{}' with destination directory of '{}'",
wsdl, destinationDir)
ant.taskdef (name : 'wsimport',
classname: 'com.sun.tools.ws.ant.WsImport',
classpath: classpath)
def params = [
wsdl : wsdl.path,
verbose : extension.verbose,
sourcedestdir : destinationDir.path,
keep : extension.keep,
wsdlLocation : extension.wsdlLocation,
xnocompile : extension.xnocompile,
fork : extension.fork,
xdebug : extension.xdebug,
target : extension.target,
xadditionalHeaders : extension.xadditionalHeaders
]
答案 0 :(得分:2)
消除重复的一种方法是创建一个列表wsdlPaths
,然后迭代它。一个简单的例子:
task wsimport {
doLast {
def wsdlPaths = ["path1", "path2", "path3"]
wsdlPaths.each { wsdlPath ->
ant {
// replace this with your current code as appropriate
echo(message: "path is ${wsdlPath}")
}
}
}
}
此处我们仅使用ant.echo
,但您应该能够替换当前的代码(根据需要进行轻微编辑)。