我正在使用 Maven 2 构建我的 Java 项目,并且正在寻找一种向用户呈现 pom.xml 当前版本号的方法(例如使用 Servlet 或 JSP)。
据我所知,最好的方法是 Maven 将版本号作为文本文件打包到 WAR 中。这使我能够从该文件中读取版本并以我想要的方式呈现它。
有谁知道有一个插件可以为我做类似的事情?也许可以配置 WAR 插件来实现这一点?或者也许一起使用其他方法?
我以稍微不同的方式解决了这个问题,因为我希望在服务的索引页面上显示版本、svn 修订版等。我使用 buildnumber-maven-plugin 和 war-plugin 将值存储在清单中。
pom.xml 片段:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Environment>${env}</Implementation-Environment>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<classifier>${env}</classifier>
</configuration>
</execution>
</executions>
</plugin>
将它们拉出来的 JSP 相当简单:
<%@ page language="java" pageEncoding="UTF-8"%>
<%
java.util.jar.Manifest manifest = new java.util.jar.Manifest();
manifest.read(pageContext.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
java.util.jar.Attributes attributes = manifest.getMainAttributes();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Health Check</title>
</head>
<body>
<h1>Health Check</h1>
<h2>Version: <%=attributes.getValue("Implementation-Version")%>-<%=attributes.getValue("Implementation-Environment")%></h2>
<h2>SVN Revision: <%=attributes.getValue("Implementation-Build")%></h2>
</body>
</html>
这显示了类似的内容:
Version: 2.0.1-SNAPSHOT-QA
SVN Revision: 932
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)