(Maven) Building to a ramdrive

I own a MacBook Pro and I have a few projects with Maven (and MevenIDE on NetBeans).

What's annoying me with the build system is that it usually writes a lot to the disk. Not only is that quite unnecessary (as the files will be overridden in no time) but the laptop hard-disk is also quite slow and all this writing is trashing it.

The solution: write to a ramdrive ! As you all know, a ramdrive is a "virtual" hard-disk that sits in your RAM and goes away when you shutdown the machine (not that I care, the build files are temporary).

First step: create the ramdisk

There are some utilities that do this, but it's quite doable from the terminal (eventually with a shellscript).

  1. First, get your disksize in MB and multiply it by 2048. So 256MB means 524288.
  2. Next, create the device: hdik -nomount ram://524288 The command will also display the name of the new device file.
  3. Create a HFS filesystem in there: newfs_hfs -v "disk name"/dev/diskXXX , where diskXXX is whatever the previous command printed
  4. Mount the filesystem: mkdir /Volumes/diskXXX &&
    diskutil mount /dev/diskXXX
You'll probably need to run some of these commands as root (su admin-user, sudo sh). I also set my non-admin user as owner with chown -R myUser:myUser /Volumes/diskXXX

At this point you should have a new 256MB drive mounted.

Second step: link maven folders


Now, I have to set the "target" folders on the ramdisk. Normally the orthodox way is to change the pom but I just didn't get it working. So my old-school solution is to use symbolic links.

This could be smarter as a "mvn clean" will remove the links we just create (but just keep a script in place that recreates this).

My script is:

for i in *; do
echo $i;
mkdir -p "$1/$i/target";
ln -s "$1/$i/target" "$i/target";
done

and I run it in the folder that keeps all my Maven projects (a flat hierarchy). Note the $1 which is the argument to the script. I use it like this:

$./linkramdisk.sh /Volumes/diskXXX

Building

All the links are in place so you can try a mvn install and see how fast it works. I my case, I reduced the build time (with no unit-tests) from 27 seconds to 17 seconds.

That doesn't seem much, but it does add up for larger projects and most importantly, it keeps the hard-drive out of the loop.

Oh, did I mention I also use FileVault on my account ? That's another reason one would like to avoid writing to disk: no need to encrypt something useless like a build artifact...