Skip to content

Instantly share code, notes, and snippets.

@neg3ntropy
Last active August 29, 2015 14:21
Show Gist options
  • Save neg3ntropy/f9a967cfd4b3bb5f603f to your computer and use it in GitHub Desktop.
Save neg3ntropy/f9a967cfd4b3bb5f603f to your computer and use it in GitHub Desktop.
Mavenizer script
#!/bin/bash
DEFAULT_JAR_FOLDER='src/main/webapp/WEB-INF/lib'
DEFAULT_JAR_PATTERN='*.jar'
DELETE=no
HELP="Usage: $(basename "$0") [--delete] [jar folder/file] [pattern]
Looks up jar files on Maven central using their hash and outputs dependency
elements for the pom.xml.
Options:
--delete deletes the jar when found
--help shows this message
Defaults:
folder: ${DEFAULT_JAR_FOLDER}
pattern: ${DEFAULT_JAR_PATTERN}
"
function list {
find "${1:-$DEFAULT_JAR_FOLDER}" -iname "${2:-$DEFAULT_JAR_PATTERN}"
}
function search {
local jar sha1 url found
while read jar; do
sha1="$(sha1sum $jar | cut -f 1 -d ' ')"
url='http://search.maven.org/solrsearch/select?q=1%3A%22'$sha1'%22&rows=20&wt=json'
found=$(wget -q "$url" -O - | jq .response.docs[0].id | jq -e -r 'select(type == "string")')
if [ $? == 0 ]; then
echo "$found"
if [ "$DELETE" = yes ]; then
rm "$jar"
fi
fi
done
}
function toXml {
local found mvnid
while read found; do
IFS=':' read -ra mvnid <<< "$found"
echo ' <dependency>'
echo " <groupId>${mvnid[0]}</groupId>"
echo " <artifactId>${mvnid[1]}</artifactId>"
echo " <version>${mvnid[2]}</version>"
echo ' </dependency>'
done
}
function main {
list $1 $2 | search | sort | toXml
}
# parse arguments
OPTS=$(getopt -o "" -l delete -l help -- "$@") || (echo "$HELP" >&2; exit 1)
eval set -- "$OPTS"
while [ $1 != "--" ]; do
case "$1" in
--delete)
DELETE='yes'
;;
--help)
echo "$HELP"
exit 0
;;
esac
shift
done
shift # eat "--"
[ -r "${1-DEFAULT_JAR_FOLDER}" ] || (echo "Error: path not readable: $1" >&2; exit 1)
main ${@}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment