Maven 2: How to package current project version in a WAR file?

后端 未结 5 1661
[愿得一人]
[愿得一人] 2021-02-06 10:14

I am using Maven 2 to build my Java project and I am looking for a way to present the current version number of the pom.xml to the user (using a Servlet or JSP for example).

5条回答
  •  失恋的感觉
    2021-02-06 10:42

    I solved this problem a little differently, as I had a desire to display version, svn revision, etc. on the index page of a service. I used the buildnumber-maven-plugin and the war-plugin to store the values in the manifest.

    pom.xml snippet:

    
        org.codehaus.mojo
        buildnumber-maven-plugin
        
          
            validate
            
              create
            
          
        
      
      
        maven-war-plugin
        
          
            
              true
            
            
              ${env}
              ${buildNumber}
            
          
        
        
          
            package
            
              war
            
            
              ${env}
            
          
        
      
    

    The JSP to pull them out was fairly trivial:

    <%@ page language="java" pageEncoding="UTF-8"%>
    <% 
    java.util.jar.Manifest manifest = new java.util.jar.Manifest();
    manifest.read(pageContext.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
    java.util.jar.Attributes attributes = manifest.getMainAttributes();
    
    %>
    
      
        
        Health Check
      
      
        

    Health Check

    Version: <%=attributes.getValue("Implementation-Version")%>-<%=attributes.getValue("Implementation-Environment")%>

    SVN Revision: <%=attributes.getValue("Implementation-Build")%>

    This displayed something like:

    Version: 2.0.1-SNAPSHOT-QA
    
    SVN Revision: 932
    

提交回复
热议问题