|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to add classport-maven-plugin to pom.xml |
| 4 | +# Usage: ./add_classport_plugin.sh [pom_file_path] |
| 5 | +# If pom_file_path is not provided, defaults to "pom.xml" in current directory |
| 6 | + |
| 7 | +POM_FILE="${1:-pom.xml}" |
| 8 | + |
| 9 | +# Check if xmlstarlet is available |
| 10 | +if ! command -v xmlstarlet &> /dev/null; then |
| 11 | + echo "Error: xmlstarlet is not installed. Please install it first." |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +# Check if pom.xml exists |
| 16 | +if [ ! -f "$POM_FILE" ]; then |
| 17 | + echo "Error: pom.xml not found at $POM_FILE" |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +# Determine if the document uses namespaces |
| 22 | +HAS_NAMESPACE=false |
| 23 | +if xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project" "$POM_FILE" &>/dev/null; then |
| 24 | + HAS_NAMESPACE=true |
| 25 | +fi |
| 26 | + |
| 27 | +# Check if build/plugins already exists, if not create it |
| 28 | +# Try with namespace first, then without (only match /project/build/plugins, not pluginManagement) |
| 29 | +if [ "$HAS_NAMESPACE" = true ]; then |
| 30 | + BUILD_PLUGINS_EXISTS=$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project/x:build/x:plugins" "$POM_FILE" 2>/dev/null) |
| 31 | +else |
| 32 | + BUILD_PLUGINS_EXISTS=$(xmlstarlet sel -t -c "/project/build/plugins" "$POM_FILE" 2>/dev/null) |
| 33 | +fi |
| 34 | + |
| 35 | +if [ -z "$BUILD_PLUGINS_EXISTS" ]; then |
| 36 | + # Check if build exists, if not create it |
| 37 | + if [ "$HAS_NAMESPACE" = true ]; then |
| 38 | + BUILD_EXISTS=$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project/x:build" "$POM_FILE" 2>/dev/null) |
| 39 | + else |
| 40 | + BUILD_EXISTS=$(xmlstarlet sel -t -c "/project/build" "$POM_FILE" 2>/dev/null) |
| 41 | + fi |
| 42 | + |
| 43 | + if [ -z "$BUILD_EXISTS" ]; then |
| 44 | + # Create build element |
| 45 | + if [ "$HAS_NAMESPACE" = true ]; then |
| 46 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project" -t elem -n "build" "$POM_FILE" |
| 47 | + else |
| 48 | + xmlstarlet ed -L -s "/project" -t elem -n "build" "$POM_FILE" |
| 49 | + fi |
| 50 | + fi |
| 51 | + # Create plugins element (only if it doesn't exist directly under build) |
| 52 | + if [ "$HAS_NAMESPACE" = true ]; then |
| 53 | + if ! xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project/x:build/x:plugins" "$POM_FILE" 2>/dev/null; then |
| 54 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build" -t elem -n "plugins" "$POM_FILE" |
| 55 | + fi |
| 56 | + else |
| 57 | + if ! xmlstarlet sel -t -c "/project/build/plugins" "$POM_FILE" 2>/dev/null; then |
| 58 | + xmlstarlet ed -L -s "/project/build" -t elem -n "plugins" "$POM_FILE" |
| 59 | + fi |
| 60 | + fi |
| 61 | +fi |
| 62 | + |
| 63 | +# Check if the plugin already exists (to avoid duplicates) |
| 64 | +# Try with namespace first, then without |
| 65 | +PLUGIN_EXISTS=$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -v "count(//x:plugin[x:groupId='io.github.project' and x:artifactId='classport-maven-plugin'])" "$POM_FILE" 2>/dev/null) |
| 66 | +if [ -z "$PLUGIN_EXISTS" ] || [ "$PLUGIN_EXISTS" = "" ]; then |
| 67 | + PLUGIN_EXISTS=$(xmlstarlet sel -t -v "count(//plugin[groupId='io.github.project' and artifactId='classport-maven-plugin'])" "$POM_FILE" 2>/dev/null || echo "0") |
| 68 | +fi |
| 69 | + |
| 70 | +if [ "$PLUGIN_EXISTS" -eq 0 ]; then |
| 71 | + # Use the same namespace context as determined earlier |
| 72 | + if [ "$HAS_NAMESPACE" = true ]; then |
| 73 | + # Use namespace-aware XPath for editing - only match /project/build/plugins (not pluginManagement) |
| 74 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins" -t elem -n "plugin" -v "" "$POM_FILE" |
| 75 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins/x:plugin[last()]" -t elem -n "groupId" -v "io.github.project" "$POM_FILE" |
| 76 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins/x:plugin[last()]" -t elem -n "artifactId" -v "classport-maven-plugin" "$POM_FILE" |
| 77 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins/x:plugin[last()]" -t elem -n "version" -v "0.1.0-SNAPSHOT" "$POM_FILE" |
| 78 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins/x:plugin[last()]" -t elem -n "executions" -v "" "$POM_FILE" |
| 79 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins/x:plugin[last()]/x:executions" -t elem -n "execution" -v "" "$POM_FILE" |
| 80 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins/x:plugin[last()]/x:executions/x:execution" -t elem -n "goals" -v "" "$POM_FILE" |
| 81 | + xmlstarlet ed -L -N x=http://maven.apache.org/POM/4.0.0 -s "/x:project/x:build/x:plugins/x:plugin[last()]/x:executions/x:execution/x:goals" -t elem -n "goal" -v "embed" "$POM_FILE" |
| 82 | + else |
| 83 | + # Use non-namespace XPath |
| 84 | + xmlstarlet ed -L -s "/project/build/plugins" -t elem -n "plugin" -v "" "$POM_FILE" |
| 85 | + xmlstarlet ed -L -s "/project/build/plugins/plugin[last()]" -t elem -n "groupId" -v "io.github.project" "$POM_FILE" |
| 86 | + xmlstarlet ed -L -s "/project/build/plugins/plugin[last()]" -t elem -n "artifactId" -v "classport-maven-plugin" "$POM_FILE" |
| 87 | + xmlstarlet ed -L -s "/project/build/plugins/plugin[last()]" -t elem -n "version" -v "0.1.0-SNAPSHOT" "$POM_FILE" |
| 88 | + xmlstarlet ed -L -s "/project/build/plugins/plugin[last()]" -t elem -n "executions" -v "" "$POM_FILE" |
| 89 | + xmlstarlet ed -L -s "/project/build/plugins/plugin[last()]/executions" -t elem -n "execution" -v "" "$POM_FILE" |
| 90 | + xmlstarlet ed -L -s "/project/build/plugins/plugin[last()]/executions/execution" -t elem -n "goals" -v "" "$POM_FILE" |
| 91 | + xmlstarlet ed -L -s "/project/build/plugins/plugin[last()]/executions/execution/goals" -t elem -n "goal" -v "embed" "$POM_FILE" |
| 92 | + fi |
| 93 | + echo "Plugin configuration added to pom.xml" |
| 94 | +else |
| 95 | + echo "Plugin already exists in pom.xml, skipping addition" |
| 96 | +fi |
| 97 | + |
0 commit comments