簡體   English   中英

使用 buildnumber-maven-plugin 檢索 svn 上次更改的修訂號

[英]Retrieve svn last changed revision number with the buildnumber-maven-plugin

我目前正在為我的應用程序開發基於微服務的架構。 我有一個 maven 多模塊項目,它有很多服務,所以我可以使用 maven deploy 命令和一個maven docker plugin輕松地將它們部署到 docker hub。

盡管如此,docker 鏡像標簽還是基於項目版本號,而我希望用每個存儲庫的最后更改的修訂號來標記它們。 目前,我只是嘗試使用buildnumber-maven-plugin將此字段添加為清單條目:

假設這是我的多模塊項目:

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

    <modules>
        <module>module-a</module>
        <module>module-b</module>
    </modules>

...

</project>

模塊 a 的模型是:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    ...


    <scm>
        <connection>scm:svn:http://myrepo.com/svn/application/module-a</connection>
    </scm>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <doCheck>false</doCheck>
                    <doUpdate>true</doUpdate>
                    <useLastCommittedRevision>true</useLastCommittedRevision>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <SCM-Revision>${buildNumber}</SCM-Revision>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            ....
        </plugins>
    </build>

    <dependencies>
        ...
    </dependencies>

</project>

問題是 {buildNumber} 計算為我的工作副本號,這是指對存儲庫所做的最后一次提交而不是scm:svn:http://myrepo.com/svn/application/module-a位置。 為了更好地解釋它,當我從 tortoise 顯示 module-a 的屬性時,我得到了這個:

在此處輸入圖片說明

我想要的是檢索 3248,它指的是對模塊 a 所做的最后一次真正的更改,而不是 3257(工作副本),這是我從插件中得到的。 這樣 docker 插件就會知道它是否是一個不同的圖像標簽,並且只有在對 repo 中的模塊進行了更改時才會推送它。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>create</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <doCheck>false</doCheck>
        <doUpdate>true</doUpdate>
        <useLastCommittedRevision>true</useLastCommittedRevision>
    </configuration>
</plugin>

這對我有用。

我也無法讓buildnumber-maven-plugin用於相同的場景。 <useLastCommittedRevision>在 v1.4 中不起作用,在 v1.3 中它不會返回最后更改的修訂版,而是在某些情況下返回倒數第二個修訂版。

有一個巧妙的解決方法,您可以從svn info命令輸出中獲取最后更改的修訂號。 兩個選項取決於您的 SVN 版本:

  1. 對於SVN v1.9或更新版本,使用maven-antrun-plugin運行svn info --show-item=last-changed-revision並將輸出保存到屬性中。

     <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <exportAntProperties>true</exportAntProperties> <target> <exec executable="svn" outputProperty="svn.info"> <arg value="info"/> <arg value="--show-item=last-changed-revision"/> </exec> </target> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestEntries> <SCM-Revision>${svn.info}</SCM-Revision> </manifestEntries> </archive> </configuration> </plugin>
  2. 對於早於 v1.9 (在我的情況下為 v1.8.13 )的SVN 版本,您可以使用maven-antrun-plugin運行svn info --xml並將輸出保存到屬性中,並使用build-helper-maven-plugin來解析該屬性的內容使用正則表達式,匹配上次更改的修訂號並將其保存到第二個屬性中。

    我選擇添加--xml參數是因為基本svn info輸出根據安裝的語言進行翻譯,但 xml 輸出格式始終相同。

     <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <exportAntProperties>true</exportAntProperties> <target> <exec executable="svn" outputProperty="svn.info"> <arg value="info"/> <arg value="--xml"/> </exec> </target> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>regex-svn-last-rev</id> <goals> <goal>regex-property</goal> </goals> <configuration> <!-- Output property --> <name>svn.last.rev</name> <value>${svn.info}</value> <regex>^[\\s\\S]*?commit[\\s\\S]*?revision=\\"(\\d+?)\\"[\\s\\S]*?$</regex> <replacement>$1</replacement> <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestEntries> <SCM-Revision>${svn.last.rev}</SCM-Revision> </manifestEntries> </archive> </configuration> </plugin>

我想你在這里唯一錯過的是

<doCheck>false</doCheck>

我想應該是true

也偶然發現了這個問題,我覺得有必要深入了解它:這似乎是 buildnumber-maven-plugin 本身的一個相當老的問題: 實施“Last Changed Rev”而不是“Revision”字段

實際上已經提交了一個修復程序,但此后沒有發布新版本。

最簡單的選擇是簽出/下載代碼並在本地構建它,確保將其放入組織的中央存儲庫中。

如果這不是一個選項,請嘗試 jitpack 直接獲取最新版本作為 maven 依賴項: Can I use a GitHub project directly in Maven? => https://stackoverflow.com/a/28483461/167865

...
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
...
<dependency>
    <groupId>com.github.mojohaus</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>e74e373</version>
</dependency>
...

這是 maven-scm-provider-svnexe 中眾所周知的錯誤。 還是沒有修好。

暫無
暫無

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

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