Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
echo "Starting POM script"
root_path="/path/to/your/modules"
pom_template="/path/to/your/pom_template.xml"
# Script assumes root path being the one where the artifact/modules folders are
if [ -d "$root_path" ]; then
echo "Path exists!"
for artifact_path in $root_path/*; do
artifactId="$(basename $artifact_path)"
echo $artifactId
for version_path in $artifact_path/*; do
version="$(basename $version_path)"
# Check if is version, just to filer out maven-metadata.xml
if [[ $version == *"."*"."* ]]; then
echo " $version"
for file_path in $version_path/*; do
filename="$(basename $version_path)"
# Find POM file
if [[ $file_path == *".pom" ]]; then
echo "Is POM fille"
pom_file=$file_path
# Get values to replace on placeholders
artifact=$(cat $pom_file | tr '\n' ' ' | sed -E 's/(<\/artifactId>).*/\1/g' | sed -E 's/.*(<artifactId>)/\1/g')
version=$(cat $pom_file | tr '\n' ' ' | sed -E 's/(<\/version>).*/\1/g' | sed -E 's/.*(<version>)/\1/g')
name=$(cat $pom_file | tr '\n' ' ' | grep name | sed -E 's/(<\/name>).*/\1/g' | sed -E 's/.*(<name>)/\1/g')
description=$(awk '/\<description\>/,/\<\/description\>/' $pom_file | tr '\n' ' ')
if [[ -z "$name" ]]; then
name="<name>$artifactId</name>"
fi
if [[ -z "$description" ]]; then
description="<description>$artifactId</description>"
fi
dependencies=$(awk '/\<dependencies\>/,/\<\/dependencies\>/' $pom_file | tr '\n' ' ')
# Replace placeholders with values from original POM file
cat $pom_template | \
sed "s#%%%ARTIFACT%%%#$artifact#g" | \
sed "s#%%%VERSION%%%#$version#g" | \
sed "s#%%%NAME%%%#$name#g" | \
sed "s#%%%DESCRIPTION%%%#$description#g" | \
sed "s#%%%DEPENDENCIES%%%#$dependencies#g" | \
tidy -i -xml -wrap 9999 2>/dev/null > "$pom_file.new"
echo "POM Created!"
fi
done
fi
done
done
else
echo "Path not found, please provide valid path as argument."
fi
# After Script runs you can check the diffs and replace the file.pom.new
# using this command:
# for i in `find . -name "*.pom"`; do mv $i.new $i; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment