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

时间:2021-02-18 06:59:30

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文件中有多个版本参数。我需要这个特定的版本它包含了这个名字,包装,工艺品等等。

6 个解决方案

#1


2  

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

最简单的方法是使用GNU grep的perl兼容regexes

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

With sed, you'd write

使用sed,你要写的

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

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

感谢大卫·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

#2


6  

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

有些人反对用正则表达式解析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),这个解决方案在被指出将会有很多不同的版本标签后仍然有效。

#3


5  

Using maven you do it this way:

使用maven,您可以这样做:

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

#4


3  

try like this:

试试这样:

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

output:

输出:

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

#5


0  

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兼容机,我更喜欢它。

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

注意:正如David w所指出的那样,如果输入文件有多个包含 <版本> 的行,那么将返回多个版本字符串。砰的一声。注释中引用的xml文件包含多个 字符串。需要一个额外的循环来处理所有的版本行,并且没有唯一的标记,不可能确定哪个版本是想要的版本。例如解析当前的full 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

#6


0  

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)

我的要求也差不多。我们使用的是版本末尾的快照所以我这样做是为了找出版本号(David答案的扩展)

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快照

#1


2  

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

最简单的方法是使用GNU grep的perl兼容regexes

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

With sed, you'd write

使用sed,你要写的

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

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

感谢大卫·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

#2


6  

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

有些人反对用正则表达式解析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),这个解决方案在被指出将会有很多不同的版本标签后仍然有效。

#3


5  

Using maven you do it this way:

使用maven,您可以这样做:

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

#4


3  

try like this:

试试这样:

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

output:

输出:

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

#5


0  

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兼容机,我更喜欢它。

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

注意:正如David w所指出的那样,如果输入文件有多个包含 <版本> 的行,那么将返回多个版本字符串。砰的一声。注释中引用的xml文件包含多个 字符串。需要一个额外的循环来处理所有的版本行,并且没有唯一的标记,不可能确定哪个版本是想要的版本。例如解析当前的full 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

#6


0  

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)

我的要求也差不多。我们使用的是版本末尾的快照所以我这样做是为了找出版本号(David答案的扩展)

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快照