Home > Back-end >  Update version for all arctifactid if groupid match
Update version for all arctifactid if groupid match

Time:04-10

I have a pom.xml file looks like below, I need to update all artifactId's which groupId is "org.springframework" and version is lower than "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>

I need to update version of all artifactId's version if groupid is "org.springframework"

code which I am tring

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

Expected pom file after changes

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

CodePudding user response:

You can use the use-dep-version mojo from maven-versions-plugin to do the job for you:

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

If it is possible, consider using either a property for spring version, so that you need to change the version only on one place, for example:

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

or a spring BOM, that defines all the versions for spring artifacts:

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

CodePudding user response:

myVersion="2.3.18"
POM=pom.xml

while read nr
do

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

 [ "$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>\)/\1'$myVersion'\3/' $POM
 fi

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

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
  • Related