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>
The simple solution to set the version to a particular value via versions-maven-plugin
If you like to increment it. This could be achieved by using by build-helper-maven-pugin like the following:
or if you like to increment the minor version:
or if you like to increment the patch version:
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:
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.In here I mention fully working command. Thank you for your support.
Use an XML-aware tool. For example, in xsh you can write
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: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.
Output looks like,
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:
Explanation:
Looks like you want to edit the pom.xml in the backend. Try the Perl one-liner. Check this out.