Skip to content

Instantly share code, notes, and snippets.

@fbiville
Last active August 29, 2015 14:00
Show Gist options
  • Save fbiville/11310348 to your computer and use it in GitHub Desktop.
Save fbiville/11310348 to your computer and use it in GitHub Desktop.
version_is : quick Git-managed Maven version checker
#!/bin/bash
__git_project_root_directory() {
echo $(git rev-parse --show-toplevel 2>&1 | grep -v "^fatal")
}
__xml_parser() {
if [[ $OSTYPE == msys ]]; then
echo "xml"
else
echo "xmlstarlet"
fi
}
__pom_version() {
cmd=`__xml_parser`
# simple case
version=$($cmd sel -N ns=http://maven.apache.org/POM/4.0.0 -t -m "/ns:project/ns:version/text()" -c . -n "$1" 2> /dev/null)
if [[ $version == "" ]]; then
# parent POM
echo $($cmd sel -N ns=http://maven.apache.org/POM/4.0.0 -t -m "/ns:project/ns:parent/ns:version/text()" -c . -n "$1" 2> /dev/null)
else
echo $version
fi
}
__check_dependency() {
command -v $1 >/dev/null 2>&1 || { echo >&2 ERROR "${1} is required but is not installed. Aborting."; exit 1;}
}

version_is

Pitch

version_is detects whether the current dir POM (or the whole Git project POM files) match the specified version.

This can be useful as a sanity check after a merge (where POM versions could have silently changed to a non-desired value).

Setup

Just export version_is directory into your PATH. The script automatically detects if you miss some dependencies, so you will quite quickly notice when you need to install one ;-)

Getting started

Easy as:

   $> cd /path/to/git/versioned/maven/project
   $> version_is --help

Global check

For example, suppose we want to check that all POM versions of a specific Git project match 2014.07.2, just run:

   $> cd /path/to/git/versioned/maven/project
   $> version_is 2014.07.2 everywhere

Please note this works from any subdirectory within the same Git project!

Local check

Following the same example, but for a specific pom.xml (located under a/b/c subdirectory).

   $> cd /path/to/git/versioned/maven/project/a/b/c
   $> version_is 2014.07.2
#!/bin/bash
shopt -u sourcepath
script_dir=$(dirname "$0")
source "$script_dir"/__functions.sh
command_name=$(basename "$0")
usage="
\033[1mNAME\033[m
$command_name - check Maven version(s) of a Git-versioned project
\033[1mSYNOPSIS\033[m
$command_name [OPTIONS]
$command_name VERSION_NUMBER
$command_name VERSION_NUMBER everywhere
\033[1mDESCRIPTION\033[m
Determines whether the current directory's \033[4mpom.xml\033[m (or all Git project's \033[4mpom.xml\033[m files, if \033[7meverywhere\033[m is specified) define the provided \033[7mVERSION_NUMBER\033[m
\033[1m-h\033[m, \033[1m--help\033[m
displays this help
\033[1mExit status\033[m:
0 if OK,
any other int if problems
"
### explicit usage display
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo -ne "$usage"
exit 0
fi
### dep sanity checks
__check_dependency git
__check_dependency $(__xml_parser)
### argument parsing & validation
expected_version=$(echo "$1" | sed 's/ *$//')
if [[ -z $expected_version ]]; then
echo -ne "$usage" >&2
exit 1
fi
### actual stuff ;)
if [[ "$2" == "everywhere" ]]; then
git_root=`__git_project_root_directory`
pom_files=$(find "$git_root" -name pom.xml)
else
pom_files=$(ls -1 pom.xml 2> /dev/null)
fi
if [ ${#pom_files[@]} -eq 0 ]; then
echo -ne "No pom.xml found. Aborting..." >&2
exit 2
fi
offending_pom_files=()
for pom in $pom_files; do
actual_version=`__pom_version $pom`
if [[ $actual_version != $expected_version ]]; then
shortened_pom=${pom/$git_root\//}
offending_pom_files="$offending_pom_files $shortened_pom->\033[4m$actual_version\033[m"
fi
done
if [ ${#offending_pom_files[@]} -eq 0 ]; then
echo -ne "\n \033[1;32mOK!\033[m All versions match \033[4m$expected_version\033[m!\n\n"
exit 0
else
echo -ne " \n \033[1;31mKO!\033[m Expected version \033[4m$expected_version\033[m, but got:\n\n" >&2
for error in $offending_pom_files; do
echo -ne " * ${error}\n" >&2
done
echo >&2
exit 3
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment