如何在gradle中使用带有项目属性的复制任务

时间:2014-10-23 15:38:25

标签: gradle build.gradle

我有一个包含多个任务的build.gradle文件。

我有一个复制任务,它接受输入从命令行获取的属性值。

def dir = new File('myDir')

task createDir << {
    dir.mkdirs()
}

task copyFile(dependsOn: createDir, type: Copy){
    from myProperty
    into myDir
}

我打电话给gradle:

gradle -PmyProperty=myInputFile copyFile

当我有一个不需要myProperty定义的其他独立任务

时,就会出现问题
task clean << {
    dir.deleteDir()
}

我有以下错误

$ gradle clean

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\temp\build.gradle' line: 41

* What went wrong:
A problem occurred evaluating root project 'temp'.
> Could not find property 'myProperty' on task ':copyFile'.

BUILD FAILED

如何在不需要提供缺失属性的情况下运行干净的任务?

P.S。我现在认为我的问题的标题不是很好,但我不知道如何使它更具表现力。

1 个答案:

答案 0 :(得分:0)

这应该有效:

task copyFile(dependsOn: createDir, type: Copy){
    from project.hasProperty('myProperty') ? project.myProperty : ''
    into myDir
}

问题出现了财产没有通过。因为此代码在配置阶段运行,您需要以某种方式替换它。

相关问题