'Update version for all arctifactid if groupid match
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>
Solution 1:[1]
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>
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | VANAN |
| Solution 2 | ufopilot |
