Displaying version and date of build in the xhtml

2019-03-29 13:02发布

问题:

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.