如何在Linux中使用sed命令替换pom.xml文件中的特定依赖项版本 [英] How to replace a specific dependency version in pom.xml file using sed command in Linux
问题描述
我有以下pom.xml文件
I have the following pom.xml file
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qburst.app</groupId>
<artifactId>main-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>main-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven</id>
<url>http://nexus:8081/repository/maven-group/</url>
</repository>
</repositories>
<distributionManagement>
<snapshotRepository>
<id>nexus</id>
<url>http://nexus:8081/repository/maven-snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus</id>
<url>http://nexus:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.5</version>
</plugin>
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.5</version>
</plugin>
</plugins>
</build>
</project>
我想在终端中使用sed命令更改特定依赖项的版本.但是,以下命令似乎不起作用:
I want to change a version of a particular dependency using sed command in terminal. However the following command doesnot seem to work:
sed -n -E 's/\(<dependency>[.\n\t ]*<groupId>com.mycompany.app<\/groupId>[.\S\w\n\t ]*<version>\)[0-9.]*\(<\/version>[.\n\t ]*<\/dependency>\)/\14.3\2/' pom.xml
我在网上发现以下正则表达式为True,但是:
I found out using online that the following regular expression is True However:
(<dependency>[.\n\t ]*<groupId>com.mycompany.app<\/groupId>[.\S\w\n\t ]*<version>)[0-9.]*(<\/version>[.\n\t ]*<\/dependency>)
但是在sed中不起作用. 有人请帮助
However in sed it doesnot work. Someone please help
推荐答案
xmlstarlet .学习起来有点困难,因为它似乎与正则表达式在使用XPath时学到的所有东西背道而驰.但最终,这是前进的方向.
An excellent shell tool that can be used to do this job is xmlstarlet. It is a bit a tough one to learn because it seems to go against everything that regex learned us as it makes use of XPath. But in the end, this is the way forward.
要编辑XML文件时,首先要做的就是弄清楚要使用的XPath.这是手头最困难的任务
As you want to edit an XML file, the first thing you need to do is figure out the XPath you want to use. This is the most difficult task at hand here
First you need to find the XPath that selects the right node. This can be done using the select
option of xmlstarlet. For this, you need to know
-
您的名称空间.您的整个XML文件位于名称空间
xmlns="http://maven.apache.org/POM/4.0.0"
中,因此每个节点都需要使用该名称空间进行引用.
your namespace. Your entire XML file is in the namespace:
xmlns="http://maven.apache.org/POM/4.0.0"
, so every node needs to be referenced with this namespace.
您正在搜索名为dependency
的节点,该节点的子节点groupId
的值为com.mycompany.app
.的XPath是:
You are searching for a node named dependency
who has a child groupId
with a value com.mycompany.app
. The XPath for this is:
//N:dependency[N:groupId = "com.mycompany.app"]
其中N:
是我们稍后将定义的名称空间的缩写.
where N:
is the namespace shorthand which we will define later.
您要更改的值是子version
的值,因此您必须在XPath表达式中添加/N:version
.
The value you want to change is the value of the child version
, thus you have to add /N:version
to the XPath expression.
要查看您选择的内容,可以输入:
So to see what you selected you can type:
$ xmlstarlet sel -N N="http://maven.apache.org/POM/4.0.0" \
-t -m '//N:dependency[N:groupId="com.mycompany.app"]' \
-c . -n pom.xml
和
$ xmlstarlet sel -N N="http://maven.apache.org/POM/4.0.0" \
-t -m '//N:dependency[N:groupId="com.mycompany.app"]/N:version' \
-c . -n pom.xml
步骤2:使用刚创建的XPath编辑文件
现在是时候在 xmlstarlet .使用前面定义的XPath,我们可以轻松地更新(-u
)这个值(-v
):
Step 2: edit your file using the XPath you just created
Now it is time to perform the edit using the edit function in xmlstarlet. Using the XPath earlier defined, we can easily update (-u
) this one value (-v
):
$ xmlstarlet ed -N N="http://maven.apache.org/POM/4.0.0" \
-u '//N:dependency[N:groupId="com.mycompany.app"]/N:version' \
-v "14.3" pom.xml > pom.new.xml
第3步:验证并应用.
使用diff
或meld
验证更改以进行检查.
Step 3: verify and apply.
Verify the changes with diff
or meld
to check.
注意:使您的XPath知识与Regex知识一样好,这样您就可以跳过第1步.
note: make your XPath knowledge as good as your Regex knowledge, this way you can skip step 1.
这篇关于如何在Linux中使用sed命令替换pom.xml文件中的特定依赖项版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!