Gradle: get folder from which “gradle” was execute

2019-04-03 04:55发布

问题:

I am trying to use gradle tasks as kind of "OS agnostic shell-script". I have no problems in writing the logic of the build file, but I would like to be able to run it from any folder.

My first attempt was to put a folder with my build.gradle file on the path, and then try to execute:

gradle myMask

from a folder that doesn't contain build.gradle - but that doesn't work.

My next attempt was:

gradle -b /folder/containing/build.gradle myTask

But that worked only to some extend. In my task I would like to find all files in CURRENT DIRECTORY. current - meaning not the one that contains build.gradle but the one from within which I am executing "gradle ....."

I have tried:

file(".")

and

file("$projectDir")

and some more, but all of them point to the folder containing build script, no the one from which I am executing it.

Any ideas how can I do that?

回答1:

and the winner is:

System.getProperty("user.dir")


回答2:

file(".") always returns the current project folder which isnt necessarily and not the working directory. To reference the working dir you can use:

new File(".").absolutePath

hope that helps!

René



标签: gradle