我想显示应用程序在约盒中构建时的时间戳。这将允许我跟踪不同版本的应用程序。我怎样才能在Java中检索这些信息?在应用程序中显示构建时间戳
回答
您需要告诉您的构建过程将时间戳记放入Java属性文件中,然后您的应用程序才可以读取它。另一个很好的选择是把jar清单文件。
对于蚂蚁,您想要使用tstamp和property任务,请参阅this for an example。
当你在这里时,你可能还希望包含一个源代码控制修订版本号。
除了属性文件之外,我更愿意将版本号和编译时间作为源的一部分并将其用于报告。属性文件往往会被复制到新的安装中,因此可能会报告不正确的版本。 (通过这种方式,应用程序可以记录属性文件中的内部版本号!=编译的内部版本号。) – rsp 2010-10-23 11:12:06
明确地将其放入一个文件中。如果文件是例如只读取文件的时间戳将会中断稍后重新包装。 – 2010-10-23 11:13:58
“属性文件往往会被复制到新的安装中,因此可能会报告错误的版本。”这些文件最终应该放在jar文件中。没有人与他们混淆(如果他们这样做,那是他们的问题)。 – Thilo 2010-10-23 11:16:21
为Maven的:
在pom.xml文件,添加以下
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/target/filter.properties</filter>
</filters>
使用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>
然后设置POM文件使用默认清单文件
<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>
然后在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
资源
为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'
资源
有不需要的插件antrun一个更简单的Maven的解决方案。 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>
这将产生一行“Build-Time:yyyyMMdd-HHmm”。格式可以自定义:
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>
该模式必须符合SimpleDateFormat的格式。
也适用于jar文件。太好了! – Snekse 2012-09-12 23:47:46
非常好!谢谢 – 2016-12-05 18:11:33
我也建议阅读文件https://stackoverflow.com/a/3403426/5580181 – 2017-09-13 13:41:42
我通常遵循这种替代方法到清单,因为这是在我们的应用程序的任何地方方便。
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;
}
}
}
显然,编译时间可以很容易地为
Versioning.INSTANCE.buildTime
- 1. 如何在我的ExtJS网络应用程序中显示sencha应用程序的构建时间戳
- 2. 在Spring Boot执行程序信息端点中显示构建时间戳
- 3. 如何使用Maven在Java Web应用程序中创建构建时间戳?
- 4. Xcode在构建Mac应用程序发布时显示错误
- 5. NVD3.js在时间序列中突出显示时间戳
- 6. 在导轨应用程序中显示响应时间
- 7. 在Django模板中显示时间戳
- 8. 在Django模板中显示时间戳?
- 9. Android:消息创建时间不显示在时间戳
- 10. 构建离子应用程序时显示错误
- 11. 当我构建应用程序时,图像不显示
- 12. ActiveRecords显示ISO8601时间戳
- 13. 在iOS应用程序(日期/时间应用程序构建)中构建信息
- 14. 在应用程序中显示消息(如停机时间)的体系结构
- 15. 使用Hudson和Maven构建时间戳?
- 16. Teamcity API中的构建时间戳
- 17. 构建EST中的时间戳对象
- 18. laravel架构构建时间戳
- 19. 使用PHP构建时间显示时间过去的时间
- 20. 在iPhone中显示应用程序运行时间
- 21. 在Android应用程序中显示当前时间和日期
- 22. 在YouMax 2.0中使用时间戳创建时间戳
- 23. 在Android应用程序项目中生成时间戳
- 24. 在RoR应用程序中不更新的时间戳
- 25. 图片不会显示在钛构建Android应用程序
- 26. AdMob广告没有显示在phonegap上构建应用程序
- 27. NodeJS应用程序使用Heroku构建,但它显示应用程序错误
- 28. Android:使用Gradle构建.apk,在应用程序的About页面中使用时间戳
- 29. 使用AM或PM显示时间戳
- 30. 在maven/ant构建过程中出现错误:“[java]时间戳响应无效”
其打造您正在使用的工具访问? – 2010-10-23 10:04:17
有没有办法在NetBeans构建环境中执行此操作? – jacknad 2010-10-23 10:12:43
@JackN NB同时使用ant和maven。它取决于您正在使用的构建工具。在简单的NB项目中,它使用ANT,所以遵循给定的解决方案蚂蚁 – 2010-10-23 10:13:58