如何在没有 @Optional 注释的情况下在 Gradle 任务上指定可选输入?

Rob*_*ert 5 gradle

我想使用可选输入(如果存在),如果不存在则继续。

当我运行时gradle -Dorg.gradle.warning.mode=all,我收到了仅指定输入的弃用警告:

任务“:addWorkingCopyInfo”的配置存在问题。通过 TaskInputs 和 TaskOutputs 方法注册无效的输入和输出已被弃用,并计划在 Gradle 5.0 中删除。- 为属性“$1”指定的文件“/Users/robert/test/special-build-tag”不存在。

这是构建脚本中的任务:

task addWorkingCopyInfo(type: Exec) {
    inputs.file file("tagFile")   // deprecated if the file does not exist
    outputs.file file("generated/taginfo")
    executable "perl" args "..."
}
Run Code Online (Sandbox Code Playgroud)

我已经看到,@Optional如果我有自定义任务类,我可以添加注释,但这里的情况并非如此。

我最好的解决方案是添加对文件的检查,并且仅将其作为输入(如果存在)。这似乎有效。

task addWorkingCopyInfo(type: Exec) {
    def tagFile = new File("tagFile");
    if (tagFile.exists()) {
        inputs.file tagFile
    }
    outputs.file file("generated/taginfo")
    executable "perl" args "..."
}
Run Code Online (Sandbox Code Playgroud)

有没有更好/更 Gradle 风格的方法来做到这一点?

Luk*_*fer 4

该方法inputs.files(...)返回TaskInputFilePropertyBuilder提供方法optional()和 的a optional(boolean)

你试一试:

inputs.files('my-file').optional()
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了这个答案,但它不起作用。我认为这实际上是不正确的。请参阅 https://discuss.gradle.org/t/what-is-taskinputfilepropertybuilder-optional-meant-to-do/41055/2 和 /sf/answers/4853831321/ (2认同)