How get full path to file build.gradle by Groovy?

瘦欲@ 提交于 2021-01-22 04:28:22

问题


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.

  1. If you look at the Working With Files page, you can simply use the file() method that is a part of the Project object.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!