Gradle Copy Task - Don't Overrite & upToDateWhen

Greetings. I wish to have a gradle task that copies a single file with the following behavior:

  1. When the source has been modified (overwrite destination).

  2. When the destination doesn’t exist.

I did see on stack overflow something similar to what I want:

task resgenCustomProps(type: Copy) {
    from '../../CodeGen/custom.properties'
    into 'src/main/assets'
    eachFile { if (it.relativePath.getFile(destinationDir).exists()) it.exclude() }
}

However, this task always excludes the copy even if the source has been modified, and is meant for entire folder copies. I was playing with “outputs.upToDateWhen” but didn’t seem to get it right:

task resgenCustomProps(type: Copy) {
    from '../../CodeGen/custom.properties'
    into 'src/main/assets'
    outputs.upToDateWhen { file('src/main/assets/custom.properties').exists() }
}

This version seems to copy even if the destination gets modified (not what I want)… maybe I need something like “outputs.onlyUpToDateWhen”? Is there some suggestions anyone might have for my use case?

Thanks for helping me get the task exactly as I want.

I think you might need to write your own task type to get what you want.
Untested example:

class MyCopy extends DefaultTask {
    @InputFile
    def srcFile

    @Input
    def destDir

    MyCopy() {
        outputs.upToDateWhen { new File( project.file( destDir ), srcFile.name ).exists() }
    }

    @TaskAction
    void doCopy() {
        project.copy {
            from srcFile
            into destDir
        }
    }
}

task resgenCustomProps( type: MyCopy ) {
    srcFile '../../CodeGen/custom.properities'
    destDir 'src/main/assets'
}

Why do you need this behaviour? What modifies the copied file after the copy?

Ahh thank you. I’ll test it out. The file is modified by developers after it’s copied (.gitignored), but, the defaults\main version is in VCS. Idea is if default\main is changed in VSS then developers loose their added preferences (typically ip address of their test machine/app props/etc), which doesn’t really happen often, otherwise they keep their modified props. They wanted it this way so if they decide to make some obscure setting/prop/etc turned on for live, that all the devs will get synced with that setting/prop/etc too, but, still lets the devs turn anything on/off if they wish afterwards.

  • In my option it’s still a little annoying when the file gets wiped by VCS, but meh.

Thanks again, I’ll test/tune it if needed.