我正在编写一个gradle任务来生成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
]
消除重复的一种方法是创建一个列表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
,但你应该能够替换你当前的代码(适当的轻微编辑)。