bash 如何从bash中的pom文件中删除版本号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29420216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:42:52  来源:igfitidea点击:

How can I cut the version number out of a pom file in bash

xmlbashmavensedgrep

提问by Alex Thomson

I have a pom file that has the following information:

我有一个 pom 文件,其中包含以下信息:

<modelVersion>4.0.0</modelVersion>
  <groupId>com.site.camera</groupId>
  <artifactId>proj3</artifactId>
  <packaging>pom</packaging>
  <version>2.6</version>
  <name>Proj 3</name>

I am writing a bash script and want to be able to open this file (pom.xml) and cut out the version (eg. 2.6 in this case). I am doing this because as I update the version I want the rest of my script to update with it.

我正在编写一个 bash 脚本并希望能够打开这个文件 (pom.xml) 并删除版本(例如,在这种情况下为 2.6)。我这样做是因为当我更新版本时,我希望我的脚本的其余部分也随之更新。

Things that I have tried:

我尝试过的事情:

var=$(grep <version>2.6</version> pom.xml)
echo $var

Note that I have multiple version parameters in the pom file. And I need this specific version which is surrounded with this name, packaging, artifactId etc.

请注意,我在 pom 文件中有多个版本参数。我需要这个特定的版本,它被这个名称、包装、artifactId 等包围。

采纳答案by glenn Hymanman

The simplest way is to use GNU grep's perl-compatible regexes

最简单的方法是使用 GNU grep's perl-compatible regexes

grep -oP '<version>\K[^<]+' pom.xml

With sed, you'd write

sed -n 's/^[[:blank:]]*<version>\([^<]\+\).*//p' pom.xml
grep -oP '<version>\K[^<]+' pom.xml

使用 sed,你会写

sed -n 's/^[[:blank:]]*<version>\([^<]\+\).*//p' pom.xml

Thanks to David W.'s thoughtful commentary: the solution must use an XML parser. xmlstarletis one such tool:

感谢 David W. 深思熟虑的评论:解决方案必须使用 XML 解析器。xmlstarlet是这样一种工具:

ver=$( xmlstarlet sel -t -v /project/version pom.xml )

And just to be different, ruby comes with an XML parser in its standard library:

只是为了不同,ruby 在其标准库中附带了一个 XML 解析器:

ruby -r rexml/document -e 'puts REXML::Document.new(File.new(ARGV.shift)).elements["/project/version"].text' pom.xml

回答by that other guy

Some people object to parsing XML with regex. Here's how you can do it correctly and robustly with xmlstarlet:

有些人反对用 regex 解析 XML。以下是如何正确且稳健地执行此操作xmlstarlet

$ cat pom.xml 
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.site.camera</groupId>
  <artifactId>proj3</artifactId>
  <packaging>pom</packaging>
  <version>2.6</version>
  <name>Proj 3</name>
</project>

$ xmlstarlet sel -t -v '/project/version' pom.xml 
2.6

This works equally well after XML tools and editors have had their way with your document:

在 XML 工具和编辑器处理您的文档后,这同样有效:

$ cat pom.xml 
<project>
  <version><![CDATA[2]]>&#x002E;6
  </version>
</project>

$ xmlstarlet sel -t -v '/project/version' pom.xml 
2.6

Edit: As a testament to Doing It Right(tm), this solution still works fine after it was pointed out that there will be a lot of different version tags.

编辑:作为对做对 (tm) 的证明,在指出会有很多不同的版本标签后,该解决方案仍然可以正常工作。

回答by Vytenis Bivainis

Using maven you do it this way:

使用 Maven 你这样做:

echo '${project.version}' | mvn help:evaluate | grep -v '^[[]'

回答by Chris Maes

try like this:

试试这样:

var=$(grep -Po '<version>\K[^<]*' pom.xml)
echo $var

output:

输出:

2.6
  • -P : use perl regexp format
  • -o : print matching part only
  • -P : 使用 perl regexp 格式
  • -o : 只打印匹配部分

回答by David C. Rankin

Or without perl compatible regular expressions using parameter expansion/substring extraction:

或者没有使用参数扩展/子字符串提取的perl 兼容正则表达式:

$ var=$(grep '<version>' pom.xml); ver=${var%<*}; ver=${ver#*>}; echo $ver
2.6

I would prefer the perl compatibile RE if you have that available.

如果你有可用的,我更喜欢 perl 兼容的 RE。

Note:As pointed out by David W. this works for the example given, but will return multipleversionstrings if the input file has more than one line containing <version>. The full pom.xmlfile referenced below in the comments contains multiple<version>strings. An additional loop will be required to process all versionlines and without a unique tag, it is not possible to determine which versionis the desired version. For example parsing the current full pom.xmlwith:

注:正如所指出的大卫W.这适用于给出的例子,但将返回多个version字符串如果输入文件具有包含多行<version>pom.xml下面评论中引用的完整文件包含多个<version>字符串。将需要一个额外的循环来处理所有version行,如果没有唯一标签,则无法确定哪个version是所需的版本。例如解析当前完整pom.xml的:

$ for i in $(grep '<version>' pom.xml); do ver=${i%<*}; ver=${ver#*>}; echo "version: $ver"; done

Returns:

返回:

version: 33
version: 3.3.2
version: 4.11
version: 2.4
version: 3.2
version: 2.9.1
version: 2.5.2
version: 2.5.1
version: 2.4
version: 3.0.1
version: 2.4
version: 2.0
version: 1.7

回答by Tiwari

My Requirement was also similar. We are using SNAPSHOT at the end of our version so I did this to find out the Version number (Extension of David's answer)

我的要求也是类似的。我们在版本的末尾使用了 SNAPSHOT,所以我这样做是为了找出版本号(大卫的答案的扩展)

for i in $(grep '<version>' pom.xml); do
    #echo "$i"; 
    ver=${i%<*}; 
    ver=${ver#*>};
    if [[ $ver == *"SNAPSHOT"* ]]
        then
            echo "$ver";
    fi 

done

And It gives me an output; Which I wanted

它给了我一个输出;我想要的

1.09-SNAPSHOT

1.09-快照