Skip to content

Instantly share code, notes, and snippets.

@nacx
Created June 23, 2011 09:17
Show Gist options
  • Select an option

  • Save nacx/1042207 to your computer and use it in GitHub Desktop.

Select an option

Save nacx/1042207 to your computer and use it in GitHub Desktop.

Revisions

  1. nacx created this gist Jun 23, 2011.
    98 changes: 98 additions & 0 deletions update-version
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    #!/bin/bash

    if [[ -z `which xmlstarlet` ]]; then
    echo "xmlstarlet must be installed to run the script."
    exit 1
    fi

    function usage() {
    cat << EOF
    Usage: ${0} -p <project root> -c <current version> -n <next version> [-h]
    Options:
    -p: The project root directory with the poms to update (e.g. ~/workspace/abiquo)
    -c: The current version (e.g. 1.7.5-SNAPSHOT)
    -n: The next version (e.g. 1.7.5)
    -h: Shows this help
    EOF
    exit 0
    }

    PROJECT_ROOT=
    CURRENT_VERSION=
    NEXT_VERSION=

    while getopts "p:c:n:h" OPT; do
    case ${OPT} in
    p) PROJECT_ROOT=${OPTARG} ;;
    c) CURRENT_VERSION=${OPTARG} ;;
    n) NEXT_VERSION=${OPTARG} ;;
    h) usage ;;
    ?) usage ;;
    esac
    done

    TMPFILE=/tmp/uv-$$

    [[ -z ${PROJECT_ROOT} ]] && usage
    [[ -z ${CURRENT_VERSION} ]] && usage
    [[ -z ${NEXT_VERSION} ]] && usage


    function update_pom() {
    echo "Updating poms..."
    POM_NS="http://maven.apache.org/POM/4.0.0"

    for POM in `find -L ${PROJECT_ROOT} -name pom.xml`; do
    HAS_NAMESPACE=`xmlstarlet sel -N x=${POM_NS} -t -c "/x:project" ${POM}`
    if [[ "${HAS_NAMESPACE}" != "" ]]; then
    NS="x:"
    else
    NS=""
    fi

    NAME=`xmlstarlet sel -N x=${POM_NS} -t -v "/${NS}project/${NS}name" ${POM}`
    echo " ${NAME}"

    # Update project versions (maybe groupId is not defined)
    xmlstarlet ed -P -N x=${POM_NS} \
    -u "/${NS}project[${NS}version/text()='${CURRENT_VERSION}']/${NS}version" \
    -v "${NEXT_VERSION}" ${POM} >${TMPFILE}

    # Update all versions of com.abiquo.abicloud artifacts
    xmlstarlet ed -P -N x=${POM_NS} \
    -u "//${NS}*[${NS}groupId/text()='com.abiquo.abicloud' and ${NS}version/text()='${CURRENT_VERSION}']/${NS}version" \
    -v "${NEXT_VERSION}" ${TMPFILE} >${TMPFILE}.tmp

    # Update all versions of com.abiquo artifacts
    xmlstarlet ed -P -N x=${POM_NS} \
    -u "//${NS}*[${NS}groupId/text()='com.abiquo' and ${NS}version/text()='${CURRENT_VERSION}']/${NS}version" \
    -v "${NEXT_VERSION}" ${TMPFILE}.tmp >${POM}

    done

    [[ -f ${TMPFILE} ]] && rm -f ${TMPFILE}
    [[ -f ${TMPFILE}.tmp ]] && rm -f ${TMPFILE}.tmp
    }

    function update_filters() {
    echo "Updating version in filters"
    FILTERS_DIR=${PROJECT_ROOT}/*/src/main/filters
    FILTERS_OLD_VERSION=${CURRENT_VERSION:0:3}
    FILTERS_NEW_VERSION=${NEXT_VERSION:0:3}

    for PF in `find ${FILTERS_DIR} -name *.properties`; do
    sed -e "s/abicloud\\.version=${FILTERS_OLD_VERSION}/abicloud\\.version=${FILTERS_NEW_VERSION}/" \
    -e "s/abicloud\\.distribution=${FILTERS_OLD_VERSION}/abicloud\\.distribution=${FILTERS_NEW_VERSION}/" \
    ${PF} >${TMPFILE}

    mv -f ${TMPFILE} ${PF}
    done
    }


    update_pom
    update_filters

    echo "Done!"