How do I programmatically read build execution properties?

I’ve have need of properties such as ‘build start time’, ‘build duration’, etc. If I had started + finished, I could of course calculate the duration… or with start and duration, calculate finished.

At any rate… where can I find this data? I know Gradle keeps track of such things, as evidenced by this output from a normal build:

BUILD SUCCESSFUL

Total time: 13.094 secs

I’m not sure if Gradle supports such a mechanism by itself but you can always try and define two tasks:

def startTime
def endTime

task getStartTime {
    startTime = System.currentTimeMillis()
}

task printBuildData {
    println "${System.currentTimeMillis() - startTime}" //for duration
    println "$startTime" //when started
    println "${System.currentTimeMillis()}" //when ended
}

Just setup your task dependencies so that getStartTime runs as the very first and printBuildData runs as the very last task. This should get you what you need. Please keep in mind that the output is in milliseconds so you will need to do some formatting to get a more human-recognizable output :slight_smile:

There are listeners you can hook into:
https://docs.gradle.org/current/javadoc/org/gradle/BuildListener.html

The start time/end time isn’t exposed.

Just FYI, your printBuildData would show the elapsed time between configuring getStartTime and configuring printBuildData. It wouldn’t include task execution or when the build actually finished.

Gradle has plenty of support for this type of info… try running a build with “–profile” command line switch.

Gradle creates a nicely formatted HTML page (in build/reports/profile) telling you all sorts of timing info, similar to this Summary (plus 3 more tabs of info):

Profile report

Profiled build: clean build uploadArchives

Started on: 2016/04/13 - 08:43:44


Description            Duration
-----------            --------
Total Build Time        15.378s
Startup            2.073s
Settings and BuildSrc    8.862s
Loading Projects        0.266s
Configuring Projects    1.492s
Task Execution        2.203s

I was hoping there’s a way to get to the data used to generate that page without forcing everyone to use --profile, especially since I need to get to it in a plugin… even if I could force --profile, I would then have to parse the HTML to get what I need. :-\

Yes, there’s an internal API for getting that same information (ProfileListener). You can see what Nebula does to collect that here: https://github.com/nebula-plugins/gradle-metrics-plugin