shashwatZing shashwatZing - 2 months ago 13
Groovy Question

Can we use one wsimport task for multiple wsdl's in gradle without repeating the similar code?

I am writing a gradle task that does wsimport to generate the web service artifacts. The task i wrote is working perfectly fine and is shown below.

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")
}
}
}
}


But Since I need to generate artifacts from multiple wsdl's i would like to pass something like an array of WSDL paths on the wsdl parameter. This is the source code that ant.taskdef uses to parse the wsdl. I have already tried passing array of wsdl thinking since
arguments[0]
is an object and since array is an instance of string it might work but it did not. Any help would be appreciated that will help me eliminate code repeatition. Also i am very new to 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
]

Answer

One way to eliminate repetition is to create a list, wsdlPaths, and iterate over it. A simple example:

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}")
            }
        }
    }
}

Here we merely use ant.echo but you should be able to substitute your current code (with slight edits as appropriate).

Comments