如果 groupid 匹配,则更新所有 arctifactid 的版本

Update version for all arctifactid if groupid match

我有一个如下所示的 pom.xml 文件,我需要更新所有 artifactId,其 groupId 为“org.springframework”且版本低于“2.3.18”

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>2.3.15</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>2.3.15</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>2.3.15</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>

如果 groupid 是“org.springframework”

,我需要更新所有 artifactId 的版本

我正在尝试的代码

sed -i '/<artifactId>org.springframework<\/artifactId>/{n;s/<version>.*<\/version>/<version>/<version>5.3.18<\/version>/}' pom.xml

更改后的预期 pom 文件

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>2.3.18</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>2.3.18</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>2.3.18</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>

您可以使用 maven-versions-pluginuse-dep-version mojo 为您完成这项工作:

mvn versions:use-dep-version -Dincludes="org.springframework:*" -DdepVersion=<new version>

如果可能,请考虑为 spring 版本使用 property,这样您只需更改一个地方的版本,例如:

<properties>
    <spring.version>2.3.15</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

或 spring BOM,它定义了 spring 个工件的所有版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>2.3.15</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <!-- no version tag needed here, the correct version from BOM is used -->
    </dependency>
</dependencies>
myVersion="2.3.18"
POM=pom.xml

while read nr
do

 nr=$((nr+2))
 version=$(sed -n ''$nr''p $POM|awk -F"[><]" '{print }')

 [ "$version" = "$myVersion" ] && continue

 sortedVersion=$(echo "$version $myVersion"|tr ' ' '\n'|sort -V|tail -1)

 if [ "$sortedVersion" = "$myVersion" ]
 then
   echo update line $nr. from  $version to $myVersion
   sed -i ''$nr's/\(<version>\)\(.*\)\(<\/version>\)/'$myVersion'/' $POM
 fi

done < <(grep -n "<groupId>org.springframework" $POM|awk -F: '{print }')

update line 4. from 2.3.15 to 2.3.18
update line 9. from 2.3.15 to 2.3.18
update line 14. from 2.3.15 to 2.3.18