Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run shell command in gradle but NOT inside a task

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.

like image 242
Marcel Avatar asked Mar 17 '14 08:03

Marcel


People also ask

How do I run a command in Gradle task?

Press ⌃⌃ (macOS), or Ctrl+Ctrl (Windows/Linux), type "gradle" followed by the gradle task name or names. We can, of course, run Gradle commands from the terminal window inside IntelliJ IDEA. Open this with ⌥F12 (macOS), or Alt+F12 (Windows/Linux). If we type a Gradle command, IntelliJ IDEA highlights it.

How do I skip a task in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

Where should I execute Gradle command?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

What is doLast in Gradle?

The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build. Both of these print the line, but the first one only prints the line when the testTask is supposed to be executed.


1 Answers

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.

like image 102
Oliver Charlesworth Avatar answered Oct 11 '22 00:10

Oliver Charlesworth