8

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

Community
  • 1
  • 1
velo
  • 334
  • 3
  • 8

1 Answers1

17

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.

noahlz
  • 10,202
  • 7
  • 56
  • 75
  • Wrt your last statement: there's a clear distinction between properties files for configuration (to be loaded by `java.util.Properties`) and properties files for internationalization/localization (to be loaded by `java.util.ResourceBundle`). A self-respected developer usually wouldn't mix them just for the sake of loading ease. – BalusC Sep 14 '12 at 16:18
  • 1
    Disagree. `ResourceBundle`'s primary use case is internationalization. If you are going to just display just the version number and build date, then use `java.util.Properties` in java code. The only reason I referenced Spring's `org.springframework.context.MessageSource` is because it reduces boilerplate code. (Google agrees with me. Example: http://www.mkyong.com/jsf2/jsf-2-0-and-resource-bundles-example/) – noahlz Sep 14 '12 at 16:23
  • Also, please note that the use case here is adding content to webapp output, not "configuration." – noahlz Sep 14 '12 at 16:29
  • I still find your statements confusing/conflicting. You namely didn't use `ResourceBundle#getBundle()` in your Java code example, but `Properties#load()`. Wrt the JSF tag support for `ResourceBundle`, surely I'm already aware of that :) – BalusC Sep 14 '12 at 16:51
  • Revised answer to clarify that the "just use Resource Bundle LOL" comment was referring to the final block of Java code, not the Maven configurations. – noahlz Sep 14 '12 at 17:03
  • I understand your point. I'm just pointing out that your code example and the statement wrt frameworks are conflicting each other. The frameworks doesn't use the same code approach. I just wanted to make it clear for future readers/starters. Note that I'm not implying that your whole answer is wrong, in contrary, it looks very good. I'd otherwise just have downvoted it. – BalusC Sep 14 '12 at 17:04