Reputation:
I need create file in my java-project near buiid.gradle-file. I must create task (Groovy task) in build.gradle-file, my task must create file near buiid.gradle in my project, but I do not know - how do get path to buiid.gradle-file, that is putting in my project.
How get full path to file buiid.gradle by Groovy? Help me, please.
Upvotes: 9
Views: 18709
Reputation: 1202
To get a handle to the java.io.File that represents the currently-executing .gradle file:
def file = buildscript.sourceFile
In my case, I needed the directory of that script so I could apply other configuration from the same directory:
def buildCommonDir = buildscript.sourceFile.getParent()
apply from: "$buildCommonDir/common.gradle"
This approach works with any .gradle file, not just build.gradle.
Upvotes: 8
Reputation: 10322
There are several ways to accomplish this.
file()
method that is a part of the Project
object.projectDir
property. Thes properties are available throughout the build.gradle
script.They can be used, respectively, like this:
task myTask << {
println file('.')
println projectDir
}
Would output
/full/path/to/your/project/ /full/path/to/your/project/
Where that directory contains the build.gradle
file where the task is located.
Upvotes: 21