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 method buildTime() for arguments [] on root project..."
Thx in advance!
Right click on "init" and select "Run Gradle Tasks". Then in the project explorer, right click on your project and select "Gradle", then select "Refresh Gradle Project". And voila! You must be able to see your build.
Gradle comes with a built-in task, called init , that initializes a new Gradle project in an empty folder. The init task uses the (also built-in) wrapper task to create a Gradle wrapper script, gradlew . The first step is to create a folder for the new project and change directory into it.
You can use -x option or --exclude-task switch to exclude task from task graph. But it's good idea to provide runnable example. gradle taskA -x taskB will run both the tasks.
Got the answer from Gradle-Support. http://goo.gl/5uYInH
Maybe it helps someone else...
The init file is a different context than the build.gradle file. But you can extend a project object (build.gradle delegates to) with a custom property or method (using a 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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With