Skip to content

Instantly share code, notes, and snippets.

@neilellis
Last active May 26, 2020 13:09
Show Gist options
  • Save neilellis/5171713 to your computer and use it in GitHub Desktop.
Save neilellis/5171713 to your computer and use it in GitHub Desktop.
Build large maven projects quicker on OS X, this gist will ensure a ramdisk is mounted in /Volumes/ramdisk and triggers a profile that redirects output to a ram disk. It also only builds modules which have changed and their dependent modules. This is done by supplying the target directory of the ultimate artifact you are interested in.

Make sure you have a profile called quick that redefines the location of maven's output to the ramdisk. To do this ensure all of the below are in your parent POM.

<properties><my.build.directory>target</my.build.directory></properties>

<build><directory>${my.build.directory}</directory></build

<profile>
  <id>quick</id>
  <properties>
    <my.build.directory>/Volumes/ramdisk/maven/${project.groupId}-${project.artifactId}/target</my.build.directory>
  </properties>
</profile>

The ramdisk is set to be a gig in size btw.

Always run from the top level POM's directory.

qmvn (<final-artifact-target-directory> | -d ) <maven-lifecycle>+

E.g. Supplying your final artifacts target directory on the ramdisk:

qmvn /Volumes/ramdisk/maven/cazcade-boardcast/target install

To just build everything make sure the first argument is not a valid directory e.g.

qmvn pom.xml install

To use the default directory (currently manually hacked into the script)

qmvn -d install
#!/bin/sh
# Make sure you have a profile called quick that redefines the location of maven's output to the ramdisk.
# To do this ensure all of the below are in your parent POM.
#
# <properties><my.build.directory>target</my.build.directory></properties>
#
# <build><directory>${my.build.directory}</directory></build
#
# <profile>
# <id>quick</id>
# <properties>
# <my.build.directory>/Volumes/ramdisk/maven/${project.groupId}-${project.artifactId}/target</my.build.directory>
# </properties>
# </profile>
#
# The ramdisk is set to be a gig in size btw.
#
# Usage: qmvn <final-artifact-target-directory> <maven-lifecycle>+
#
DEFAULT_TARGET=/Volumes/ramdisk/maven/cazcade-boardcast/target
if [ $1 == "-d" ]
then
TARGET=$DEFAULT_TARGET
else
TARGET=$1
fi
shift 1
if [ -d /Volumes/ramdisk ]
then
echo "Ramdisk already built."
else
diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://2097152`
fi
function newer() {
find . -name pom.xml -exec dirname {} \; | while read dir
do
if [ -d $TARGET ]
then
if [ -n "`find $dir/src $dir/pom.xml -newer $TARGET`" ]
then
echo $dir/pom.xml
fi
else
echo $dir
fi
done
}
projects=$(newer | sort -u | tr '\n' ',')
if [ -n "$projects" ]
then
mvn -Dmaven.test.skip --projects $projects --also-make-dependents $@ -P quick
else
echo "Up to date."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment