簡體   English   中英

物理上更改Maven pom.xml <properties>值

[英]Changing Maven pom.xml <properties> values physically

我正在為幾個相互依賴的項目構建一個部署管道。 每個構建都會生成一個具有唯一版本號的新版本,該版本號部署到Maven存儲庫。 然后,使用該新版本作為依賴項觸發管道中的下游項目,並以類似方式構建。

我需要的是在構建項目之前更改pom.xml(或多模塊項目中的所有poms)中的屬性值。 例如,在下面的代碼中,“0.1.200”將更改為“0.1.345”(或者無論最新的內部版本號是什么)。 使用系統屬性不是一種選擇,因為更新的pom將部署在Maven存儲庫中,因此更改必須是持久的。

<properties>
    <foo.version>0.1.200</foo.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>foo</artifactId>
        <version>${foo.version}</version>
    </dependency>
</dependencies>

是否有一些Maven插件使用一個命令行指令執行此操作? 否則我需要編寫一個簡短的腳本(例如在Ruby中),它解析並更改項目中的所有pom.xml文件。

是的,有一個maven-replacer-plugin就可以了。

但是如果你正在使用一些CI工具(顯然你是),你也可以為此目的堅持使用自定義腳本。

可用的Maven插件不適合我的目的,所以我最終編寫了以下Ruby腳本,它完全符合我的需要:

#!/usr/bin/env ruby
require 'rexml/document'

def change_property(pom, key, value)
  property = pom.elements["/project/properties/#{key}"]
  if property != nil
    puts "    #{key}: #{property.text} -> #{value}"
    property.text = value
  end
end

unless ARGV.length == 2
  puts "Usage: #{$0} KEY VALUE"
  exit 1
end
KEY = ARGV.shift
VALUE = ARGV.shift

Dir.glob("**/pom.xml") { |pom_path|
  puts pom_path

  pom = REXML::Document.new(File.new(pom_path))
  pom.context[:attribute_quote] = :quote
  change_property(pom, KEY, VALUE)

  File.open(pom_path, 'wb') { |file|
    pom.write(file)
  }
  puts
}

你試過這個嗎?

版本Maven插件

我有類似的要求。 除了更新/ project / properties節點下的屬性外,我還需要更新/ project / profiles / properties節點下的屬性,因此我修改了Esko的腳本以支持更新這兩種情況。 同時,它還支持在一個命令中更新多個屬性,因此如果需要在同一個pom.xml中更新多個屬性,則不必多次運行它。

#!/usr/bin/env ruby
require 'rexml/document'

def change_profile_property(pom, profile_id, key, value)
  property = pom.elements["/project/profiles/profile[id='#{profile_id}']/properties/#{key}"]
  if property != nil
    puts "    #{profile_id}-#{key}: #{property.text} -> #{value}"
    property.text = value
  end
end

def change_property(pom, key, value)
  property = pom.elements["/project/properties/#{key}"]
  if property != nil
    puts "    #{key}: #{property.text} -> #{value}"
    property.text = value
  end
end

if ARGV.length == 0
  puts "Usage: #{$0} KEY=VALUE [-profile <profile id>] KEY=VALUE"
  exit 1
end

# parse the command line argument to get the key/value
global_properties = Array.new
profile_properties= Array.new

profile = nil
loop { case ARGV[0]
  when '-profile' then  ARGV.shift; profile=ARGV.shift
  when nil then break
  else 
    kv_str = ARGV.shift
    if profile == nil
      global_properties.push(kv_str)
    else
      profile_properties.push(kv_str)
    end
  end; 
}

Dir.glob("**/pom.xml") { |pom_path|
  puts pom_path

  pom = REXML::Document.new(File.new(pom_path))
  pom.context[:attribute_quote] = :quote

  # updating the global properties
  if global_properties.length != 0
    for kv in global_properties
      kv_array = kv.split('=')
        if kv_array.length == 2
          change_property(pom, kv_array[0], kv_array[1])
        end
    end
  end

  # updating the properties in profile
  if profile_properties.length != 0
    for kv in profile_properties
      kv_array = kv.split('=')
      if kv_array.length == 2
        if profile != nil
          change_profile_property(pom, profile, kv_array[0], kv_array[1])
        end
    end
    end
  end

  File.open(pom_path, 'wb') { |file|
    pom.write(file)
  }
  puts
}

Flatten Maven插件可用於更新變量。 讓我舉個例子來解釋一下。

<groupId>groupid</groupId>
<artifactId>artifactid</artifactId>
<version>${ServiceVersion}</version>
<packaging>pom</packaging>

<properties>
    <foo.version>${ServiceVersion}</foo.version>
</properties>

在pom.xml中,我使用“ServiceVersion”來獲取構建期間的值。 在構建期間,foo.version變量也將更新。 現在,foo.version變量可以用在任何依賴項中。

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>foo</artifactId>
        <version>${foo.version}</version>
    </dependency>
</dependencies>

所以,最后在pom.xml中包含Flatten Maven插件

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
    <updatePomFile>true</updatePomFile>
</configuration>
 <executions>
    <execution>
        <id>flatten</id>
        <phase>process-resources</phase>
        <goals>
            <goal>flatten</goal>
        </goals>
    </execution>
    <execution>
      <id>flatten.clean</id>
      <phase>clean</phase>
      <goals>
        <goal>clean</goal>
      </goals>
    </execution>
  </executions>
</plugin>

現在提供服務版本。

clean install -DServiceVersion=0.1.345

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM