Java 在应用程序中显示构建时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4003410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying the build time-stamp in an application
提问by Artium
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?
我想在关于框中显示应用程序何时构建的时间戳。这将允许我跟踪应用程序的不同版本。如何在 Java 中检索此信息?
回答by Thilo
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.
您需要告诉您的构建过程将时间戳放入 Java 属性文件中,然后您的应用程序可以从中读取它。放置它的另一个好选择是 jar 清单文件。
For ant, you want to use the tstampand propertytasks, see this for an example.
对于 ant,您想使用tstamp和property任务,请参阅此示例。
While you are at it, you might also wish to include a source control revision number.
在此过程中,您可能还希望包含源代码控制修订号。
回答by Jigar Joshi
for Maven:
对于Maven:
In pom.xml file, add the following
在 pom.xml 文件中,添加以下内容
<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,
使用 Maven AntRun 插件生成构建时间,
<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
然后设置pom文件使用默认的manifest文件
<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
</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.
然后在 jar 文件中生成的 MANIFEST.MF 将如下所示。
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
为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
资源
回答by Christoph Leiter
There's a much simpler maven solution which doesn't require the antrun plugin. Maven has a special variable maven.build.timestamp (since Maven 2.1.0-M1).
有一个更简单的 maven 解决方案,它不需要 antrun 插件。Maven 有一个特殊的变量 maven.build.timestamp(自 Maven 2.1.0-M1 起)。
<plugin>
<artifactId>maven-war-plugin</artifactId> <!-- or maven-jar-plugin -->
<version>2.2</version>
<configuration>
<archive>
<manifestEntries>
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
</archive>
</configuration>
</plugin>
This will produce a line "Build-Time: yyyyMMdd-HHmm". The format can be customized with:
这将产生一行“构建时间:yyyyMMdd-HHmm”。格式可以自定义:
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>
The pattern has to comply with the format of SimpleDateFormat.
该模式必须符合 SimpleDateFormat 的格式。
Reference: Maven Documentation
参考:Maven 文档
回答by Andreas Klein
I normally follow this alternative approach to the MANIFEST, as this is easily accessible from anywhere in our applications.
我通常遵循这种 MANIFEST 的替代方法,因为它可以从我们应用程序的任何地方轻松访问。
package com.livngroup.sandbox;
import java.io.File;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;
public enum Versioning {
INSTANCE;
public final DateTime buildTime;
private Versioning() {
this.buildTime = this.getLastModifiedDate();
}
private DateTime getLastModifiedDate() {
try {
return new DateTime(this.getLastModifiedFile().lastModified());
} catch (Exception e) {
try {
URLConnection conn = Versioning.class.getResource(Versioning.class.getSimpleName()+".class").openConnection();
return new DateTime(conn.getLastModified());
} catch (Exception e2) {
return new DateTime(0L); //Just a fallback
}
}
}
private File getLastModifiedFile() {
try {
URL url = Versioning.class.getResource(Versioning.class.getSimpleName()+".class");
File dir = new File(url.toURI()).getParentFile().getParentFile().getParentFile().getParentFile().getParentFile();
//System.out.println("classes.dir: "+dir);
String[] extensions = null;
File lastModifiedFile = null;
for (Iterator<File> iterator = FileUtils.iterateFiles(dir, extensions, true); iterator.hasNext();) {
File file = iterator.next();
if(lastModifiedFile==null || FileUtils.isFileNewer(file, lastModifiedFile)) {
lastModifiedFile = file;
}
}
//System.out.println("lastModified: "+lastModified);
return lastModifiedFile;
} catch (Exception e) {
return null;
}
}
}
Obviously the build time can then easily be accessed as
显然,构建时间可以很容易地访问为
Versioning.INSTANCE.buildTime