Force a copy task to run again

task copyConfigFiles {
    def src = System.getenv()['SOURCE']
    def dest = System.getenv()['DESTINATION']
    copy {
        from "${src}/config/etc/"
        into "${dest}/home/tivo/etc"
        include 'test*.conf'
        rename { String fileName ->
            fileName.replace('testABC.conf', 'ABC.conf')
        }
        rename { String fileName ->
            fileName.replace('testPQR.conf', 'PQR.conf')
        }
        rename { String fileName ->
            fileName.replace('testSTU.conf', 'STU.conf')
        }
        rename { String fileName ->
            fileName.replace('testXYZ.conf', 'XYZ.conf')
        }
        fileMode = 0644
    }
}

I want this code to be forcefully executed. I mean if it is executed twice consecutively, it should actually copy file twice. I saw some solutions like

outputs.upToDateWhen {
     false
   }

but it does not actually copy file twice. since if any file in SOURCE is changed is not reflected in DESTINATION directory.

Is there any good clean solution to do that?

You are copying in the configuration phase rather than the execution phase, which can’t be right. The task declaration should look like this:

task copyConfigFiles(type: Copy) {
    def ...
    from ...
    into ...
    ...
}

Usually it isn’t necessary to throw in ‘outputs.upToDateWhen { false }’ because Gradle can reliably figure out on its own whether the task has to run again.

Thanks peter, but what if I need to ignore gradle’s judgement and even if gradle decide’s not to run I still want to run it forcefully?

In that case you can use ‘outputs.upToDateWhen { false }’ or the ‘–rerun-tasks’ command line option.