It's a bit complicated but here's a bash script that will parse the properties element from a pom.xml, check if each property is used in the pom, and optionally checks all project files (a deep search).
#!/bin/bash
cmd=$(basename $0)
read_dom () {
local IFS=\>
read -d \< entity content
local retval=$?
tag=${entity%% *}
attr=${entity#* }
return $retval
}
parse_dom () {
# uncomment this line to access element attributes as variables
#eval local $attr
if [[ $tag = "!--" ]]; then # !-- is a comment
return
elif [[ $tag = "properties" ]]; then
in=true
elif [[ $tag = "/properties" ]]; then
in=
elif [[ "$in" && $tag != /* ]]; then #does not start with slash */
echo $tag
fi
}
unused_terms () {
file=$1
while read p; do
grep -m 1 -qe "\${$p}" $file
if [[ $? == 1 ]]; then echo $p; fi
done
}
unused_terms_dir () {
dir=$1
while read p; do
unused_term_find $dir $p
done
}
unused_term_find () {
dir=$1
p=$2
echo -n "$p..."
find $dir -type f | xargs grep -m 1 -qe "\${$p}" 2> /dev/null
if [[ $? == 0 ]]; then
echo -ne "\r$(tput el)"
else
echo -e "\b\b\b "
fi
}
if [[ -z $1 ]]; then
echo "Usage: $cmd [-d] <pom-file>"
exit
fi
if [[ $1 == "-d" ]]; then
deep=true
shift
fi
file=$1
dir=$(dirname $1)
if [ $deep ]; then
while read_dom; do
parse_dom
done < $file | unused_terms $file | unused_terms_dir $dir
else
while read_dom; do
parse_dom
done < $file | unused_terms $file
fi
The script uses XML parsing code from this thread.
This script won't find any properties that are used by maven or maven plugins directly. It simply looks for the pattern ${property} within project files.
It works on my mac; your milage may vary.