如何在基于 Spring 的 Web 应用程序中显示内部版本号

发布于 2024-09-08 05:11:13 字数 282 浏览 4 评论 0原文

我需要在我的 index.jsp 页面中显示内部版本号

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Title (build: BUILDNUMBER )
</head>

内部版本号可以由 maven 提供到 *.properties 文件中。 读取 *.properties 文件并使用 Spring 显示属性的最佳方法是什么?

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 is the best way to read *.properties file and display a property with Spring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-09-15 05:11:13

您可以加载 .properties 文件作为本地化消息源(使用 ResourceBundlerMessageSource) 并使用 在 JSP 中访问它或 :

src/main/resources/buildInfo.properties

buildNumber=${buildNumber}

其中 buildNumber 按照 Roland Schneider 的建议公开。

上下文配置:

<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>

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>
终难愈 2024-09-15 05:11:13

这是我的做法,使用 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 的使用的更多详细信息可以在 使用页面

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.

叹沉浮 2024-09-15 05:11:13

警告:资源过滤对于 .jsp 文件不起作用。正如 Pascal Thivent 指出的(谢谢),index.jsp 不是资源,而是属于 web 应用程序。


我不知道你的问题的确切答案,但当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 过滤手册

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文