shashwatZing shashwatZing - 11 months ago 75
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 {
taskdef(name: 'wsimport',
classname: '',
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:"") {
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
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]"parsing wsdl '{}' with destination directory of '{}'",
wsdl, destinationDir)

ant.taskdef (name : 'wsimport',
classname: '',
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 :,
xadditionalHeaders : extension.xadditionalHeaders

Answer Source

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).