我需要在index.jsp页面中显示内部版本号
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Title (build: BUILDNUMBER )
</head>
构建号可以由maven提供到* .properties文件中。 读取* .properties文件并使用Spring显示属性的最佳方法是什么?
答案 0 :(得分:6)
您可以将.properties
文件作为本地化消息源加载(使用ResourceBundlerMessageSource
),并使用<spring:message>
或<fmt:message>
在JSP中访问它:
src/main/resources/buildInfo.properties
:
buildNumber=${buildNumber}
Roland Schneider建议将buildNumber
暴露出来。
上下文配置:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name = "basenames"><value>buildInfo</value></property>
<!-- Or a comma separated list if you have multiple .properties files -->
</bean>
JSP文件:
Version: <spring:message code = "buildNumber" />
pom.xml
:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
答案 1 :(得分:4)
以下是我使用maven + jstl完成它的方法,而忽略了Spring,因为它使它变得更复杂。
build.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="dateValue" class="java.util.Date" />
<jsp:setProperty name="dateValue" property="time" value="${timestamp}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Build info</title>
</head>
<body>
<table>
<tr>
<td>Build time: </td>
<td><fmt:formatDate value="${dateValue}" pattern="dd.MM.yyyy HH:mm:ss" /></td>
</tr>
<tr>
<td>Build number: </td>
<td>${buildNumber}</td>
</tr>
<tr>
<td>Branch: </td>
<td>${scmBranch}</td>
</tr>
</table>
</body>
</html>
Maven pom
<!-- plugin that sets build number as a maven property -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
<configuration>
<format>{0,date,yyDHHmm}</format>
<items>
<item>timestamp</item>
</items>
</configuration>
</execution>
</executions>
<configuration>
<format>{0,date,yyDHHmm}</format>
<items>
<item>timestamp</item>
</items>
</configuration>
</plugin>
<!-- war plugin conf to enable filtering for our file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp/</directory>
<includes>
<include>build.jsp</include>
</includes>
<filtering>true</filtering>
<targetPath>.</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
有关buildnumber-maven-plugin用法的更多详细信息,请参见usage page。
答案 2 :(得分:1)
警告:资源过滤对.jsp文件不起作用。正如Pascal Thivent指出的那样(谢谢)index.jsp不是资源,而是属于webapp。
我不知道您问题的确切答案,但是当index.jsp文件被复制到目标目录时,您可以直接使用maven将buildnumber硬编码到index.jsp文件中。您只需要将一个变量插入index.jsp并配置maven-resource-plugin以启用过滤。
<强> 实施例 强>
<强>的index.jsp 强>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Title (build: ${buildNumber} )
</head>
Maven配置(从pom.xml中提取)
<build>
<!-- Enable Resource Filtering -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<!-- Fetch the SVN build-number into var ${buildNumber} -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
</configuration>
</plugin>
</plugins>
</build>
有关过滤的详细信息,请查看Maven Filtering Manual