跳到主要內容

[Maven] 增加建構 (build) 過程的資訊到 Manifest 中

自動產生建構相關的資訊

在部署到伺服器後,有時會弄不清楚這個到底是根據哪一個 commit 發布的、何時發布的、某個函示庫使用哪個版本,或是

  • 不想一直修改版號,想要用發布時間代替版號
  • 頻繁的部署到伺服器,甚至部署到不同的伺服器,你想知道目前這個伺服器上目前到底是哪一版、或執行哪個 commit 的 code

這時你可以把建構過程的資訊加入 manifest 中,再由程式讀出來顯示在適合的地方。

buildnumber 產生 git commit, git 分支 (branch) 等資訊

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <goals><goal>create</goal></goals>
        </execution>
    </executions>
    <configuration>
        <revisionOnScmFailure>0</revisionOnScmFailure>
        <useLastCommittedRevision>true</useLastCommittedRevision>
    </configuration>
</plugin>

build-helper 產生台灣時間 (GMT+8)。因為 project.build.timestamp 是 UTC+0 時間。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <execution>
            <id>timestamp-property</id>
            <goals>
                <goal>timestamp-property</goal>
            </goals>
            <configuration>
                <name>tw-timestamp</name>
                <pattern>yyyy/MM/dd HH:mm:ss</pattern>
                <timeZone>GMT+8</timeZone>
            </configuration>
        </execution>
    </executions>
</plugin>

再把以上產生的資訊,加到 manifest 中。

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <archive>
            <manifestEntries>
                <Build-Time>${tw-timestamp}</Build-Time>
                <Project-Version>${project.version}</Project-Version>
                <Git-Commit>${buildNumber}</Git-Commit>
                <Git-Branch>${scmBranch}</Git-Branch>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

可用 ServletContext 讀取 manifest 並顯示在頁面上。

InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");

遇到錯誤

我遇到幾個錯誤訊息如下:

  • The scm url cannot be null
    pom.xml 中 scm.url 必須設定

  • No such provider: ''
    <connection> 內容語法錯誤,沒有填 provider,正確語法應該是 scm:[PROVIDER]:[git repository URL]

<scm>  
 <connection>scm:git:http://gitlab.abc.com/abc/abc.git</connection>  
 <url>http://gitlab.hawk.com/abc/abc.git</url>
</scm>

其實 buildnumber plugin 也可以產生時間,預設插入 millisecond,但是我只要一設定日期格式,maven 就會插入我設定的語法字串如: yyyy/MM/dd,而不是日期字串,到 manifest 中,因此只好放棄改用 build-helper。

留言

這個網誌中的熱門文章

iframe DOM 被移動造成重新載入

如果用 javascript 去搬動 iframe DOM 的位置, 瀏覽器會將其內容重新載入,這是現有 HTML 規格 > When an iframe element is inserted into a document that has a browsing context, the user agent must create a nested browsing context, and then process the iframe attributes for the "first time". 範例: http://jsfiddle.net/pZ23B/ 測試結果: * Safari 3.1 / Win: reload * Opera 9.5 / Win: reload * IE10: reload * IE7 / IE8: not reload (部份摘自 https://bugzilla.mozilla.org/show_bug.cgi?id=254144 ) 參考: * https://bugzilla.mozilla.org/show_bug.cgi?id=254144

JavaScript 關掉瀏覽器頁面

如果你直接呼叫 window.close() 來關掉目前的頁面的話,你應該會在 console 看到以下訊息: Scripts may close only the windows that were opened by it. ( 我在 Chrome, Firefox, IE 11 都試過) 主因就是你並沒有用 JavaScript 開啟這個頁面,所以也不能用 JavaScript 關掉它。這是 HTML window.close() 的規格 規定,各家瀏覽器應該都遵循。 隨著瀏覽器安全性增加,以下方法已經不適用了,可參考 這個討論 。 有個變通的辦法就是: window.open(location, '_self').close();

Web Dynpro 前後端資料流動機制 (Dataflow)

在Web Dynpro中提供3種資料流機制[1],只要適當地設定,可以不用寫程式就將畫面、中間層控制器(controller)到後端模型物件的資料自動化地、牢靠地綁在一起,使得不管前後端某一方有資料變動,變動部份都會自動地流動來保持一致性,使得前後端都能存取到同一份資料。 context 關鍵元件是 context,每個controller都有一份屬於自己的context,它扮演MVC架構中的M (model),web dynpro的實做方式比較像是該controller的「資料空間」,它由node(資料節點)與attribute(資料屬性)組成,controller可以透過wdContext這個預先產生好的 shortcut variable(捷徑變數)去取得context的資料內容。 context中必須要建立node才可以儲存資料,一個node代表一個collection(集合物件)裡面仍可以含有node, attribute,node裡面的一份資料實體就是一個element,一個node可以有一個或多個element(這部份可以透過cardinality property設定),其結構就是該node所包含的結構。 data binding 此種機制可以將UI元件的資料跟context中的某個node或attribute綁定在一起,context中的改變會自動 更新到UI元件上,UI元件的改變也會自動寫入到綁定的context node(or attribute)中。通常UI元件所綁定的node(attribute)是由component controller對應過來的。 context mapping 每一個controller都有屬於自己的context(資料空間)如果要達到彼此共享資料,則要透過context mapping機制,一旦mapping設定好,則會在另一個context產生一個同樣的結構的node,兩邊的controller會存取到的是同一分資料,任何一邊的資料更動都會散佈到設定好mapping的node。 不過在web dynpro裡,只允許將custom controller or component controller的node對應到view controller去,不允許從view controller對應回來,主因是嚴格遵守MVC...