How to include WAR build timestamp in project

I want to include time of build in properties file that is present inside the source code. I currently do it manually by setting current date and time in properties file before WAR generation,

propertyFile.withWriter { props.store(it, null) }
'buildDir = new File(rootProject.projectDir, "gradleBuild/" + project.name)'

Though, there occurs some time difference, say 10:02 AM in properties file and the WAR generated time is 10:03 AM

How to resolve this and is there any method to achieve this?

Writing the file with the current timestamp takes some amount of time. Writing a WAR file takes some amount of time. Both can be very small, but there is no way that a WAR file can contain a file that is guaranteed to be the time that the WAR file was created. The elapsed time, even at the smallest measurable increment could cross the minute boundary.

That said, considering information from your other posts, you’re generating the timestamp as soon as the build is configured, not as part of a task action that executes right before the WAR is created. If your trying to tack the property file writing on to the war task, the code inside the war block should also be surrounded with doFirst { } so that it doesn’t run until the war task is running. However, it would be much better if you split this out to a task that only handles the property file or used a filter when the resources are copied as messing with a source file in place isn’t good for UP-TO-DATE checking.

I have changed as,

war {
	doFirst {
	def propertyFile = file "src/main/java/com/about/About.properties"
	def props = new Properties()
	propertyFile.withReader { props.load(it) }
	println "Before setProperty:"
	println props.getProperty('releaseDate')
	props.setProperty('releaseDate', new Date().format('yyyy-MM-dd-HH:mm:ss'))
    propertyFile.withWriter { props.store(it, null) }
	println "After setProperty:"
	println props.getProperty('releaseDate')
	buildDir = new File(rootProject.projectDir, "gradleBuild/" + project.name)
	}
}

This works. When I separate the property file accessing as task, say,

task(timeStamp) {
doFirst {
	def propertyFile = file "src/main/java/com/about/About.properties"
	def props = new Properties()
        ...
	println props.getProperty('releaseDate')
	}
}

This seems to be not working. Am I doing it wrong somewhere?