如何在Linux中使用sed命令替换pom.xml文件中的特定依赖项版本 [英] How to replace a specific dependency version in pom.xml file using sed command in Linux

查看:137
本文介绍了如何在Linux中使用sed命令替换pom.xml文件中的特定依赖项版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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

推荐答案

.学习起来有点困难,因为它似乎与正则表达式在使用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

首先,您需要找到选择正确节点的XPath.可以使用

.使用前面定义的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步:验证并应用.

使用diffmeld验证更改以进行检查.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆