Scott
Scott

Reputation: 68

Gradle - Error Copying Files To Project Root

I’m getting the following error whenever I attempt to use a Copy task to copy a file into the root of a project (the same folder I’m running gradle from):

Failed to create MD5 hash for file content.

I thought this was related to the artifacts I was pulling from Artifactory, but that seems to be unrelated. I was able to get the same results with a minimal script.

Is there something obviously wrong with what I’m doing, or does Gradle intentionally disallow such things?

task fails(type:Copy) {
    from 'build/someFile.txt'
    into new File('.').absolutePath
}

task works(type:Copy) {
    from 'build/someFile.txt'
    into new File('.').absolutePath + '/output'
}

Upvotes: 2

Views: 2208

Answers (2)

Anand Rockzz
Anand Rockzz

Reputation: 6658

Can you believe it, the following works

task myCopy(type: Copy) {
    from "$rootDir/app1/src/main/resources/db"
    into "$rootDir/app2/src/test/resources/db"
}
test.dependsOn myCopy

and the following doesn't 🤦

task myCopy(type: Copy) {
    from '$rootDir/app1/src/main/resources'
    into '$rootDir/app2/src/test/resources'
}
test.dependsOn myCopy

Upvotes: 0

lance-java
lance-java

Reputation: 27976

Short Answer: Don't copy into the project directory, you are best to use into "$buildDir/someFolder" so that the folder is isolated to this single task, and also so that it will be cleaned by gradle clean

Long Answer: At it's core, Gradle has the concept of an "UP-TO-DATE" check for every single task. If Gradle sees that nothing has changed since last time a task was executed it will use the old result instead of executing again.

UP-TO-DATE checking is implemented by taking a "hash" of the task inputs and task outputs. Since you are using into '.' that means that the entire contents of the project directory is considered a task output (bad)

Gradle uses the .gradle folder for temp files (eg task hashes) It's likely some of these files are locked for writing as Gradle is trying to also read the same files (to calculate the "hash" of the task outputs) causing the error you are seeing

* EDIT *

If you need to copy into the project directory for legacy reasons, you might use Project.copy(...) directly instead of a Copy task. You could manually manage the task inputs/outputs in this case

Eg

task customCopy {
    inputs.file "$buildDir/someFile.txt"
    outputs.file 'someFile.txt'
    doLast {
        copy {
            from "$buildDir/someFile.txt"
            into '.'
        }
    }
}

Upvotes: 3

Related Questions