3

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/bash

version=$(grep -ri "<version>" pom.xml |head -n 1 | sed -e 's/^[ \t]* 
<version>\([^<]*\)<.*$/\1/' | sed 's/[-SNAPSHOT]//g')
var1=$(echo $version | cut -c1)
var2=$(echo $version | cut -c2)
var3=$(echo $version | cut -c3)
var4=$(echo $version | cut -c4)
var5=$(echo $version | cut -c5)
var5=$((var5+1))
incrementVer=$var1$var2$var3$var4$var5
echo $incrementVer

output is 0.0.2

But I want to push this output into pom file and update as, <version>0.0.2</version>

Can I use sed command to update pom file?

My pom file looks like this,

<?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>
Janith
  • 217
  • 1
  • 6
  • 15
  • Add pom.xml to your question. – Cyrus Nov 01 '18 at 09:26
  • 2
    simply use [versions-maven-plugin](https://www.mojohaus.org/versions-maven-plugin/).. just `mvn versions:set -DnewVersions=0.0.1` to set it..or to increment by using [build-helper-maven-plugin](https://www.mojohaus.org/build-helper-maven-plugin/parse-version-mojo.html) via `mvn build-helper:parse-version versions:set \ -DnewVersion=\${parsedVersion.nextMajorVersion}.0.0 \ versions:commit` ...or [maven-release-plugin](http://maven.apache.org/maven-release/maven-release-plugin/update-versions-mojo.html)... – khmarbaise Nov 01 '18 at 10:46
  • 1
    https://stackoverflow.com/a/1732454/927493 – J Fabian Meier Nov 01 '18 at 14:09

6 Answers6

13

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

mvn versions:set -DnewVersion=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.

rsmets
  • 789
  • 1
  • 13
  • 23
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • While `mvn versions:set` is nice it is not alway useful if you just want to quickly adjust/change a version number in a pom. In my case it triggers a check for updates on jitpack which takes 15 minutes. Using the -o flag doesn't help in my case because it breaks other things. So for me bash or python is the way to go :) – Wlad Jul 01 '20 at 19:58
  • thank you! Your response was exactly what I needed! – Alin Gabriel Arhip Jul 08 '20 at 12:44
1

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
choroba
  • 231,213
  • 25
  • 204
  • 289
0

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
Janith
  • 217
  • 1
  • 6
  • 15
  • It is not reliable cause grep etc. do not know about the XML structure of a pom file...sorry your instructor does not know to do it right see https://stackoverflow.com/a/53099850/296328 – khmarbaise Nov 01 '18 at 12:52
  • @khmarbaise: This is your opinion and it is wrong. This solution is perfectly usable for this specific case. There is never a single solution for any problem. – Abdelhamid MEDDEB Nov 01 '18 at 15:08
  • Above thing work if pom file have only one version attribute. @AbdelhamidMEDDEB have mentioned a command as, `LN=$(grep -n "" pom.xml | head -1 | awk -F ":" '{print $1}') sed -i "$LN s/$version/$incrementVer/" pom.xml`. It works for me. thank you so much – Janith Nov 02 '18 at 05:09
  • @Janith It works for the exact file you have, but I would not use it "in the wild" for arbitrary pom files. – J Fabian Meier Nov 02 '18 at 14:49
0

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>

>
stack0114106
  • 8,534
  • 3
  • 13
  • 38
  • Perl etc. grep is not aware of any kind of xml structure which could fail under different circumstances so better use the solution https://stackoverflow.com/a/53099850/296328 which works always... – khmarbaise Nov 01 '18 at 12:53
  • @khmarbaise: This is your opinion and it is wrong. This solution is perfectly usable for this specific case. There is never a single solution for any problem. – Abdelhamid MEDDEB Nov 01 '18 at 15:05
  • It's simply not an opinion it's simply based on the problem cause an XML structure can't be parsed reliable with grep/shell/perl etc. there are always cases which fail...which is exactly what wrote... – khmarbaise Nov 01 '18 at 16:03
  • @khmarbaise.. the OP is concerned about the tactical solution to the specific case which just works with the perl answer. – stack0114106 Nov 01 '18 at 16:55
0

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
Janith
  • 217
  • 1
  • 6
  • 15
-4

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.
  • Grep etc. is not reliable cause a pom file is an XML structure which grep and awk don't know about... – khmarbaise Nov 01 '18 at 12:51
  • @khmarbaise: This is your opinion and it is wrong. This solution is perfectly usable for this specific case. There is never a single solution for any problem. – Abdelhamid MEDDEB Nov 01 '18 at 15:06
  • 1
    @AbdelhamidMEDDEB There are many valid pom files around in which your solution would fail. So it is dangerous to recommend it even if it works for the specific file used by the OP. – J Fabian Meier Nov 01 '18 at 15:29
  • @JF Meier You're right but in this specific question, a specific pom file is given. – Abdelhamid MEDDEB Nov 01 '18 at 15:40
  • 2
    Dear future readers of this answer: Don't use this solution unless your pom file is line by line, character by character the same as the one of the OP. In any other case: Use the solution of @khmarbaise. – J Fabian Meier Nov 02 '18 at 15:36