What is a good way to handle a version number in a Java application?

前端 未结 6 829
[愿得一人]
[愿得一人] 2020-12-23 10:25

I am developing a desktop app using Java and Swing Application Framework. I have an application about box and I\'d like to have that box contain some indication of what ver

6条回答
  •  -上瘾入骨i
    2020-12-23 10:54

    I will start this post off by stating that I use Apache Maven to build. You could also do a similar sort of thing with Ant or another tool, but this is what I have done using maven.

    The best way I have found to handle this is to use the version of your project plus the subversion revision as the build number. From maven you can include the following. This will give you the subversion revision number as ${scm.revision}.

    
        
          
            maven-scm-plugin
            
              
                getting-scm.revision
                validate
                
                  update
                
              
            
          
        
      
    

    After you have then, I then use this as part of the jar file manifest as the Implementation Version.

      
        maven-jar-plugin
        2.1
        
          
            
              ${this.version}.${scm.revision}
            
          
        
      
    

    The nice thing about this is that you can access this from code by using the following:

    Package p = getClass().getPackage();
    String version = p.getImplementationVersion();
    

    That gives you the full build number like "1.0.13525" where the last number is the subversion revision. For more information on setting this up you can check out the full blog post I did a while back on this very issue.

提交回复
热议问题