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

后端 未结 11 2246
天命终不由人
天命终不由人 2020-12-14 16:07

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 hom

11条回答
  •  不知归路
    2020-12-14 17:06

    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:

            
                org.apache.maven.plugins
                maven-war-plugin
                2.6
                
                    
                        
                            ${project.version}
                            ${buildDateTime}
                            ${buildNumber}
                            ${buildRevision}
                        
                    
                
            
    

    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.

提交回复
热议问题