I want to know is it possible to configure Gradle to use one global build
directory for all projects? I need this because I store all my projects in Google Driv
Thanks to Peter Niederwieser. I decided to post my own answer because of additional details.
To setup global build directory you need to add these lines to ~/.gradle/init.gradle:
gradle.projectsLoaded {
rootProject.allprojects {
buildDir = "/path/to/build/${rootProject.name}/${project.name}"
}
}
Nice option to have global build
directory on the RAM disk.
If you are macOS (OS X) user you can do this with:
diskutil erasevolume HFS+ 'RAMDiskName' `hdiutil attach -nomount ram://XXXXXX`
where XXXXXX
is a count of 512-byte blocks (for example, it's 262114 for 128MB RAM disk)
And buildDir
line will be:
buildDir = "/Volumes/RAMDiskName/${rootProject.name}/${project.name}"
Also you can extend configuration to:
build
directory inside project directory;~/.gradle/init.gradle:
def configProject(p, buildDir) {
p.buildDir = "${buildDir}/${p.name}"
p.subprojects { s ->
configProject(s, "${p.buildDir}")
}
}
def buildDir = System.env.GRADLE_GLOBAL_BUILD_PATH
if (!buildDir?.trim()) {
buildDir = "build"
}
gradle.projectsLoaded {
if (ext.has("group")) {
buildDir += "/${ext.group}"
}
configProject(rootProject, buildDir)
}
And in project settings.gradle:
gradle.ext.group = 'com.example.yourproject'
Also you can use this setting in project build.gradle to set project group but this is optional:
allprojects {
...
group gradle.ext.group
}
You could also go the opposite way. Replace the build
folder with a global folder.
build
foldermklink /d ...disk\path\to\project\build ...RAM\path\to\project
(or ln
in Unix)You can automate this in the clean
task, so a gradlew clean
cleans the folder contents and recreates the link.
With some quick testing it seems that Google Drive doesn't recognize directory in junction links on Windows. It'll sync the folders, but not the contents. Weirdly if I upload files to Drive into those folders, they're synced into the local copy. But, if I delete the synced file from the disk, the deletion is not synced back. It seems links cause Drive to be one way.
build.gradle:
def baseDir = "/global/build/dir/$project.name"
buildDir = baseDir + "/root"
subprojects {
buildDir = baseDir + project.path.replaceFirst(":", "/").replace(":", ".")
}