2012-04-03 104 views
0

我有一些POM文件在我的項目結構如下找到,並在特定塊

<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/maven-v4_0_0.xsd"> 
    <parent> 
      <artifactId>xparent</artifactId> 
      <groupId>y</groupId> 
      <version>2.0.0</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 
    <artifactId>someparent</artifactId> 
    <version>x.x.x-needs_change</version> 
    <packaging>pom</packaging> 
    <name>some name</name> 
    <description>some description</description> 
    <url>myurl</url> 

    <modules> 
     <module>mymodules</module> 
    </modules> 

    <properties> 
     <my.version>x.x.x-needs_change</my.version> 
    </properties> 

    <dependencies> 
    <dependency> 
    <groupId>hhhhh</groupId> 
    <artifactId>hhhhh</artifactId> 
    <version>x.x.x-should not change</version> 
    </dependency> 
    </dependencies> 
</project> 

我使用SED給當前版本作爲輸入,將其更改爲賦予了新的版本替換版本。但我不想在依賴塊中更改版本。我該怎麼做呢?

我不想去maven版本的插件路線。我已經嘗試過了,並不符合我的要求。

我更喜歡sed/python腳本。

感謝

回答

2

你可以嘗試:

sed -e '/<dependencies>/,/<\/dependencies>/ !{ 
     s!<version>[0-9.]\+</version>!<version>'"$NEWVERSION"'</version>! 
     }' MY_FILE 

/<dependencies>/,/<\/dependencies>/說: 「找到<dependencies></dependencies>之間的所有行」。

後的!說:「執行以下操作到處那些線(即做了<dependencies></dependencies>之間除了所有行)」。

s!<version>[0-9.]\+</version>!<version>'"$NEWVERSION"'</version>!說:「與<version>$NEWVERSION</version>,其中$NEWVERSION是包含新的版本號一些環境變量替換<version>...</version>

的用引號('"$NEWVERSION"')是因爲我想單引號的主要部分放屁各地sed命令(所以我不擔心感嘆號&反斜槓),但我$NEWVERSION進行擴展。

修改,以適應你的腳本。

+1

不錯,比我想出了一個版本更清潔。稍微改進就是使用'\'(\\ \)[0-9。] \ + \( \)!\ 1'「$ NEWVERSION」'\ 2!'來保存幾個字符。 – 2012-04-04 00:03:28

+0

我認真的認爲_Do不使用正則表達式來解析html_規則應該擴展爲_do不使用sed來修改xml_ :) – Kimvais 2012-04-04 07:32:11

+0

是的,但是對於所有人來說,「不要這樣做!「,在某些情況下,它可能是適當的,這取決於您對輸入的控制權(例如,生成的HTML/XML可以依賴於與用戶輸入相對應的格式),作業是否是一次-off,代碼是誰(我很樂意在我的個人項目中使用正則表達式)以及用sed寫一個腳本和用XML/HTML解析器編寫腳本的時間投入(特別是如果regex對於你來說已經是第二天性了) – 2012-04-04 23:37:23

0
nawk '{ 
     a=$0; 
     getline; 
     if($0!~/depend/ && a!~/version/) 
     {gsub(/2.0.0/,"1.0.0",$0);print a"\n"$0} 
     else print a"\n"$0 
     }' file3 

下面是測試:

pearl.302> cat file3 
    <parent> 
    <aritifactID> </artifactID> 
    <groupID> </groupID> 
    <version>2.0.0</version> 
    </parent> 

    <properties> 
    <version>2.0.0</version> 
    </properties> 

    <dependencies> 
    <dependency> 
    <version>2.0.0</version> 
    </dependency> 
    </dependencies> 
pearl.303> nawk '{a=$0; 
        getline; 
        if($0!~/depend/ && a!~/version/) 
        {gsub(/2.0.0/,"1.0.0",$0);print a"\n"$0} 
        else print a"\n"$0 }' file3 
    <parent> 
    <aritifactID> </artifactID> 
    <groupID> </groupID> 
    <version>1.0.0</version> 
    </parent> 

    <properties> 
    <version>1.0.0</version> 
    </properties> 

    <dependencies> 
    <dependency> 
    <version>2.0.0</version> 
    </dependency> 
    </dependencies> 
+0

這看起來很脆弱如果在「版本」和「依賴」之間有一條界限怎麼辦 – 2012-04-04 13:06:13

1

解析和修改XML - 你真的應該使用XML感知的解析器,如​​而不是文本工具,如sedawk

我假設您的POM文件確實是有效的POM文件,即它們也包含<project>標記。

>>> t = """<project> 
... <parent> 
... <artifactID> </artifactID> 
... <groupID> </groupID> 
... <version>2.0.0</version> 
... </parent> 
... 
... <properties> 
... <version>2.0.0</version> 
... </properties> 
... 
... <dependencies> 
... <dependency> 
... <version>2.0.0</version> 
... </dependency> 
... </dependencies></project> 
... """ 
>>> from lxml import etree 
>>> r = etree.fromstring(t) 
>>> r.xpath("//parent/version")[0].text = "3.0" 
>>> r.xpath("//properties/version")[0].text = "3.0" 
>>> print(etree.tostring(r)) 
<project> 
<parent> 
<artifactID> </artifactID> 
<groupID> </groupID> 
<version>3.0</version> 
</parent> 

<properties> 
<version>3.0</version> 
</properties> 

<dependencies> 
<dependency> 
<version>2.0.0</version> 
</dependency> 
</dependencies></project> 
+0

如果輸入是一個文件名怎麼辦?我嘗試了t = etree。 parse(filename_to_update)r = etree.tostring(t)r.xpath(「// parent/version」)[0] .text = newversion r.xpath(「// properties/version」)[0] .text = newversion但得到一個錯誤AttributeError:'str'對象沒有屬性'xpath'。一個喵這個。所以我不知道如何解決這個問題 – user1164061 2012-04-04 20:02:25

+0

你o只需要調用'.tostring()'_after_你已經更新了版本,所以你應該在_parsed tree_上調用't.xpath()'而不是在_string_上。 – Kimvais 2012-04-05 05:09:57

+0

現在我試着t = etree.parse(filename_to_update) t.xpath(「// parent/version」)[0] .text = newversion t.xpath(「// properties/version」)[0] .text = newversion,但我得到 IndexError:列表索引超出範圍。我也試圖擴展/父母/版本到/父/ artifactId/groupId /版本,但我仍然得到相同的錯誤 – user1164061 2012-04-05 18:29:43

0
awk -v change=1 -v newver=2.3.4 ' 
    change && /version/ {sub(/>[^<]+/, ">" newver)} 
    /<\/?dependencies>/ {change = !change} 
    {print} 
' 
相關問題