Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle global build directory

Tags:

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 Drive folder (for backup and fast access from other devices) and when I build project Google Drive is loading CPU while syncing project build directory. So I want to move build directories outside of Google Drive.

Also this option will be useful for those who want to build projects using RAM disk as storage for build directory.

like image 642
mixel Avatar asked Apr 28 '14 12:04

mixel


People also ask

Where is the build directory in gradle?

get global build path from environment variable or fallback to build directory inside project directory; include project group in path, this makes path more unique especially if you have several projects with the same name.

What is .gradle directory?

The Gradle user home directory ( $USER_HOME/.gradle by default) is used to store global configuration properties and initialization scripts as well as caches and log files.

What is gradle service directory path?

The Service Directory Path is the default Gradle Home directory. This is where the cache is maintained.


2 Answers

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:

  1. get global build path from environment variable or fallback to build directory inside project directory;
  2. include project group in path, this makes path more unique especially if you have several projects with the same name.

~/.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 } 
like image 105
mixel Avatar answered Sep 20 '22 17:09

mixel


build.gradle:

def baseDir = "/global/build/dir/$project.name" buildDir = baseDir + "/root" subprojects {     buildDir = baseDir + project.path.replaceFirst(":", "/").replace(":", ".") } 
like image 41
Peter Niederwieser Avatar answered Sep 20 '22 17:09

Peter Niederwieser