1#!/bin/bash 2 3set -ef 4 5# For each library module in current working directory, this script 6# sets the parent to the root pom.xml 7 8# Run this script at the root of google-cloud-java repository 9 10function replaceParent { 11 parent_pom=$1 12 # First, read the values from the parent pom.xml 13 parent_version=$(perl -nle 'print $1 if m|<version>(.+)</version>|' "$parent_pom"|head -1) 14 parent_group_id=$(perl -nle 'print $1 if m|<groupId>(.+)</groupId>|' "$parent_pom" |head -1) 15 parent_artifact_id=$(perl -nle 'print $1 if m|<artifactId>(.+)</artifactId>|' "$parent_pom"|head -1) 16 relativePath=$(echo "$parent_pom" | sed 's/\//\\\//g') 17 18 # Search for <parent> tag in module pom and replace the next three lines -- groupId, artifcatId, and version 19 perl_command="s/\s*<parent>.*?<\/parent>/\n\n <parent>\n <groupId>${parent_group_id}<\/groupId>\n <artifactId>${parent_artifact_id}<\/artifactId>\n <version>${parent_version}<\/version><!-- {x-version-update:google-cloud-java:current} -->\n <relativePath>${relativePath}<\/relativePath>\n <\/parent>/s" 20 # execute the replacement in pom.xml 21 perl -i -0pe "$perl_command" pom.xml 22} 23 24# Then, apply the values as the parent pom of each module 25for module in $(find . -mindepth 2 -maxdepth 2 -name pom.xml | sort --dictionary-order | xargs dirname); do 26 # example value of module is "./java-accessapproval" 27 if [[ "${module}" = *gapic-libraries-bom ]] || \ 28 [[ "${module}" = *google-cloud-jar-parent ]] || \ 29 [[ "${module}" = *google-cloud-pom-parent ]] || \ 30 [[ "${module}" = *java-shared-dependencies ]]; then 31 continue 32 fi 33 echo "Processing module $module" 34 pushd $module 35 36 replaceParent ../google-cloud-jar-parent/pom.xml 37 38 # update the bom projects as well by removing parent 39 if ls -1 | grep 'bom'; then 40 BOM=$(ls -1 | grep 'bom') 41 cd "$BOM" 42 replaceParent ../../google-cloud-pom-parent/pom.xml 43 fi 44 45 popd 46done 47