Skip to content

Instantly share code, notes, and snippets.

@recalde
Last active April 1, 2025 14:45
Show Gist options
  • Select an option

  • Save recalde/9d3aac1122eae8d40852db3aa2f91a2b to your computer and use it in GitHub Desktop.

Select an option

Save recalde/9d3aac1122eae8d40852db3aa2f91a2b to your computer and use it in GitHub Desktop.
Clean Maven POM XML Files
#!/bin/bash
set -e
MAVEN_VERSION=3.9.6
MAVEN_DIR="$HOME/.maven/apache-maven-$MAVEN_VERSION"
MAVEN_BIN="$MAVEN_DIR/bin/mvn"
echo "=== Checking Java ==="
if ! command -v java >/dev/null 2>&1; then
echo "Java not found. Installing OpenJDK..."
brew install openjdk
echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zprofile
source ~/.zprofile
fi
echo "=== Setting up Apache Maven $MAVEN_VERSION ==="
if [ ! -x "$MAVEN_BIN" ]; then
mkdir -p "$HOME/.maven"
curl -L "https://dlcdn.apache.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz" -o /tmp/maven.tar.gz
tar -xzf /tmp/maven.tar.gz -C "$HOME/.maven"
rm /tmp/maven.tar.gz
fi
export PATH="$MAVEN_DIR/bin:$PATH"
echo "Maven version: $($MAVEN_BIN -v | head -n 1)"
echo "=== Adding DepClean plugin to pom.xml (if not already present) ==="
if ! grep -q "depclean-maven-plugin" pom.xml; then
awk '
/<plugins>/ && c == 0 {
print
print " <plugin>"
print " <groupId>se.kth.castor</groupId>"
print " <artifactId>depclean-maven-plugin</artifactId>"
print " <version>2.0.6</version>"
print " </plugin>"
c = 1
next
}
{ print }
' pom.xml > pom.tmp && mv pom.tmp pom.xml
fi
echo "=== Running DepClean to generate cleaned pom ==="
$MAVEN_BIN se.kth.castor:depclean-maven-plugin:2.0.6:depclean
if [ -f "debloated-pom.xml" ]; then
echo "Found debloated-pom.xml. Replacing current pom.xml..."
mv pom.xml pom.backup.xml
mv debloated-pom.xml pom.xml
else
echo "No debloated-pom.xml created. Check for dependency issues."
exit 1
fi
echo "=== Installing SortPom ==="
if ! command -v sortpom >/dev/null 2>&1; then
brew install sortpom || echo "Please install SortPom manually if brew fails."
fi
echo "=== Formatting final pom.xml ==="
$MAVEN_BIN com.github.ekryd:sortpom-maven-plugin:sort
echo "✅ pom.xml cleaned, formatted, and backup saved as pom.backup.xml"
#!/bin/bash
set -e
echo "=== Checking Java ==="
if ! command -v java >/dev/null 2>&1; then
echo "Java not found. Installing OpenJDK..."
brew install openjdk
echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zprofile
source ~/.zprofile
fi
echo "=== Checking Maven ==="
if ! command -v mvn >/dev/null 2>&1; then
echo "Maven not found. Installing..."
brew install maven
fi
echo "=== Adding DepClean plugin to pom.xml (if not already present) ==="
if ! grep -q "depclean-maven-plugin" pom.xml; then
# Add plugin inside the <plugins> section
awk '
/<plugins>/ && c == 0 {
print
print " <plugin>"
print " <groupId>se.kth.castor</groupId>"
print " <artifactId>depclean-maven-plugin</artifactId>"
print " <version>2.0.6</version>"
print " </plugin>"
c = 1
next
}
{ print }
' pom.xml > pom.tmp && mv pom.tmp pom.xml
fi
echo "=== Running DepClean to generate cleaned pom ==="
mvn se.kth.castor:depclean-maven-plugin:2.0.6:depclean
if [ -f "debloated-pom.xml" ]; then
echo "Found debloated-pom.xml. Replacing current pom.xml..."
mv pom.xml pom.backup.xml
mv debloated-pom.xml pom.xml
else
echo "No debloated-pom.xml created. Check for dependency issues."
exit 1
fi
echo "=== Installing SortPom ==="
brew install sortpom || echo "SortPom already installed."
echo "=== Formatting final pom.xml ==="
mvn com.github.ekryd:sortpom-maven-plugin:sort
echo "✅ pom.xml cleaned, formatted, and backup saved as pom.backup.xml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment