简体   繁体   English

通过shell脚本替换pom依赖

[英]Replacing pom dependency through shell script

I am reading pom.xml and uplifting dependency through shell script 我正在阅读pom.xml并通过shell脚本提升依赖性

<dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.gwt.inject</groupId>
      <artifactId>gin</artifactId>
      <version>2.0</version>
    </dependency>

where I want to uplift guice version to 3.0, Is there any way where I can replace particular artifactId dependencies through shell script. 我想将guice版本提升到3.0,有什么方法可以通过shell脚本替换特定的artifactId依赖项。

So output should be as below 所以输出应该如下

<dependency>
          <groupId>com.google.inject</groupId>
          <artifactId>guice</artifactId>
          <version>3.0</version>
        </dependency>
        <dependency>
          <groupId>com.google.gwt.inject</groupId>
          <artifactId>gin</artifactId>
          <version>2.0</version>
        </dependency>

The cleanest way (imho) would be to use Properties to define versions of the dependencies in your POM. 最干净的方法(imho)是使用“ 属性 ”在POM中定义依赖项的版本。 For example like this: 例如这样:

<properties>
   <guice.version>2.0</guice.version>
   <gin.version>2.0</gin.version>
</properties>
...
<dependencies>
...
   <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>${guice.version}</version>
   </dependency>
   <dependency>
      <groupId>com.google.gwt.inject</groupId>
      <artifactId>gin</artifactId>
      <version>${gin.version}</version>
   </dependency>

This makes replacing the version of a specific dependency later on easier. 这使得以后更容易替换特定依赖项的版本。 In the future, each time you'd need to do it, you could do a simple sed : 将来,每次您需要这样做时,都可以执行一个简单的sed

sed -i 's|<guice.version>.*</guice.version>|<guice.version>3.0</guice.version>|g' pom.xml

I see someone else tagged your question with xmlstarlet. 我看到有人用xmlstarlet标记了您的问题。 If xmlstarlet is an option I don't think there would be any good reason to choose sed or awk. 如果xmlstarlet是一个选项,我认为没有任何理由选择sed或awk。

The only thing that might trip you up is POMs usually have a default namespace so you need to make sure to account for it. 唯一可能使您失望的是POM通常具有默认名称空间,因此您需要确保对其进行考虑。

Here's an example... 这是一个例子

POM XML Input POM XML输入

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.inject</groupId>
                <artifactId>guice</artifactId>
                <version>2.0</version>
            </dependency>
            <dependency>
                <groupId>com.google.gwt.inject</groupId>
                <artifactId>gin</artifactId>
                <version>2.0</version>
            </dependency>            
        </dependencies>
    </dependencyManagement>

</project>

xmlstarlet command line xmlstarlet命令行

xml ed -L -N p="http://maven.apache.org/POM/4.0.0" -u "//p:dependency[p:artifactId='guice']/p:version" -v "3.0" pom.xml

POM XML Output POM XML输出

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>3.0</version>
      </dependency>
      <dependency>
        <groupId>com.google.gwt.inject</groupId>
        <artifactId>gin</artifactId>
        <version>2.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Caution: The -L will edit the file inplace. 注意: -L将在原位置编辑文件。 See here for more details. 请参阅此处了解更多详细信息。

----- Update ----- -----更新-----

To add -SNAPSHOT to the version number, use -x (expression) instead of -v ... 要将-SNAPSHOT添加到版本号,请使用-x (表达式)而不是-v ...

xml ed -L -N p="http://maven.apache.org/POM/4.0.0" -u "//p:dependency[p:artifactId='guice']/p:version" -x "concat(normalize-space(),'-SNAPSHOT')" pom.xml

Output 输出量

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>2.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>com.google.gwt.inject</groupId>
        <artifactId>gin</artifactId>
        <version>2.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Here's a solution with sed : 这是sed的解决方案:

sed -r '/<artifactId>guice<\/artifactId>/ { N; s/(<version>).*?(<\/version>)/\13.0\2/ }' pom.xml > pom_new.xml

Here's a solution with awk : 这是awk的解决方案:

awk 'last ~ "<artifactId>guice</artifactId>" && $0 ~ "version" { gsub("<version>.*?</version>","<version>3.0</version>") } { last = $0 } 1;' pom.xml > pom_new.xml

Both commands will create new file pom_new.xml with your desired output. 这两个命令都将使用所需的输出创建新文件pom_new.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM