Why won't gradle Exec task run my command?

2019-07-06 22:32发布

问题:

I have read around stackoverflow and the gradle forms, but I am still stumped. The ultimate goal here is that after I copy some files, I want to set the writable flag -- because 'copy' doesn't like overwriting read-only files on 'nix (huh...), nor can it be forced to do so (harumph!).

Here is the outline of what I have:

task setPermissions (type : Exec) {
  executable = 'chmod -R +w'
}

// ... a little while later ...

task('somethingElse') << {
  // ... unrelated stuff ...
  def String targetDir = "$aVar/theTarget"

  // >> TASK CALL <<
  setPermissions {
    commandLine = [executable + " $targetDir"]
  }

  // but that doesn't work... this does...
  proc = Runtime.getRuntime().exec("chmod -R +w $deployDir")
  proc.waitFor()

}

I have tried variations in "setPermissions".

Trial 1:

commandLine = 'chmod'
args = '-R', '+w'

In which case I appended the target directory to "args" when I called setPermissions.

Trial 2:

commandLine = 'chmod -R +w'

In which case I appended the target directory to "commandLine" when I called setPermissions. I also tried making it the only "args" value.

Trial 3:

commandLine = 'chmod', '-R', '+w'

In which case I appended the target directory to "commandLine" when I called setPermissions. I also tried making it the only "args" value.

So what am I doing wrong here that an Exec task won't run this properly, but the Rt.gR.exec() will?

回答1:

You can't call a task from another task. You'll have to make one depend on the other, or call the Project.exec method from a task action. The syntax for configuring the exec method is exactly the same as for the Exec task.

PS: Have you tried to use Copy.fileMode instead of chmod?



标签: gradle