检查 xml 文件的值并增加十进制值

检查 xml 文件的值并增加十进制值

我有一个 shell 脚本,用于将 pom 文件版本增加 1。但每次我都必须更改脚本以增加版本 (major.minor.patch)。现在我想编写一个脚本来自动增加它。例如,如果补丁版本等于 0.0.10,则自动增加次要版本并将次要版本设置为 0.1.0

#!/bin/bash
get_version()
{
  # Get the package version from pom.xml
  group_id=$(grep -ri "<groupId>" pom.xml |head -n 1 | sed -e 's/^[ \t]*<groupId>\([^<]*\)<.*$/\1/')
  artifact_id=$(grep -ri "<artifactId>" pom.xml |head -n 1 | sed -e 's/^[ \t]*<artifactId>\([^<]*\)<.*$/\1/')
  artifact_version=$(grep -ri "<version>" pom.xml |head -n 1 | sed -e 's/^[ \t]*<version>\([^<]*\)<.*$/\1/' | sed 's/[-SNAPSHOT]//g')
  echo Group ID: ${group_id}
  echo Package Name: ${artifact_id}
  echo Package Version: ${artifact_version}
}

version_split()
{
  version=$(grep -ri "<version>" pom.xml |head -n 1 | sed -e 's/^[ \t]*<version>\([^<]*\)<.*$/\1/')
  major_version=$(echo $version| cut -c1)
  minor_version=$(echo $version| cut -c3)
  patch_version=$(echo $version| cut -c3)
}

increment_patch_version()
{
  patch_version=$((patch_version+ 1))
}

update_build_version()
{
  updated_version=${major_version}.${minor_version}.${patch_version}
  echo Updated Build Version: ${updated_version}
}

update_version_file()
{
  LN=$(grep -n "<version>" pom.xml | head -1 | awk -F ":" '{print $1}')
  LN=$(sed -i "$LN s/$artifact_version/$updated_version/" pom.xml)
  updated_newversion=$(grep -ri "<version>" pom.xml |head -n 1 | sed -e 's/^[ \t]*<version>\([^<]*\)<.*$/\1/')
  echo POM UPDATED AS: ${updated_newversion}
}

get_version
version_split
increment_patch_version
update_build_version
update_version_file

我可以使用 if 条件来做到这一点吗?

我写了一个 if 条件来检查 PATCH_VERSION 并进行更新。但无法使用它。

increment_patch_version()
{
    NUM="*.*.10"
    CHECK="$major_version.$minor_version.$patch_version"
    if [[ $CHECK < $NUM || bc ]]; then
        patch_version=$((patch_version+ 1))
        echo $patch_version
    fi
}

有没有办法将其应用于所有版本更新?我的 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>test24x7</groupId>
    <artifactId>ab-backend</artifactId>
    <version>0.0.2</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                        <manifest>
                            <mainClass>com.test24x7.innovation.quickcheckout.main.QuickCheckoutBackendMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- bind to the packaging phase -->
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/out</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!--Clean out folder-->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>out</directory>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                        <fileset>
                            <directory>${basedir}</directory>
                            <includes>
                                <include>**/ABBBackend-tcp*/</include>
                            </includes>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <!--Copy folder to out-->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/out/abresources</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/abresources</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-resource-one</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>

                        <configuration>
                            <outputDirectory>${basedir}/out/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}</directory>
                                    <includes>
                                        <include>Runner.bat</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-logback</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>

                        <configuration>
                            <outputDirectory>${basedir}/out/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}</directory>
                                    <includes>
                                        <include>logback.xml</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>version.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.client.mqttv3 -->
        <dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.sonarsource.scanner.maven/sonar-maven-plugin -->
        <dependency>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.5.0.1254</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.scream3r/jssc -->
        <dependency>
            <groupId>org.scream3r</groupId>
            <artifactId>jssc</artifactId>
            <version>2.8.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.4.2</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <properties>
        <jdk.version>1.8</jdk.version>
    </properties>

</project>

相关内容