Execute gradle build task from any location
Asked Answered
A

3

15

I´m trying to build my gradle projects from other locations than the project folder itself, but it always says it couldn´t find build task.

What I´ve tried so far:

sudo ./myprojects/myapp/gradlew build

sudo ./myprojects/myapp/gradlew ./myprojects/myapp/build

How can I execute a gradle build task from any location?

Anaesthesia answered 3/9, 2014 at 7:44 Comment(1)
You should not need to run a build with sudo.Cheesy
L
18

Various people have written (and published) scripts to execute gradlew from any subproject directory (in a multi-project build). To reliably execute Gradle from any subdirectory, it is necessary to set the "current project directory" via -p. It would be nice to have this restriction lifted (this would make a good feature request).

Leblanc answered 3/9, 2014 at 8:7 Comment(5)
Thank you, with sudo ./myproject/myapp/gradlew -p /myproject/myapp/build it works ;) are there any reasons for this restrictions? It doesn´t make sense in my eyesLuis
Gradle needs to know what the "current project directory" is. Currently, the "current project directory" defaults to the current working directory. Unless I'm missing something, it should be possible to have it default to "nearest subproject directory" instead. However, this needs to be done inside Gradle, because only Gradle knows what the subproject directories are.Leblanc
Most often I run tasks from a shell that has current directory in root project using commands like ./gradlew :myapp:test.Cheesy
Okay, my mistake, it´s not working. After I execute the command, "Welcome to gradle..." is prompted with "Build successful"Luis
solved it now by using "cd /my/project/path/;./gradlew build"Luis
C
3
./usmobile-microservice/gradlew -p ./usmobile-microservice clean buildUI

./project_directory/gradlew -p ./project_directory clean build

worked for me

Croissant answered 22/7, 2020 at 21:4 Comment(0)
K
2

You may try this script, which is 90-lines long: https://github.com/dougborg/gdub

Or use this straightforward one-liner I use myself:

function lookupgradle() { 
  find . .. ../.. ../../.. ../../../.. ../../../../.. ../../../../../.. -maxdepth 1 -name 'gradlew' -executable -print -quit
}

alias g='$(lookupgradle)'

If you'll find out that it is still required to specify project directory, add -p .:

alias g='$(lookupgradle) -p .'
Karmakarmadharaya answered 4/12, 2019 at 17:46 Comment(1)
gng.dsun.org is the successor of gdub. I am actively working on this. The original author of gdub have transfered this project to me. Thanks.Process

© 2022 - 2024 — McMap. All rights reserved.