从基于Maven的Web应用程序获取版本字符串
打包maven項目時,它將自動在其中生成pom.properties文件,其中將包含版本,artifactId和groupId信息。 這些在運行時很方便擁有并顯示給您的Web應(yīng)用程序。 可以使用如下方法檢索它。
public class Application {private String version;public String getVersion() {if (version == null) {String res = "META-INF/maven/myapp/pom.properties";URL url = Thread.currentThread().getContextClassLoader().getResource(res);if (url == null) {version = "SNAPSHOT." + Utils.timestamp();} else {Properties props = Utils.loadProperties(res);version = props.getProperty("version");}}return version;} }聽起來不錯? 不太快! 原來,您需要做更多的技巧才能使其正常運行以進行部署。 默認(rèn)情況下,maven war插件會將您的類文件打包到WEB-INF / classs中,但pom.properties在同一級別的META-INF中,而不在WEB-INF / classes / META-INF中! 這導(dǎo)致上面的代碼無法從類路徑中找到您的資源pom.properties!
要解決此問題,您需要將以下內(nèi)容添加到pom.xml文件中:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.6</version><configuration><archiveClasses>true</archiveClasses></configuration></plugin>這將告訴maven將類和pom.properties打包到一個單獨的文件中,然后將其放在WEB-INF / lib文件夾中,而不是使用解壓縮的WEB-INF / classes版本。 這將強制由我們的getVersion()方法正確添加和讀取pom.properties。
翻譯自: https://www.javacodegeeks.com/2015/05/getting-version-string-from-a-maven-based-web-application.html
總結(jié)
以上是生活随笔為你收集整理的从基于Maven的Web应用程序获取版本字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何设置陌生电话打不进来(苹果7plus
- 下一篇: glassfish_具有GlassFis