I would like to display the time-stamp of when the application was built in an about box. This will allow me tracking different versions of the application. How can I retrieve this information in Java?
+5
A:
You need to tell your build process to put the time stamp into a Java properties file, from which your application can then read it. Another good option to put it would be the jar manifest file.
For ant, you want to use the tstamp and property tasks, see this for an example.
While you are at it, you might also wish to include a source control revision number.
Thilo
2010-10-23 10:00:54
In addition to the property file, I prefer to put build number and compile time in a version class as part of the source and use that to report. Property files tend to be copied to new installs making it probable that an incorrect version is reported. (This way the application can log if the build number in the property file != the compiled-in build number.)
rsp
2010-10-23 11:12:06
Explicitly put it a file. Just reading timestamps of files will break if the file is e.g. repacked later.
Thorbjørn Ravn Andersen
2010-10-23 11:13:58
"Property files tend to be copied to new installs making it probable that an incorrect version is reported. " Those files should end up inside of the jar file. No one messes with them (and if they do, that's their problem).
Thilo
2010-10-23 11:16:21
+4
A:
for Maven:
In pom.xml file, add the following
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/target/filter.properties</filter>
</filters>
use Maven AntRun plugin to generate the build time,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir="${project.build.directory}"/>
<tstamp>
<format property="last.updated"
pattern="yyyy-MM-dd hh:mm:ss"/>
</tstamp>
<echo file="${basedir}/target/
filter.properties" message="build.time=${last.updated}"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Then set the pom file to use the default manifest file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
<!--
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>true
</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true&
lt;/addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Built-By>${user.name}</Built-By>
<Build-Jdk>${java.version}</Build-Jdk>
</manifestEntries>
</archive>
-->
</configuration>
</plugin>
Then generated MANIFEST.MF in the jar file will look like this.
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: admin
Build-Jdk: 1.5.0_14
Specification-Title: App Name
Specification-Version: 0.1 - 2008-02-21 01:03:13
Specification-Vendor: Company Name
Implementation-Title: App Name
Implementation-Version: 0.1 - 2008-02-21 01:03:13
Implementation-Vendor: Company Name
Build-Time: 2008-02-21 01:03:13
Resources
for ANT
<?xml version="1.0"?>
<project name="tstamp" basedir="." default="jar">
<property name="src" value="src"/>
<property name="obj" value="obj"/>
<property name="jar" value="tstamp"/>
<target name="clean">
<delete file="${jar}.jar"/>
<delete dir="${obj}"/>
<delete dir="${doc}"/>
</target>
<target name="compile">
<javac srcdir="${src}" destdir="${obj}" source="1.4" debug="true"
deprecation="true" />
</target>
<target name="jar" depends="compile">
<tstamp/>
<jar jarfile="${jar}-${DSTAMP}${TSTAMP}.jar" compress="true">
<fileset dir="${obj}" includes="**/*"/>
<fileset dir="${src}" includes="**/*"/>
</jar>
</target>
</project>
The above build.xml outputs a jarfile named 'tstamp-200307011540.jar'
Resource
org.life.java
2010-10-23 10:11:04