Function from init.gradle in build script

in my init.gradle i have

... // the last thing in init.gradle def buildTime() { def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") //you can change it df.setTimeZone(TimeZone.getTimeZone("UTC")) return df.format(new Date()) } 

In my build.gradle, I want to do something like this:

 task showTime() << { println buildTime() } 

But I get "Could not find the buildTime () method for arguments [] in the root project ..."

thanks in advance!

+8
gradle
source share
1 answer

Got a response from Gradle -Support. http://goo.gl/5uYInH

Maybe this helps someone else ...

The initialization file is a different context than the build.gradle file. But you can extend the project object (build.gradle delegates to) with a custom property or method (using closure):

init.gradle

 import java.text.SimpleDateFormat gradle.allprojects{ ext.buildTime = { def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") df.setTimeZone(TimeZone.getTimeZone("UTC")) return df.format(new Date()) } } 

build.gradle

 task showBuildTime() << { println buildTime() } 
+6
source share

All Articles