How to display buildnumber in spring-based web application
I need to display build number in my index.jsp page
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Title (build: BUILDNUMBER )
</head>
The build number can be supplied by maven into a *.properties file. What开发者_StackOverflow中文版 is the best way to read *.properties file and display a property with Spring?
You may load the .properties
file as a localization message source (using ResourceBundlerMessageSource
) and access it in JSP using <spring:message>
or <fmt:message>
:
src/main/resources/buildInfo.properties
:
buildNumber=${buildNumber}
where buildNumber
is exposed as Roland Schneider suggests.
Context configuration:
<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 file:
Version: <spring:message code = "buildNumber" />
pom.xml
:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Here's how I've done it, using maven + jstl and leaving out Spring as it just makes it more complicated.
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>
More details about the usage of buildnumber-maven-plugin can be found on the usage page.
Warning: Resources filtering does not work this way for .jsp files. As Pascal Thivent pointed out (thank you) an index.jsp is not a resource but belongs to the webapp.
I do not know the exact answer to your question but you could hard-code the buildnumber into the index.jsp file with maven directly when the index.jsp file is copied to the target directory. You only would need to insert a variable into the index.jsp and configure the maven-resource-plugin to enable filtering.
Example:
index.jsp
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Title (build: ${buildNumber} )
</head>
Maven Configuration (extract from 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>
For more information on filtering have a look at the Maven Filtering Manual
精彩评论