Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top