'How to find gradle subproject tasks from different working directory

I'm running into the problem that my gradle wrapper will only find subprojects if I execute it whilst being in the same working directory. For example:

Let's say the project structure is as follows:

.
├── app
│   ├── build.gradle
│   ├── ...
├── build.gradle
├── gradlew
├── settings.gradle
└── ...

It makes a difference whether I run gradlew from it's directory or from a different directory. If I run:

$ ./gradlew projects

> Task :projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'com.name'
+--- Project ':app'

it has no problem finding :app. However, if I navigate and execute gradlew from a folder up, it cannot find it:

$ cd ..
$ ./android/gradlew projects

> Task :projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'com'
No sub-projects

It can't find the projects. This is problematic for me since I need to run a task in :app from a pipeline from a different working directory, e.g. ./xx/yy/gradlew app:publishTask. However doing it this way, gradle can't find the task because it can't find the project. Is there a way to run these commands from any location?



Solution 1:[1]

Yes, it is. You have to:

  1. store your current location in an temporary variable
  2. change location to project directory
  3. run ./gradlew
  4. restore directory from variable

ex:

TMP_DIR=`pwd`
cd /path/to/project
./gradlew projects
cd $TMP_DIR

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marioc64