Displaying version and date of build in the xhtml page

被刻印的时光 ゝ 提交于 2019-12-04 13:18:24

问题


I want to display the build version and build date on the footer of a JSF application. The pages are XHTML. I'm looking for ways to get the information from pom.xml or other artifacts.

I found the following that uses maven-replace plugin. http://www.vineetmanohar.com/2010/09/how-to-display-maven-project-version-in-your-webapp/

Are there any other techniques you use?

I'm looking for something like this with JSF - Displaying the build date


回答1:


One approach that will work: use Maven filtering to put a file in your WAR or JAR containing the required information. Then in your Java webapp, load that file's contents as a ClassPath resource InputStream.

Create a file (let's say "buildInfo.properties") under src/main/resources containing something like:

build.version=${project.version}
build.timestamp=${timestamp}

Note that due to an open defect, you need to define the timestamp property as follows in the <properties> block of your pom:

`<timestamp>${maven.build.timestamp}</timestamp>`

During your build, this file will be filtered with the value of project.version (which you define with <version> in your pom.xml, when you specify

 <resources>
   <resource>
     <directory>src/main/resources</directory>
     <filtering>true</filtering>
   </resource>
 </resources>

In your Java code (JSF bean, whatever), have code like the following:

    InputStream in = getClass().getClassLoader().getResourceAsStream("buildInfo.properties");
    if (in == null)
        return;

    Properties props = new Properties();
    props.load(in);

    String version = props.getProperty("build.version");
    // etc.

If your framework supports loading properties as "Resource Bundles" from the classpath (i.e. like in Spring), no need for the preceding Java code that loads the properties file.



来源:https://stackoverflow.com/questions/12426723/displaying-version-and-date-of-build-in-the-xhtml-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!