How can I in a jsp page get maven project version number?

Viewed 42110

I am working on a java web application, managed by maven2. From time to time, we did some changes, and want to do new releases, of course with new version number. In the homepage (jsp), there is text like

<b>version:</b> 2.3.3... 

Is it possible, every time I do a new release, I only change the <version/> in pom.xml, and version number in jsp can be automatically filled by this maven ${project.version}?

I tried maven profile, however it doesn't seem to work.

any ideas?

Thank you.

11 Answers

There are many ways of passing the values (as discussed in these comments). Another approach (which has its own pros and cons) is to add the parameter(s) to the manifest from your POM file:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Build-Version>${project.version}</Build-Version>
                        <Build-Date>${buildDateTime}</Build-Date>
                        <Build-Number>${buildNumber}</Build-Number>
                        <Build-Revision>${buildRevision}</Build-Revision>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

and then open and read the manifest to set a singleton bean during configuration or directly import them into the JSP with:

<%
    String buildVersion;
    String buildDate;
    String buildRevision;
    String buildNumber;
    Attributes attributes;
    String version = "";
    InputStream in = null;

    // Get manifest attributes
    try {
        Manifest manifest;

        in = pageContext.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
        manifest = new Manifest(in);
        attributes = manifest.getMainAttributes();
    } catch (Exception ex) {
        attributes = new Attributes();
        attributes.put(new Attributes.Name("Build-Version"), "None (Inplace Deployment)");
    } finally {
        if (in != null) {
            in.close();
        }
    }
    buildVersion = attributes.getValue("Build-Version");
    buildDate = attributes.getValue("Build-Date");
    buildRevision = attributes.getValue("Build-Revision");
    buildNumber = attributes.getValue("Build-Number");
%>

One advantage is that this information is also present in the manifest as easy to locate documentation. One disadvantage is the need to open and read the manifest file.

In http://mojo.codehaus.org/jspc/jspc-maven-plugin/usage.html

It states this:
Non-WAR Projects

You can also use this plugin with non-war projects, for instance to validate JSPs. They will be compiled, but not included in your final artifact, and no web.xml file will be generated or modified.

If you want to just validate and compile your JSPs without actually including the generated code in your war project, you can also use set the includeInProject parameter to false.

Related