Run shell command in gradle but NOT inside a task

你离开我真会死。 提交于 2019-11-30 00:43:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/22449649/run-shell-command-in-gradle-but-not-inside-a-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!