Shell command to update pom file from a variable

后端 未结 6 1465
[愿得一人]
[愿得一人] 2021-01-06 17:24

Previously I used below command to take the version in pom.xml and increment it from one. Before increment snapshot version is, 0.0.1

#!/bin/bas         


        
相关标签:
6条回答
  • 2021-01-06 18:02

    Use an XML-aware tool. For example, in xsh you can write

    open pom.xml ;
    register-namespace m http://maven.apache.org/POM/4.0.0 ;
    for //m:version
        set . xsh:subst(., '(?<=\.)([0-9]+)$', '$1+1', 'e') ;
    save :b ;
    

    Which changes "0.0.1" to "0.0.2".

    To also increment the version if -SNAPSHOT is present, the regular expression becomes a bit more complex:

    xsh:subst(., '(?<=\.)([0-9]+)(?=$|-SNAPSHOT$)', '$1+1', 'e') ;
                  ^         ^          ^              ^      ^
                  |         |          |              |      |
                  |         |      Followed by        |  Evaluate replacement
             Preceded       |      end of string      |  as code
             by a dot   At least   or -SNAPSHOT plus  |
                        one digit  end of string     Add one
                                                     to the 1st
                                                     capture group
    
    0 讨论(0)
  • 2021-01-06 18:06

    The simple solution to set the version to a particular value via versions-maven-plugin

    mvn versions:set -DnewVersions=0.0.1
    

    If you like to increment it. This could be achieved by using by build-helper-maven-pugin like the following:

    mvn build-helper:parse-version versions:set \
         -DnewVersion=\${parsedVersion.nextMajorVersion}.0.0 \
         versions:commit
    

    or if you like to increment the minor version:

    mvn build-helper:parse-version versions:set \
     -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.nextMinorVersion}.0 \
     versions:commit
    

    or if you like to increment the patch version:

    mvn build-helper:parse-version versions:set \
     -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion} \
     versions:commit
    

    You need to pay attention to quoting which is needed on Windows/Linux.

    or you can use maven-release-plugin which will increment the current version to the next one by just calling:

    mvn -B release:update-versions
    

    Or you you the maven-release-plugin via the usual release process by mvn release:prepare release:perform which by default increments the version also.

    0 讨论(0)
  • 2021-01-06 18:06

    Looks like you want to edit the pom.xml in the backend. Try the Perl one-liner. Check this out.

    > cat pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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>
    <groupId>com.mss.inven</groupId>
    <artifactId>INVEN</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    > perl -ne ' { s/(.*)\.(\d+)(-SNAPSHOT.*)/printf("%s.%d%s",$1,$2+1,$3)/ge and $_=~s/.$//g if /<version>/; print } ' pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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>
    <groupId>com.mss.inven</groupId>
    <artifactId>INVEN</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    
    >
    
    0 讨论(0)
  • 2021-01-06 18:09

    In here I mention fully working command. Thank you for your support.

    #!/bin/bash
    
    version=$(grep -ri "<version>" pom.xml |head -n 1 | sed -e 's/^[ \t]*<version>\ 
    ([^<]*\)<.*$/\1/')
    
    major=$(echo $version | cut -c1)
    var2=$(echo $version | cut -c2)
    miner=$(echo $version | cut -c3)
    var4=$(echo $version | cut -c4)
    bug=$(echo $version | cut -c5)
    bug=$((bug+1))
    str="-SNAPSHOT"
    incrementVer=$major$var2$miner$var4$bug$str
    echo $incrementVer
    
    LN=$(grep -n "<version>" pom.xml | head -1 | awk -F ":" '{print $1}')
    sed -i "$LN s/$version/$incrementVer/" pom.xml
    
    0 讨论(0)
  • 2021-01-06 18:11

    Yes you can use sed to modify the version in pom.xml file. Simply add, at the end of your given bash script, those two lines:

    LN=$(grep -n "<version>" pom.xml | head -1 | awk -F ":" '{print $1}')
    sed -i "$LN s/$version/$incrementVer/" pom.xml
    

    Explanation:

    • The first line recover the line number of the project version (in LN var)
    • The second modify in place (-i parameter of sed) the pom file with new version value.
    0 讨论(0)
  • 2021-01-06 18:17

    I can use maven plugin to do above increment. But the thing is my mentor has been instructed me to get it by using shell commands for practice shell scripting. I highly appreciate your support. Somehow I solved my question using below commands.

    #!/bin/bash
    
    cd Project/
    
    version=$(grep -ri "<version>" pom.xml |head -n 1 | sed -e 's/^[ \t]*<version>\ 
    ([^<]*\)<.*$/\1/')
    
    major=$(echo $version | cut -c1)
    var2=$(echo $version | cut -c2)
    miner=$(echo $version | cut -c3)
    var4=$(echo $version | cut -c4)
    bug=$(echo $version | cut -c5)
    bug=$((bug+1))
    str="-SNAPSHOT"
    incrementVer=$major$var2$miner$var4$bug$str
    echo $incrementVer
    
    pomChange=$(grep -ri "<version>" pom.xml | head -n 1 | sed -i "s/\(<version>\)[^<]*\ 
    (<\/version>\)/\1$incrementVer\2/" pom.xml)
    echo $pomChange
    

    Output looks like,

    [INFO] ------------------------------------------------------------------- 
    [INFO] BUILD SUCCESS
    [INFO] --------------------------------------------------------------------- 
    [INFO] Total time: 4.087s
    [INFO] Finished at: Wed Oct 31 00:25:40 IST 2018
    [INFO] Final Memory: 6M/17M
    [INFO] --------------------------------------------------------------------
    0.0.2-SNAPSHOT
    Finished: SUCCESS
    
    0 讨论(0)
提交回复
热议问题