Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle execute command lines in custom task

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.

like image 510
Corneliu Avatar asked Jul 07 '16 16:07

Corneliu


1 Answers

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.

Edit @2022-07-07

There's an important caveat mentioned in the comments below: Gradle's exec does not evaluate a command line like a shell (e.g. Bash on Linux or CMD on Windows) would, it simply executes specified executable with the provided arguments.

This has some hidden consequences that one needs to keep in mind:

  1. To execute a 'command', one needs to specify a valid existing executable file.

    The most common case when this comes up is when trying to use 'commands' that are implemented on a specific OS by a shell and do not have an available implementation in a form of an executable file (e.g. echo and similar commands on Windows).

  2. There are no 'fancy' features supported by modern shells like streams redirects and similar (although you can still redirect stdout of one process to stdin of another using two separate exec invocations with custom IO configuration).

  3. User-defined aliases (e.g. defined in ~/.bsahrc) are not available.

FIY: do not actually do it!

The 'common' work around to 'easily' get a modern shell's functionality inside Gradle would be to actually run a shell process and provide the command line as its argument, e.g. if using Bash:

task shellExec(type: Exec) {
    workingDir "${buildDir}/foo"
    commandLine 'bash', '-c', "echo 'Hello world!'"
}

However, this approach instantly binds the build script to a particular platform making it inoperable on platforms that do not have that particular shell (e.g. above example would not work on Windows).

like image 75
Andrei LED Avatar answered Oct 29 '22 23:10

Andrei LED