Skip to content

Instantly share code, notes, and snippets.

@jwheeler31
Last active May 13, 2019 13:59
Show Gist options
  • Select an option

  • Save jwheeler31/6d4de72449e9ac8297250d4bf58766b5 to your computer and use it in GitHub Desktop.

Select an option

Save jwheeler31/6d4de72449e9ac8297250d4bf58766b5 to your computer and use it in GitHub Desktop.
Naive Gradle replacement for Ant <uptodate/> task
/**
* Checks if the file at <tt>targetFilePath</tt> has a last modified time later
* than <tt>srcFilePath</tt>.
*
* At the time of writing, the goal of this method was to create a replacement for
* Apache Ant's <tt>uptodate</tt> task. This is a naive implementation of that
* task.
*
* @param srcFilePath fully qualified path to source file
* @param targetPath fully qualified path to the target
*
* @return <tt>true</tt> if File#lastModified() of target >= File#lastModified() of source
*
* @see https://ant.apache.org/manual/Tasks/uptodate.html
* @see https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/taskdefs/UpToDate.java
*/
boolean upToDate(String srcFilePath, String targetFilePath) {
File srcFile = file(srcFilePath)
File targetFile = file(targetFilePath)
boolean current = targetFile.lastModified() >= srcFile.lastModified()
// Log results
if (current) { // target is newer
logger.quiet("File:[${targetFile.getName()}] is newer than [${srcFile.getName()}]")
}
else { // target is older
if (!srcFile.exists()) {
logger.quiet("Source file:[${srcFile.exists()}] does not exist")
}
if (!targetFile.exists()) {
logger.quiet("Target file:[${targetFile.getName()}] does not exist")
}
if (srcFile.exists() && targetFile.exists()) {
logger.quiet("File:[${srcFile.getName()}] is newer than [${targetFile.getName()}]")
}
}
return current
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment