Gradle Exec task

Guys, i’m not 100% sure that i’m right but it looks like bug for me. Anyway let me describe actual problem.

So, Gradle version is 2.12, we use Gradle wrapper on top of regular Gradle.

What i found that Gradle 2.12 is not working correctly on Mac OS Capitan(working on CentOS). So, we have something like:

task gemsCheck(type: Exec) {
commandLine “bundle”, “check”
}
task installGems(type: Exec) {
commandLine “bundle”, “install”
}
task manageGems {
try {
tasks.gemsCheck.execute()
} catch (GradleException e) {
tasks.installGems.execute()
}
}

where bundle is Ruby package manager and it fails:

  • What went wrong:
    Execution failed for task ‘:ModuleName:installGems’.
    A problem occurred starting process ‘command ‘bundle’’

As you can see parent problems happens in specific module not in parent.
But if i change Gradle version in Gradle wrapper from 2.12 to 2.11 everything is working. Plus, what more interesting if i manually call from my terminal

bundle check
or
bundle install

everything is working. My guess is since i call Gradle wrapper from parent folder compare to ModuleName Gradle is trying to execute those Exec commands from parent folder and since parent folder does not have any signs of Ruby gems it fails as described above. Please let me know about any suggestions.

P.S. I’m sorry if misunderstood something, but i googled first and didn’t find right topics/answers

Try running Gradle with --info. That should print out the working directory and command line being executed. The Exec task should run from the current project directory.

You should never call execute() on a task. I think what you really want is…

task installGems(type: Exec) {
   onlyIf { 
      def result = exec { 
         executable = "bundle"
         args "check"
         ignoreExitValue = true
      }
      result.exitValue != 0
   } 
   executable = "bundle"
   args "install"
}

The idea here that you want to execute installGems only if bundler exits with a non-zero exit value.

Thank you very much, this is exactly what i was looking for. Regarding execute method - legacy code.

Sorry, for confuse, but this elegant solution is working on Gradle below 2.12 and if i try Gradle 2.12 i have next output:

Starting process ‘command ‘bundle’’. Working directory: /path/to/ModuleName Command: bundle check
:ModuleName:installGems FAILED
:ModuleName:installGems (Thread[Daemon worker Thread 43,5,main]) completed. Took 0.005 secs.

FAILURE: Build failed with an exception.

  • What went wrong:
    Could not evaluate onlyIf predicate for task ‘:ModuleName:installGems’.
    A problem occurred starting process ‘command ‘bundle’’
  • Try:
    Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

So, as i can see Gradle is not seeing bundle command. Either issue in Gradle or in my recently updated OS (Capitan) - IMHO. Something inside me tells this is OS issue since i tested same script on CentOS and nothing suspicious - worked as supposed. Exclude one of them is really complicated - please advise

Bundle is likely not on your PATH

I think finally figured out the problem. So, first of all(if i understand correctly) there is no such term as PATH for ruby gems. Issue comes as i expected from Mac OS Upgrade (EL Capitan). Apple added additional security and if you use Ruby/Gems OOTB(i’m talking about pre-configured stuff) then you gonna have issues to get access those Gems using Gradle. I had to configure Ruby/Gems from scratch and put it to my custom folder(s) and now Gradle is seeing bundle command. On another hand if i switch for instance to Gradle 2.11 build also working. So, in 90% i’m sure this issue happens because of OS Update and in 10% that something was made in Gradle 2.12. I leave this decision for you, if you find answer - let US know.

Thanks.

I wouldn’t expect 2.11 and 2.12 to differ in behavior. The bundle command would need to be on your $PATH for Gradle to find it in either version.