问题
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.
回答1:
There are several ways to accomplish this.
- If you look at the Working With Files page, you can simply use the
file()
method that is a part of theProject
object. - If you look at the Project DSL Docs, you should see the 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.
回答2:
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.
来源:https://stackoverflow.com/questions/23522198/how-get-full-path-to-file-build-gradle-by-groovy