Gradle: getting the root project directory path when starting with a custom build file

GradleGroovy

Gradle Problem Overview


The structure of my Gradle project is the following:

Project
├── app
     └── build.gradle
├── foo
     └── bar.txt
·
·
·
└── build.gradle

Normally to get the absolute path of the foo folder I can just simply do new File('foo').getAbsolutePath() in the root build.gradle file.

But this unfortunately doesn't work if you run the gradle script from outside the project directory, for example doing something like this:

$ trunk/gradlew -b trunk/build.gradle tasks

With the previous command gradle is looking for the foo directory in the parent of the Project, because I started the script from there.

Is there a way to get the absolute path of the Project where the build.gradle is, even if you start your script from another directory? Or is there any other way to get a reference of a directory in the same folder where the script is?

I've tried also with getClass().protectionDomain.codeSource.location.path but it is returning the path to the gradle cache directory.

Gradle Solutions


Solution 1 - Gradle

I got past this problem by ensuring Java userDir was set to the project directory (i.e. project.projectDir) at the top of my build.gradle file, as follows:

    System.setProperty( "user.dir", project.projectDir.toString() )
    println  "  project dir:  "+ System.getProperty("user.dir");

This can be checked by executing a separate (Groovy) code file such as:

    println "User Dir: ${System.getProperty( 'user.dir' )}"

You can output the Gradle project values before and after using these statements.

    println  "Root project:   ${project.rootProject}";
    println  "  rootDir:      ${project.rootDir}"
    println  "  projectDir:   ${project.projectDir}";
    println  "  project dir:  ${System.getProperty("user.dir")}";

If you have sub-projects, projectDir is not the same as rootDir.

This hasn't fixed my actual problem but it has ensured that I'm opening the correct file (relative to the location of build.gradle.

Solution 2 - Gradle

new File('foo') by definition (look at its JavaDoc) makes a path relative to the current working directory, so depends on where you call the app from. If you want a path relative to the project folder, use project.file('foo'), or as project is the default for resolving the method just file('foo') and you get the relative path resolved against the project directory, not the working directory. So use file('foo').absolutePath and you will be fine.

Solution 3 - Gradle

In the build.gradle file just use projectDir to get the absolute path of the build.gradle file. from there you can navigate your project's files. read this for more info:

https://www.tutorialspoint.com/gradle/gradle_build_script.htm

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoberto LeinardiView Question on Stackoverflow
Solution 1 - GradlewillView Answer on Stackoverflow
Solution 2 - GradleVampireView Answer on Stackoverflow
Solution 3 - GradleAliReza19330View Answer on Stackoverflow