Get duration of a task in java

Hey there,

I’m using the gradle API org.gradle.tooling in Java and I would like to be able to get the duration of each task of a build. Is there any way to do so?

thanks

So since no one had an answer for me, I finally figured it out by myself. In case anyone else needs this, here’s my solution, might be not the best one but it works for me. Just add this Listener to your BuildLauncher:

class Listener implements ProgressListener{
	List<String> tasks = null;
	String task;
	String current;
	long diff;
	
	@Override
	public void statusChanged(ProgressEvent event) {
		if(event instanceof TaskProgressEvent) {
		        current = event.getDescriptor().toString().replace("Task :", "");

			if(event instanceof TaskFinishEvent) {
			diff = event.getEventTime() - diff;
			System.out.printf( "%-40s%-22s", "Name: " + event.getDescriptor().toString().replace("Task :", ""), "Duration: " + calcDiff(diff));
			}else {
				diff = event.getEventTime();
			}
		}
}	
	
	public String calcDiff(long diff) {
		if(diff < 1000) {
			return (diff + " ms");
		} else if((1000 <= diff) && (diff < 60000)){
			double i = (double) diff / 1000;
			return(i  + " s");
		} else {
			double i = ((double)diff / 1000) / 60;
			i = round(i, 4);
			return(i  + " min");
		}
	}