groovyantantbuilder

How can I build up an AntBuilder call programmatically in Groovy?


Lets say I have this Groovy code:

ant.exec(executable:"cmd",osfamily:"windows",dir:bin) {
    arg(value: "/c")
    arg(value: "add-user.bat")
    arg(value: user)
    arg(value: pw)
    arg(value: "--silent")                                                                                                      
}

I have such exec calls often in my code with different number of arguments, so I tought it could be a function with an object array parameter:

private void execute(Object... argumens) {
    ant.sequential {
        exec(executable:"cmd",osfamily:"windows",dir:bin) {
            arg(value: "/c")
            //What should I do here
        }
    }
}

//It would be called like this:
execute("add-user.bat",user,pw,"--silent");

What should I write inside the exec element? Is it possible at all to have an iteration inside that exec?

Please be patient with me, I am a Java guy, who wants to write some script in Maven, so I did not understand the magic, which happens in the AntBuilder of Groovy. If you have some easy to understand explanation about how AntBuilder in Groovy works, it is appreciated.


Solution

  • You should be able to do:

    private void execute(Object... argumens) {
        ant.sequential {
            exec(executable:"cmd",osfamily:"windows",dir:bin) {
                arg(value: "/c")
                argumens.each {
                    arg(value: it)
                }
            }
        }
    }