是否可以使用sed替换pom文件中的依赖项版本? [英] Is it possible to use sed to replace dependencies's version in a pom file?

查看:147
本文介绍了是否可以使用sed替换pom文件中的依赖项版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pom.xml 文件,我想将任何 dependency 节点内的版本从 0.1-SNAPSHOT 替换为不同的版本(比如说 NEW-VERSION ),我唯一的限制是仅当 groupId 与特定文本匹配时才应进行替换,例如: com.company.xyz .

I have a pom.xml file and I want to replace the version inside any of the dependency nodes from 0.1-SNAPSHOT to a different version (let's say NEW-VERSION), the only restriction I have is that the replacement should be done only if the groupId matches a particular text, lets say: com.company.xyz.

话虽这么说,这就是我的 pom.xml dependencies 部分:

That being said, let's say this is the dependencies section of my pom.xml:

<dependencies>
        <dependency>
            <groupId>com.company.xyz</groupId>
            <artifactId>xyz-xx-utils</artifactId>
            <version>0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.ibm.xyz</groupId>
            <artifactId>xyz</artifactId>
            <version>56.1</version>
        </dependency>
</dependencies>

应用sed命令后,我希望它看起来像:

After applying the sed command I want it to look like:

<dependencies>
        <dependency>
            <groupId>com.company.xyz</groupId>
            <artifactId>xyz-xx-utils</artifactId>
            <version>NEW-VERSION</version>
        </dependency>

        <dependency>
            <groupId>com.ibm.xyz</groupId>
            <artifactId>xyz</artifactId>
            <version>56.1</version>
        </dependency>
</dependencies>

这是我尝试过但没有成功的事情:

Here is what I have tried without any success:

newVersion="NEW-VERSION"

sed -i "s/\(<dependency>\)\(<groupId>com.company.xyz</groupId>\)\(<artifactId>.*\</artifactId>\)\(<version>0.1-SNAPSHOT</version>\)\(</dependency>\)/\1\2\3$newVersion\5/" pom.xml

我想知道这样做不起作用的原因是否是因为我的文本有多行,所以我做了一些研究,但我不理解用于多行的语法(有些是 N 东西).

I was wondering if the reason why this does not work is because my text has multiple lines, so I researched a little bit but I did not understand the syntax used to work with multiple lines (some N thing).

顺便说一句,我将这段代码包含在执行其他操作的bash文件中,这就是为什么我要使用sed或与bash兼容的任何其他工具"来执行此操作.

Btw, I am including this piece of code in a bash file that performs other operations, that is why I want to do this using sed or any other "tool" that is bash compatible.

推荐答案

带有自定义 RS (记录分隔符)的gnu-awk更适合:

gnu-awk with custom RS (record separator) suites this better:

awk -v RS="</dependency>" 'index($0, "<groupId>com.company.xyz</groupId>"){
    sub(/0\.1-SNAPSHOT/, "NEW-VERSION")} RT{$0 = $0 RT} 1' file

Awk命令分解

-v RS="</dependency>"               # sets input record separator as </dependency>
index($0, "<groupId>...")           # for lines that have give string
sub(/0\.1-SNAPSHOT/, "NEW-VERSION") # replace 0.1-SNAPSHOT by NEW-VERSION
1                                   # default action to print the line

简而言之,此awk命令将输入​​数据分成包含</dependency> 之前的多行的部分的记录,然后使用index 函数,并使用 sub 函数替换文本.

In short this awk command breaks input data into records as section containing multiple lines before </dependency> and then checks for presence of desired groupId using index function and replaces the text using sub function.

输出:

<dependencies>
        <dependency>
            <groupId>com.company.xyz</groupId>
            <artifactId>xyz-xx-utils</artifactId>
            <version>NEW-VERSION</version>
        </dependency>


        <dependency>
            <groupId>com.ibm.xyz</groupId>
            <artifactId>xyz</artifactId>
            <version>56.1</version>
        </dependency>

</dependencies>

这篇关于是否可以使用sed替换pom文件中的依赖项版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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