Catfish
Catfish

Reputation: 19294

JSF how to show pom.xml version in my app?

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?

Upvotes: 2

Views: 3382

Answers (3)

wemu
wemu

Reputation: 8160

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

Upvotes: 2

Manfred Moser
Manfred Moser

Reputation: 29912

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

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240900

have the maven pom keys coming from different properties file

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

Upvotes: 1

Related Questions