[英]How get full path to file build.gradle by Groovy?
I need create file in my java-project near buiid.gradle-file. 我需要在buiid.gradle文件附近的java项目中创建文件。 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.
我必须在build.gradle文件中创建任务(Groovy任务),我的任务必须在我的项目中的buiid.gradle附近创建文件,但我不知道 - 如何获取buiid.gradle文件的路径,即放入我的项目。
How get full path to file buiid.gradle by Groovy? 如何通过Groovy获取文件buiid.gradle的完整路径? Help me, please.
请帮帮我。
There are several ways to accomplish this. 有几种方法可以实现这一目标。
file()
method that is a part of the Project
object. Project
对象一部分的file()
方法即可。 projectDir
property. projectDir
属性。 Thes properties are available throughout the build.gradle
script. build.gradle
脚本中都提供了这些属性。 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. 该目录包含任务所在的
build.gradle
文件。
To get a handle to the java.io.File that represents the currently-executing .gradle file: 要获取表示当前正在执行的.gradle文件的java.io.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. 此方法适用于任何 .gradle文件,而不仅仅是build.gradle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.