2013-03-02 25 views
6

我正在爲一些相互依賴的項目構建部署管道。每個版本都會生成一個具有唯一版本號的新版本版本,該版本號將部署到Maven存儲庫中。然後,管道中的下游項目將以該新版本作爲依賴項被觸發,並以類似的方式構建。更改Maven pom.xml <properties>物理上的值

我需要的是在構建項目之前更改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文件。

回答

1

你試過這個嗎?

Version Maven Plugin

+2

這插件具有自動更新依賴於他們的最新版本的操作,但我沒有發現有更新單個屬性爲特定值的方式。 – 2013-03-02 16:46:02

+2

[versions-maven-plugin](http://mojo.codehaus.org/versions-maven-plugin/examples/update-properties.html)能夠處理這些屬性。看看文檔。 – khmarbaise 2013-03-02 17:29:30

+2

版本問題:更新屬性是它會自動嘗試檢測最新版本。在一個不好的部署管道中,因爲它可能會意外地導致Maven找到過時或新版本。爲了避免不可重複的構建,我需要明確使用哪個版本。 – 2013-03-02 18:32:34

3

是,存在一個maven-replacer-plugin其能夠這一點。

但是,如果您使用的是一些CI工具(顯然您是),您可以使用自定義腳本來達到此目的。

2

可用的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 
} 
1

我也有類似的要求。除了更新/ 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 
} 
0

Flatten Maven Plugin可用於更新變量。讓我用一個例子來解釋。

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

所以,最後包括平化Maven插件在pom.xml

<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