Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the time of start of a gradle build

Tags:

gradle

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.

like image 877
Artur Nowak Avatar asked Aug 11 '11 13:08

Artur Nowak


People also ask

How much time does Gradle build takes?

gradle, if you have minifyEnabled for debug, remove it. I had it in my project and it took ~2-3 minutes to build. After looking at build progress, I found this as culprit, so deleting the commented line below, my issue was fixed.

How do I check my Gradle build?

In the command prompt, enter Gradle -version. It will display the current version of Gradle just installed on the screen.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is afterEvaluate in Gradle?

> gradle -q test Adding test task to project ':project-a' Running tests for project ':project-a' This example uses method Project. afterEvaluate() to add a closure which is executed after the project is evaluated. It is also possible to receive notifications when any project is evaluated.


2 Answers

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
like image 112
Peter Niederwieser Avatar answered Nov 15 '22 09:11

Peter Niederwieser


I've managed to retrieve the build time clock with following code:

Clock clock = project.gradle.services.get(BuildRequestMetaData.class).buildTimeClock
Date startDate = new Date(clock.getStartTime())

Still, it's far from being ideal, since it uses Gradle internal API: getServices() method is defined in org.gradle.api.internal.GradleInternal interface.

like image 34
Artur Nowak Avatar answered Nov 15 '22 09:11

Artur Nowak