Parsing maven version with bash

Recently I needed to parse my pom.xml file to get the artifact version out of it. I needed it on one of our CI agents and I did not have maven installed. I wrote a bash command to parse.

Here it is

grep version pom.xml | grep -v -e '<?xml|~'| head -n 1 | sed 's/[[:space:]]//g' | sed -E 's/<.{0,1}version>//g' | awk '{print $1}'

Let’s go over it step by step to help us understand it better

  • grep version pom.xml – this till get you all the lines with word version in them
  • grep –v –e '<?xml|~' – this will exclude all the matches (-v is reverse match) that are matching the regex (-e), there can be XML specification in the POM file
  • head –n 1 – only the first match
  • sed 's/[[:space:]]//g' – this removes the spaces around/in version string
  • sed -E 's/<.{0,1}version>//g' – this removed the <version> and </version> tags
  • awk '{print $1}' – prints the result

原文链接:Parsing maven version with bash

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容