2

I'm trying to rewrite a legacy app in JSF and the other apps thave have been rewritten have the maven version posted in the footer.

I'm trying to figure out how their doing it and so far, here's what i have figured out that they are doing:

footer.xhtml

<h:outputText id="fullBuildString" value="#{ApplicationInfo.fullBuildString}" />

ApplicationInfoBacking.java

public class ApplicationInfoBacking {

    private String             buildTime;
    private String             iteration;
    private String             version;
    private String             inception;
    private String             fullBuildString;

    @PostConstruct
    public void init() {
        fullBuildString = generateFullBuildString();
    }

    public String getBuildTime() {
        return buildTime;
    }

    public void setBuildTime(final String buildTime) {
        this.buildTime = buildTime;
    }

    public String getIteration() {
        return iteration;
    }

    public void setIteration(final String iteration) {
        this.iteration = iteration;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(final String version) {
        this.version = version;
    }

    public String getInception() {
        return inception;
    }

    public void setInception(final String inception) {
        this.inception = inception;
    }

    /**
     * @return ApplicationName vVersion (Iteration) BuildTime
     */
    public String getFullBuildString() {
        return fullBuildString;
    }

    public String generateFullBuildString() {

        if ((version == null) || "".equals(version.trim())) {
            version = "Unknown version";
        }

        if ((iteration == null) || "".equals(iteration.trim())) {
            iteration = "Unknown iteration";
        }

        if ((buildTime == null) || "".equals(buildTime.trim())) {
            buildTime = "Unknown build time";
        }

        final StringBuilder build = new StringBuilder();
        build.append("v. ").append(version);

        if (!Phase.PRODUCTION.equals(PlatformUtil.getPhase()) && !Phase.BETA.equals(PlatformUtil.getPhase())) {
            build.append(" (").append(iteration).append(")");
            build.append(" ").append(buildTime);
        }

        return build.toString();
        }
}

faces-config.xml

<managed-bean>
        <managed-bean-name>ApplicationInfo</managed-bean-name>
        <managed-bean-class>path.to.class.ApplicationInfoBacking</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
        <managed-property>
            <property-name>buildTime</property-name>
            <value>#{initParam.buildTime}</value>
        </managed-property>
        <managed-property>
            <property-name>iteration</property-name>
            <value>#{initParam.iteration}</value>
        </managed-property>
        <managed-property>
            <property-name>version</property-name>
            <value>#{initParam.version}</value>
        </managed-property>
        <managed-property>
            <property-name>inception</property-name>
            <value>#{initParam.inception}</value>
        </managed-property>
    </managed-bean>

web.xml

<context-param>
        <param-name>buildTime</param-name>
        <param-value>${buildNumber}</param-value>
    </context-param>
    <context-param>
        <param-name>iteration</param-name>
        <param-value>${iteration}</param-value>
    </context-param>
    <context-param>
        <param-name>version</param-name>
        <param-value>${pom.version}</param-value>
    </context-param>

This is what is actually displayed when i load the app:

v. ${pom.version}

For some reason the ${pom.version} is not getting interpreted.

Does anyone know why?

Catfish
  • 18,876
  • 54
  • 209
  • 353

3 Answers3

2

It looks like they are using the buildnumber plugin: http://mojo.codehaus.org/buildnumber-maven-plugin/

You need to add that to your web-module, then enable filtering for the web.xml through the resources section in pom.xml - I think the faces-config does not need to be changed. I was not aware you can use initParam. If you cant you could still filter the faces-config directly in case your IDE does not like filtering the web.xml

the "pom.version" may not work as it is deprecated? Try using project.version

wemu
  • 7,952
  • 4
  • 30
  • 59
  • Can you explain what you mean by "enabling filtering for the web.xml through the resources section in pom.xml"? – Catfish Feb 15 '13 at 19:46
  • I noticed in the parent pom that this plugin is being used. I never knew what it did though. Thanks. – Catfish Feb 15 '13 at 22:18
  • if you want to replace the properties / placeholders you need to turn on filtering which is off by default. See http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html for some examples. Often you include .xml files for filtering but dont want images to be filtered as well (they will be damaged). So you have the same folder configured twice, once with filtering off copying the images, once with filtering on copying the xml files. you can also exclude all and then include specific files. I think the link gives good examples on how to work with filtering. – wemu Feb 15 '13 at 22:33
1

have the maven pom keys coming from different properties file

read the properties file on app startup and put it in application scoped bean

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I'd really like to mimic what currently exists if possible. I don't want to have to change the version number in another file besides my pom.xml file. – Catfish Feb 14 '13 at 21:22
  • then just parse pom.xml file or look for manifest file it should also have version it would be easy to parse just construct Properties out of it – jmj Feb 14 '13 at 21:25
  • Can you provide an example on how i can extract the version from the manifest file? – Catfish Feb 14 '13 at 21:32
  • http://stackoverflow.com/questions/615493/how-do-i-read-the-manifest-file-for-a-webapp-running-in-apache-tomcat – jmj Feb 14 '13 at 21:53
  • This is a potential solution, but i guess i'm really looking for an answer as to why `${pom.version}` is not being interpreted. – Catfish Feb 14 '13 at 22:33
1

pom.version is not interpreted because at runtime there is no such thing as a pom. The pom.xml is in memory as a Java object tree of the project setup at build time only when Maven reads the pom and creates the model. At runtime Maven is not running so pom.version has no value. Also pom.* is deprecated.. it should be project.*

In order to do what you want use the solution proposed in the other answer..

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123