26

In my gradle file I defined the following task:

task text_example <<
{
   //?!? commandLine ''
   println 'Fam Flinstone'
}

I want to put inside this task some commands line. How can I do that ?

I'm using a library to automatically publish in google play. My project is based on Product Flavors and I need to pass in terminal command line by command line for every and each one of my flavors. So I want to pass all commands line in test_example task.

Opal
  • 76,740
  • 25
  • 177
  • 200
Corneliu
  • 371
  • 1
  • 5
  • 12

1 Answers1

48

You basically have two major convenient options:

  1. Use Gradle Exec task type

     task fooExec(type: Exec) {
         workingDir "${buildDir}/foo"
         commandLine 'echo', 'Hello world!'
         doLast {
             println "Executed!"
         }
     }
    
  2. Use Gradle Project.exec method

     task execFoo {
         doLast {
             exec {
                 workingDir "${buildDir}/foo"
                 executable 'echo'
                 args 'Hello world!'
             }
             println "Executed!"
         }
     }
    

In both cases inside the closure you can specify execution parameters using methods of ExecSpec. Standard output and error output from the executed commands will be redirected to stdout and stderr of the gradle process.

Andrei LED
  • 2,420
  • 16
  • 20