Gradle复制任务无提示失败

时间:2016-12-06 14:23:34

标签: gradle

我有1个文件的复制任务

task myCopyTask(type: Copy) {
    copy {
        from "/path/to/my/file"
        into "path/to/out/dir"
    }
}

怎么办,如果复制失败,任务会失败?现在,如果文件不存在,则不会出错。

Fail Gradle Copy task if source directory not exist提供了解决方案。这不起作用,因为如果一切都不在

之内
copy { ... }

任务根本不起作用。

我也尝试了

task myCopyTask(type: Copy) {
    copy {
        from "/path/to/my/file"
        into "path/to/out/dir"
        inputs.sourceFiles.stopExecutionIfEmpty()
    }
}

上述操作会失败,因为inputs.sourceFiles将为空。

2 个答案:

答案 0 :(得分:8)

为什么不将任务指定为:

task myCopyTask(type: Copy) {
    from "/path/to/my/file"
    into "path/to/out/dir"
    inputs.sourceFiles.stopExecutionIfEmpty()
}

这将在执行阶段按预期工作,而您的解决方案将在每次调用任何任务时在构建的配置阶段尝试复制某些内容。

答案 1 :(得分:0)

任务的第一个定义实际上并没有达到你对任务的期望:

task myCopyTask(type: Copy) {
copy {
    from "/path/to/my/file"
    into "path/to/out/dir"
}

实际相同
task myCopyTask(type: Copy) {
    project.copy {
        from "/path/to/my/file"
        into "path/to/out/dir"
    }
}

它将在任务配置期间执行copy操作,无论是否调用该任务。

您需要的是:

task myCopyTask(type: Copy) {
    from "/path/to/my/file"
    into "path/to/out/dir"

    doFirst {
        if(inputs.empty) throw new GradleException("Input source for myCopyTask doesn't exist")
    }
}