Gradle 6.2 copy doesn't fail if file is missing

Viewed 205

I am copying files using a copy block in Gradle 6.2 as follows but it does not fail if the file is missing.

copy {
    from file("$project.projectDir/ABC.toml")
    into file("${artifactDir}/XYZ")
}

How can I make the copying fail if "$project.projectDir/ABC.toml" file is missing ?

1 Answers

You can make it fail by checking the existence of the file manually and if it's absent, then throw an exception.

task copyMethod {
    doFirst {
        if (!file("ABC.toml").exists()) {
            throw new GradleException("ABC.toml not found")
        }
    }

    doLast {
        copy {
            from "ABC.toml"
            into "${artifactDir}/XYZ"
        }
    }
}
Related