title: How do you create a MANIFEST.MF that's available when you're testing and running from a jar in production?
body: <p>I've spent far too much time trying to figure this out.  This should be the simplest thing and everyone who distributes Java applications in jars must have to deal with it.</p>
  
  <p>I just want to know the proper way to add versioning to my Java app so that I can access the version information when I'm testing, e.g. debugging in Eclipse <strong>and</strong> running from a jar.</p>
  
  <p>Here's what I have in my build.xml:</p>
  
  <pre><code>&lt;target name="jar" depends = "compile"&gt;
      &lt;property name="version.num" value="1.0.0"/&gt;
      &lt;buildnumber file="build.num"/&gt;
      &lt;tstamp&gt;
          &lt;format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" /&gt;
      &lt;/tstamp&gt;
  
      &lt;manifest file="${build}/META-INF/MANIFEST.MF"&gt;
          &lt;attribute name="Built-By" value="${user.name}" /&gt;
          &lt;attribute name="Built-Date" value="${TODAY}" /&gt;                   
          &lt;attribute name="Implementation-Title" value="MyApp" /&gt;
          &lt;attribute name="Implementation-Vendor" value="MyCompany" /&gt;                
          &lt;attribute name="Implementation-Version" value="${version.num}-b${build.number}"/&gt;                              
      &lt;/manifest&gt;
  
      &lt;jar destfile="${build}/myapp.jar" basedir="${build}" excludes="*.jar" /&gt;                   
  &lt;/target&gt;
  </code></pre>
  
  <p>This creates /META-INF/MANIFEST.MF and I can read the values when I'm debugging in Eclipse thusly:</p>
  
  <pre><code>public MyClass()
  {
      try
      {                        
          InputStream stream = getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
          Manifest manifest = new Manifest(stream);            
  
          Attributes attributes = manifest.getMainAttributes();
  
          String implementationTitle = attributes.getValue("Implementation-Title");
          String implementationVersion = attributes.getValue("Implementation-Version");
          String builtDate = attributes.getValue("Built-Date");
          String builtBy = attributes.getValue("Built-By");
     }
     catch (IOException e)
     {            
          logger.error("Couldn't read manifest.");
     }        
  </code></pre>
  
  <p>}</p>
  
  <p>But, when I create the jar file, it loads the manifest of another jar (presumably the first jar loaded by the application - in my case, activation.jar).</p>
  
  <p>Also, the following code doesn't work either although all the proper values are in the manifest file.</p>
  
  <pre><code>    Package thisPackage = getClass().getPackage();
      String implementationVersion = thisPackage.getImplementationVersion();
  </code></pre>
  
  <p>Any ideas?</p>