简体   繁体   English

使用sed / regex / bash脚本碰撞版本字符串

[英]Bumping version string using sed/regex/bash script

I am currently building a CI pipeline that handles incrementing version from a maven pom.xml file. 我目前正在建立一个CI管道来处理来自maven pom.xml文件的递增版本。 The pom file looks something like this: pom文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>myApp</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <parent>
        <groupId>com.acme.project</groupId>
        <artifactId>parentApp</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- internal libs -->
        <dependency>
            <groupId>...

I want to perform the following: 我要执行以下操作:

  1. Extract the application version (first occurrence of ), ie 1.0.0-SNAPSHOT and replace it with 1.0.0 提取应用程序版本(首次出现),即1.0.0-SNAPSHOT并将其替换为1.0.0
  2. Bump up the version (1.0.1) and append SNAPSHOT to it ( 1.0.1-SNAPSHOT ) 碰撞版本(1.0.1)并将SNAPSHOT附加到它( 1.0.1-SNAPSHOT

Since I am using Teamcity, I am limited to using bash/sed/regex for this string manipulation. 由于我使用的是Teamcity,因此仅限于使用bash / sed / regex进行此字符串操作。 For step 1 I have done something like this: 对于第1步,我做了如下操作:

sed -i -e '1,/<version>1.0.0-SNAPSHOT<\/version>/s/<version>1.0.0-SNAPSHOT<\/version>/<version>1.0.0<\/version>/' pom.xml

The problem is that I am hardcoding the version - this could be 1.0.2-SNAPSHOT , so I need a way to extract this in a flexible way. 问题是我正在对版本进行硬编码-这可能是1.0.2-SNAPSHOT ,所以我需要一种灵活的方式来提取它。 For step 2, I have no idea how to do it with a primitive tool like sed/regex. 对于第2步,我不知道如何使用sed / regex之类的原始工具来实现。

with bash and grep 与bash和grep

# extract the version
v=$(grep -m1 -o '[^<>]*-SNAPSHOT' pom.xml)
[[ $v ]] || { echo "failed to extract version"; exit 1;}
# bumped version remove -SNAPSHOT suffix
bv=${v%-SNAPSHOT}
# new version (split on last dot)
nv=${bv%.*}.$((${bv##*.}+1))

or driectly with perl 或与perl直接

perl -i -pe 's/([^<>]*)(\d+)-SNAPSHOT/$1.($2+1)/e' pom.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM