gradle

Get the time of start of a gradle build


How to get a Date object representing the time of start of the build process in Gradle?

Some background

In a plugin I am writing, I need to store the date and time of the build in the constructed archive (it's mainly used to display the date along with build number in the title bar of the application, to make QA life easier).

I want to have the same date for multiple subprojects that are being built, so ideally I would like to just use the start of build. As a workaround, I hold the date in a static field, but it hurts my eyes. I've seen in the source that there are multiple org.gradle.util.Clock instances that hold such information, but I cannot find a way to retrieve one from a plugin.


Solution

  • Your plugin could just set/get a dynamic property on the root project:

    def root = project.rootProject
    if (!root.hasProperty("startTime")) {
        root.startTime = new Date()
    }
    // do whatever necessary with root.startTime