0

sample input :

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.in28minutes.microservices</groupId>
    <artifactId>currency-exchange-service</artifactId>
    <version>3.0.3</version>
    <name>currency-exchange-service-docker</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>16</java.version>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>
</project>

expected output:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.in28minutes.microservices</groupId>
    <artifactId>currency-exchange-service</artifactId>
    <version>3.0.4</version>
    <name>currency-exchange-service-docker</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>16</java.version>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>
</project>

I have calclated the 3.0.4 and using var2 variable to store it. I was using this command to update

sed -i "s/<version>.*<\/version>/<version>$var2<\/version>/" pom.xml

but it was changing versions under each version tag. But i need to change value under version tag which has project as a parent tag.

I tried with

sed -i "s/<project><version>.*<\/version>/<project><version>$var2<\/version>/" pom.xml

but its not working.

my script.sh

#!/bin/sh
var1=$(echo '${project.version}' | mvn help:evaluate | grep -v '^[[]')
var2=$(echo "$var1" | awk -F. -v OFS=. '++$NF')
sed -i "s/<version>.*<\/version>/<version>$var2<\/version>/" pom.xml
echo "$var2"

2 Answers2

2
$ xmlstarlet edit --update '//project/version' --value '3.0.4' file.xml
<?xml version="1.0"?>
<project>
  <version>3.0.4</version>
  <dependencies>
    <dependency>
      <version>1.2.5.RELEASE</version>
    </dependency>
  </dependencies>
</project>
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • xml edit --update '//project/version' --value '3.0.4' pom.xml .This command does not update anything. I am using windows . I have updated my post with the xml that i have been working on. – Karamveer Gahlot Aug 16 '21 at 05:26
  • If you're using Windows then why did you tag your question with `unix` and post a question about a shell script? – Ed Morton Aug 16 '21 at 12:46
0

This sed should do:

sed "/<version>/s/>.*</>${var2}</" pom.xml
<project>
       <version>3.0.4</version>
</project>

It searches for line with <version> then replace data between >xxx< with the variable.

Using regex, you can make sure what to find does not grab to much.

sed -E "/<version>/s/>[^<]+</>${var2}</" pom.xml

If you need to make sure it does not change other version than the one after project you can use awk

awk -v var=$var2 '/<project>/ {f=1} f && /<version>/ {sub(/>[^<]+</,">"var"<")} 1' pom.xml
<project>
       <version>3.0.4</version>
</project>

Sed version that test for project and change on next line

sed -E '/<project>/!b;n;s/<version>[^<]+</<version>'$var2'</' pom.xml
<project>
       <version>3.0.4</version>
</project>
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • when i tired this it is not giving me the expected result. It is not updating anythig. – Karamveer Gahlot Aug 15 '21 at 09:17
  • @KaramveerGahlot Does any of the other work? What system are you on? – Jotne Aug 15 '21 at 09:31
  • MY script.sh #!/bin/bash var1=$(echo '${project.version}' | mvn help:evaluate | grep -v '^[[]') var2=$(echo $var1 | awk -F. -v OFS=. '++$NF') awk -v var=$var2 '// {f=1} f && // {sub(/>[^<]+,">"var"<")} 1' pom.xml echo $var2 I used both the awk and sed commands but they are not updating anything. Any suggestion if i am doing something wrong ? I am using windows. – Karamveer Gahlot Aug 15 '21 at 09:39
  • Edit your post and add this information and script to it. Not easy to see formatting in comments. – Jotne Aug 15 '21 at 09:57