在运行时访问工件的Maven和SCM版本
您可以輕松地告訴Maven在JAR清單文件中包含工件的版本及其Git / SVN /…修訂版,然后在運(yùn)行時(shí)通過getClass()。getPackage訪問該信息。 getImplementationVersion() 。 (所有功勞歸功于MarkusKrüger和其他同事。)
在清單中包括Maven工件版本
(注意:如果您還想包含SCM修訂版,則實(shí)際上將不希望使用它;請(qǐng)參見下文。)
pom.xml:
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId>...<configuration><archive><manifest><addDefaultImplementationEntries>true</addDefaultImplementationEntries><addDefaultSpecificationEntries>true</addDefaultSpecificationEntries></manifest></archive></configuration>...</plugin></plugins></build>... </project>然后,JAR文件的結(jié)果MANIFEST.MF將包括以下條目,其值來自指示的屬性:
Built-By: ${user.name} Build-Jdk: ${java.version} Specification-Title: ${project.name} Specification-Version: ${project.version} Specification-Vendor: ${project.organization.name Implementation-Title: ${project.name} Implementation-Version: ${project.version} Implementation-Vendor-Id: ${project.groupId} Implementation-Vendor: ${project.organization.name}(規(guī)范供應(yīng)商和實(shí)施供應(yīng)商來自POM的組織/名稱。)
包括SCM修訂版
為此,您可以使用生成屬性$ {buildNumber}的內(nèi)部版本號(hào)Maven插件 ,也可以從Jenkins或Hudson(對(duì)于Subversion為SVN_REVISION,對(duì)于Git為GIT_COMMIT) 傳遞的環(huán)境變量中檢索它。
僅對(duì)于git,您還可以使用maven-git-commit-id-plugin ,該插件可以替換現(xiàn)有資源文件中的字符串,例如$ {git.commit.id}(使用maven的資源過濾,必須啟用)。實(shí)際值或?qū)⑺鼈內(nèi)枯敵龅絞it.properties文件中。
讓我們使用buildnumber-maven-plugin顯式創(chuàng)建清單條目,其中包含內(nèi)部版本號(hào)(即修訂版)
<project><build><plugins><plugin><!-- Create the property $buildNumber holding the current Git revision --><groupId>org.codehaus.mojo</groupId><artifactId>buildnumber-maven-plugin</artifactId><version>1.2</version><executions><execution><phase>validate</phase><goals><goal>create</goal></goals></execution></executions><configuration><doCheck>false</doCheck><doUpdate>false</doUpdate></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.4</version><configuration><archive><manifestEntries><Implementation-Title>${project.name}</Implementation-Title><!-- buildNumber is produced at runtime by buildnumber-maven-plugin --><Implementation-Version>${project.version} ${buildNumber}</Implementation-Version></manifestEntries></archive></configuration></plugin> ...訪問版本和修訂版
如上所述,您可以通過getClass()。getPackage.getImplementationVersion()和getClass()。getPackage.getImplementationTitle()訪問代碼中的清單條目。
資源資源
- SO:如何在運(yùn)行時(shí)獲取Maven Artifact版本?
- Maven存檔器文檔
翻譯自: https://www.javacodegeeks.com/2013/05/accessing-an-artifacts-maven-and-scm-versions-at-runtime.html
總結(jié)
以上是生活随笔為你收集整理的在运行时访问工件的Maven和SCM版本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: DDoS防火墙(ddos网页防火墙)
- 下一篇: 查房子备案怎么查询不到(查房子备案怎么查
