Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - make use of RAMdisk

Tags:

gradle

ramdisk

I just run into idea of using RAMdisk for compilation results 1

How to use RAMdisk with Gradle?

I guess it is worth moving .gradle and build folders into RAMdisk.

like image 244
Paul Verest Avatar asked Mar 18 '15 07:03

Paul Verest


2 Answers

Just for completeness, here's how I configured Gradle (and thereby also Android Studio) on Ubuntu 14.04 to always build to RAM disk:

My ~/.bashrc contains this line in the end:

. ~/bin/mkramdisk # Setup personal RAM disk on login.

My ~/bin/mkramdisk is listed below. I suppose you could omit this script and simply use e.g. /dev/shm/${System.env.USER}/gradle-buildsin the following step, but I like having a general RAM disk for other purposes as well, for instance I use it for my $TMP so here goes:

# Setup personal RAM disk.

# This script should be sourced, hence the missing +x flag.
# Source it from e.g. from ~/.bashrc or run it from crontab
# at @reboot event (doesn't work with encrypted homedir btw.)

export RAMDISK=$HOME/tmp/ramdisk
if [ ! -d $RAMDISK ]; then
        [ -d /dev/shm/$USER-ramdisk ] || install -vd /dev/shm/$USER-ramdisk -o $USER -m 700
        [ -d ~/tmp ] || mkdir -v ~/tmp
        [ -L ~/tmp/ramdisk ] || ln -vs /dev/shm/$USER-ramdisk ~/tmp/ramdisk
fi

# Use personal RAM disk for $TMP.
export TMP=$RAMDISK

NOTE to Macintosh users: It seems you can modify mkramdisk to instead contain this command to make it work on your system.

To finally answer the question, my ~/.gradle/init.gradle is this:

println "Loaded personal ~/.gradle/init.gradle"

gradle.projectsLoaded {
    rootProject.allprojects {
        buildDir = "${System.env.RAMDISK}/gradle-build/${rootProject.name}/${project.name}"
        println "BUILDING TO RAMDISK: buildDir=$buildDir"
    }
}

Remove debug println statements as you see fit.

like image 199
Stephan Henningsen Avatar answered Sep 22 '22 14:09

Stephan Henningsen


in root build.gradle

allprojects {
    buildDir = "/path/to/build/${rootProject.name}/${project.name}"
}

See also

  • Gradle global build directory
  • Change gradle build directory in android studio?
  • Change Gradle's working directory when compiling a Groovy project

and docs https://gradle.org/docs/current/userguide/writing_build_scripts.html

like image 30
Paul Verest Avatar answered Sep 23 '22 14:09

Paul Verest