如何在Gradle中为“Exec”任务指定一个“@OutputDirectory”?

如何在Gradle中为“Exec”任务指定一个“@OutputDirectory”?,gradle,Gradle,我有以下资料: task cloneProtobuf(type: Exec) { workingDir "${rootProject.buildDir}/github.com/google" commandLine 'git', 'clone', 'https://github.com/google/protobuf.git' enabled = { -> !new File(workingDir, "protobuf/.git").isDirectory() }() d

我有以下资料:

task cloneProtobuf(type: Exec) {
  workingDir "${rootProject.buildDir}/github.com/google"
  commandLine 'git', 'clone', 'https://github.com/google/protobuf.git'

  enabled = { -> !new File(workingDir, "protobuf/.git").isDirectory() }()
  doFirst {
    mkdir workingDir
  }
}

<> >而不是显式设置<代码>启用< /代码>,并使用Gealle表示任务是“代码>跳过”,我希望Gealle认为,如果<代码>原Buff目录已经存在,则任务是<>代码>最新。如何做到这一点?

请注册一个任务,而不是启用/禁用一个任务,然后gradle将知道任务是否是最新的。请看下面的示例,您会发现这些示例非常有用:

task mk(type: Exec) {
  def output = project.file('mk')
  outputs.dir output
  commandLine 'mkdir', output
  workingDir '.'
}

如果您运行了两次
gradle mk
,任务将只执行一次。

这会运行一段时间,然后生成开始发出错误。我猜这是因为目录的时间戳会随着目录内容的改变而改变(例如在目录中创建或删除文件)。嗯,即使我指定了
outputs.file project.file(${workingDir}/protobuf/.git/config”)
,它仍在失败。@NoelYap能否请您提供一个示例来重现您得到的错误?``task cloneProtobuf(type:Exec){workingDir protobufDir.getParentFile()命令行'git','clone','outputs.file project.file(“build/github.com/google/protobuf/.git/config”)doFirst{mkdir workingDir}任务checkoutProtobuf(类型:Exec){workingDir protobufDir命令行'git','checkout','v${protocVersion()}dependsOn cloneProtobuf}任务autogenProtobuf(类型:Exec){workingDir protobufDir executable./autogen.sh'outputs.file project.file($workingDir/configure)dependsOn checkoutProtobuf}``对格式设置表示歉意。不知道如何在注释中正确格式化代码。