Skip to content

Instantly share code, notes, and snippets.

@rnorth
Last active February 1, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnorth/07eb82d86fc88f4f913a to your computer and use it in GitHub Desktop.
Save rnorth/07eb82d86fc88f4f913a to your computer and use it in GitHub Desktop.
qmvn
#!/usr/bin/env bash
# qmvn (quick maven)
#
# Maven wrapper script to accelerate the build of multi-module projects
# This script detects which modules have been modified since the last build, and executes maven in `-amd -pl ...` mode.
# This means only modules with changes to source code or their POMs, or which depend on a changed module, get built.
# See this page for details of how these flags accelerate the build:
# http://blog.sonatype.com/2009/10/maven-tips-and-tricks-advanced-reactor-options/
#
# Authors:
# - Richard North (https://rnorth.org)
#
# Usage:
# qmvn <goal>, e.g. ./qmvn install
#
# Licence:
# The MIT License (MIT)
#
# Copyright (c) 2016 Richard North <rich.north@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -o errexit
set -o nounset
set -o pipefail
#set -x
# Discover all modules in the project - only done once
if [[ ! -f .qmvn.modules ]]; then
echo "Discovering modules..."
mvn exec:exec -Dexec.executable=echo -Dexec.args="qmvn_module: \${basedir}" | grep "^qmvn_module:" | cut -d\ -f2 | sed "s#${PWD}##" | sed -e "s/^/./" > .qmvn.modules
fi
MODULES=($(cat .qmvn.modules ))
# Array to collect which modules have changed
CHANGED_MODULES=()
# We need to known when the maven command (args to this script) were last run
COMMAND_HASH=$(echo $@ | md5)
# JVM options to make maven run more quickly
export MAVEN_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
# Iterate over all the modules to detect changes
for MODULE in ${MODULES[@]}; do
GUARDFILE="${MODULE}/target/.lastchange.${COMMAND_HASH}"
if [[ ! -f ${GUARDFILE} ]]; then
mkdir -p ${GUARDFILE} && rm -rf ${GUARDFILE}
touch -t "$(date -r 0 +%Y%m%d%H%M.%S)" ${GUARDFILE}
fi
# Specifically ignore changes the to target and src/generated directories
CHANGES=$(find ${MODULE} -newer ${GUARDFILE} \
\( -path "${MODULE}/src/*" -or \
-wholename "${MODULE}/pom.xml" \) -and \
-not -path "${MODULE}/target/*" -prune -and \
-not -path "${MODULE}/src/generated/*" -prune -and \
-not -name "\.*" -prune
)
CHANGE_COUNT=$(echo $CHANGES | sed "/^\s*$/d" | wc -l | xargs)
if [[ $CHANGE_COUNT > 0 ]]; then
echo "$MODULE has change(s) since the last invocation of mvn $@"
echo " Changes:"
echo ${CHANGES} | sed -e "s/^/ /"
CHANGED_MODULES+=($MODULE)
fi
done
# Exit right away if nothing has changed
if [[ ${#CHANGED_MODULES[@]} == 0 ]]; then
echo "No changes"
exit 0
fi
# Create a comma-separated list of changed modules
function join { local IFS="$1"; shift; echo "$*"; }
PL_LIST=$(join "," ${CHANGED_MODULES[@]})
# Execute maven such that just the changed modules, and modules that depend on them, get built
mvn $@ -pl ${PL_LIST} -amd
# Finally, assuming successful invocation of maven, update our guardfiles so that future changes are detected from now onwards
for MODULE in ${MODULES[@]}; do
GUARDFILE="${MODULE}/target/.lastchange.${COMMAND_HASH}"
touch ${GUARDFILE}
done
@outofcoffee
Copy link

Nice! Does this work if you change your maven args between builds?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment