title: Build and Version Numbering for Java Projects (ant, cvs, hudson)
body: <p>What are current best-practices for systematic build numbering and version number management in Java projects?  Specifically:</p>
  
  <ul>
  <li><p>How to manage build numbers systematically in a distributed development environment</p></li>
  <li><p>How to maintain version numbers in source / available to the runtime application</p></li>
  <li><p>How to properly integrate with source repository</p></li>
  <li><p>How to more automatically manage version numbers vs. repository tags</p></li>
  <li><p>How to integrate with continuous build infrastructure</p></li>
  </ul>
  
  <p>There are quite a number of tools available, and ant (the build system we're using) has a task that will maintain a build number, but it's not clear how to manage this with multiple, concurrent developers using CVS, svn, or similar.</p>
  
  <p>[EDIT]</p>
  
  <p>Several good and helpful partial or specific answers have appeared below, so I'll summarize a few of them.  It sounds to me like there is not really a strong "best practice" on this, rather a collection of overlapping ideas.  Below, find my summaries and some resulting questions that folks might try to answer as follow-ups.  [New to stackoverflow... please provide comments if I'm doing this wrong.]</p>
  
  <ul>
  <li><p>If you are using SVN, versioning of a specific checkout comes along for the ride.  Build numbering can exploit this to create a unique build number that identifies the specific checkout/revision.  [CVS, which we are using for legacy reasons, doesn't provide quite this level of insight... manual intervention with tags gets you part way there.]</p></li>
  <li><p>If you are using maven as your build system, there is support for producing a version number from the SCM, as well as a release module for automatically producing releases.  [We can't use maven, for a variety of reasons, but this helps those who can.  [Thanks to <a href="https://stackoverflow.com/users/83674/marcelo-morales">marcelo-morales</a>]]</p></li>
  <li><p>If you are using <a href="http://ant.apache.org" rel="noreferrer">ant</a> as your build system, the following task description can help produce a Java .properties file capturing build information, which can then be folded into your build in a number of ways.  [We expanded on this idea to include hudson-derived information, thanks <a href="https://stackoverflow.com/users/79194/marty-lamb">marty-lamb</a>].</p></li>
  <li><p>Ant and maven (and hudson and cruise control) provide easy means for getting build numbers into a .properties file, or into a .txt/.html file.  Is this "safe" enough to keep it from being tampered with intentionally or accidentally?  Is it better to compile it into a "versioning" class at build time?</p></li>
  <li><p>Assertion: Build numbering should be defined/enacted in a continuous integration system like <a href="http://hudson-ci.org/" rel="noreferrer">hudson</a>.  [Thanks to <a href="https://stackoverflow.com/users/83674/marcelo-morales">marcelo-morales</a>]  We have taken this suggestion, but it does crack open the release engineering question:  How does a release happen?  Are there multiple buildnumbers in a release?  Is there a meaningful relationship between buildnumbers from differing releases?</p></li>
  <li><p>Question: What is the objective behind a build number?  Is it used for QA?  How?  Is it used primarily by developers to disambiguate between multiple builds during development, or more for QA to determine what build an end-user got?  If the goal is reproducibility, in theory this is what a release version number should provide -- why doesn't it?  (please answer this as a part of your answers below, it will help illuminate the choices you have made/suggested...)</p></li>
  <li><p>Question: Is there a place for build numbers in manual builds?  Is this so problematic that EVERYONE should be using a CI solution?</p></li>
  <li><p>Question: Should build numbers be checked in to the SCM?  If the goal is reliably and unambiguously identifying a particular build, how to cope with a variety of continuous or manual build systems that may crash/restart/etc... </p></li>
  <li><p>Question: Should a build number be short and sweet (i.e., monotonically increasing integer) so that it's easy to stick into file names for archival, easy to refer to in communication, etc... or should it be long and full of usernames, datestamps, machine names, etc?</p></li>
  <li><p>Question: Please provide details about how the assignment of build numbers fits into your larger automated release process.  Yes, maven lovers, we know this is done and done, but not all of us have drunk the kool-aid quite yet...</p></li>
  </ul>
  
  <p>I'd really like to flesh this out into a complete answer, at least for the concrete example of our cvs/ant/hudson setup, so someone could build a complete strategy based on this question.  I'll mark as "The Answer" anyone who can give a soup-to-nuts description for this particular case (including cvs tagging scheme, relevant CI config items, and release procedure that folds the build number into the release such that it's programmatically accessible.)  If you want to ask/answer for another particular configuration (say, svn/maven/cruise control) I'll link to the question from here. --JA</p>
  
  <p>[EDIT 23 Oct 09]
  I accepted the top-voted answer because I think it's a reasonable solution, while several of the other answers also include good ideas.  If someone wants to take a crack at synthesizing some of these with <a href="https://stackoverflow.com/users/79194/marty-lamb">marty-lamb</a>'s, I'll consider accepting a different one.  The only concern I have with marty-lamb's is that it doesn't produce a reliably serialized build number -- it depends on a local clock at the builder's system to provide unambiguous build numbers, which isn't great.</p>
  
  <p>[Edit Jul 10]</p>
  
  <p>We now include a class like the below.  This allows the version numbers to be compiled into the final executable.  Different forms of the version info are emitted in logging data, long-term archived output products, and used to trace our (sometimes years-later) analysis of output products to a specific build.</p>
  
  <pre><code>public final class AppVersion
  {
     // SVN should fill this out with the latest tag when it's checked out.
  
     private static final String APP_SVNURL_RAW = 
       "$HeadURL: svn+ssh://user@host/svnroot/app/trunk/src/AppVersion.java $";
     private static final String APP_SVN_REVISION_RAW = "$Revision: 325 $";  
  
     private static final Pattern SVNBRANCH_PAT = 
       Pattern.compile("(branches|trunk|releases)\\/([\\w\\.\\-]+)\\/.*");
     private static final String APP_SVNTAIL = 
       APP_SVNURL_RAW.replaceFirst(".*\\/svnroot\\/app\\/", "");
  
    private static final String APP_BRANCHTAG;
    private static final String APP_BRANCHTAG_NAME;
    private static final String APP_SVNREVISION = 
      APP_SVN_REVISION_RAW.replaceAll("\\$Revision:\\s*","").replaceAll("\\s*\\$", "");
  
  
    static {
      Matcher m = SVNBRANCH_PAT.matcher(APP_SVNTAIL);
      if (!m.matches()) {
        APP_BRANCHTAG = "[Broken SVN Info]";
        APP_BRANCHTAG_NAME = "[Broken SVN Info]";
      } else {
        APP_BRANCHTAG = m.group(1);
        if (APP_BRANCHTAG.equals("trunk")) {
          // this isn't necessary in this SO example, but it 
          // is since we don't call it trunk in the real case
          APP_BRANCHTAG_NAME = "trunk";
        } else {
          APP_BRANCHTAG_NAME = m.group(2);
        }
      }
    }
  
    public static String tagOrBranchName()
    { return APP_BRANCHTAG_NAME; }
  
    /** Answers a formatter String descriptor for the app version.
     * @return version string */
    public static String longStringVersion()
    { return "app "+tagOrBranchName()+" ("+
      tagOrBranchName()+", svn revision="+svnRevision()+")"; }
  
    public static String shortStringVersion()
    { return tagOrBranchName(); }
  
    public static String svnVersion()
    { return APP_SVNURL_RAW; }
  
    public static String svnRevision()
    { return APP_SVNREVISION; }
  
    public static String svnBranchId()
    { return APP_BRANCHTAG + "/" + APP_BRANCHTAG_NAME; } 
  
    public static final String banner()
    {
      StringBuilder sb = new StringBuilder();
      sb.append("\n----------------------------------------------------------------");
      sb.append("\nApplication -- ");
      sb.append(longStringVersion());
      sb.append("\n----------------------------------------------------------------\n");
      return sb.toString();
    }
  }
  </code></pre>
  
  <p>Leave comments if this deserves to become a wiki discussion.</p>