Question

What I currently have is:

task myTask (type : Exec) {
   executable "something.sh"
   ... (a lot of other things)
   args "-t"
   args ext.target
}

task doIt {
   myTask.ext.target = "/tmp/foo"
   myTask.execute();

   myTask.ext.target = "/tmp/gee"
   myTask.execute();
}

With this I thought I could run "myTask" with different parameters when I start "doIt". But only the first time the script is executed because gradle takes care that a task only runs once. How can I rewrite the "myTask" so that I can call it more than once? It is not necessary to have it as a separate task.

Was it helpful?

Solution

You can do something like the following:

def doMyThing(String target) {
    exec {
        executable "something.sh"
        args "-t", target
    }
}

task doIt {
    doLast {
        doMyThing("/tmp/foo")
        doMyThing("/tmp/gee")
    }
}

The exec here is not a task, it's the Project.exec() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top