简体   繁体   English

eclipse java如何插入构建时间戳

[英]eclipse java How to insert a build timestamp

I am using Eclipse Java for my project. 我正在为我的项目使用Eclipse Java。 And I want to display the buildtime to the user. 我想向用户显示构建时间。 How do I get Eclipse (or a plugin) to insert the build timestamp into a properties file? 如何让Eclipse(或插件)将构建时间戳插入属性文件?

You could use either maven , or ant . 你可以使用mavenant A quick google for maven and build timestamps returned this . 一个快速的谷歌maven和构建时间戳返回

Maven is pretty good for also managing dependencies, etc, but it'll take some time to get used to it. Maven还非常适合管理依赖项等,但它需要一些时间来习惯它。 Ant is a bit easier, but just for building. Ant更容易一些,但仅限于构建。

Using Ant you can save the build timestamp to a properties file. 使用Ant可以将构建时间戳保存到属性文件中。 Eg: If you have a properties file called project.props, in Ant you can define a target which can be run each time during the build: 例如:如果你有一个名为project.props的属性文件,在Ant中你可以定义一个目标,它可以在构建期间每次运行:

<target name="buildtime">
    <tstamp>
        <format property="buildtime" pattern="yyyy MMMM dd HH:mm:ss" locale="en"/>
    </tstamp>

    <echo message="Current time and date: ${buildtime}" />
<!-- property name="buildtime" value="${buildtime}"/ -->
    <propertyfile file="project.props">
        <entry key="buildtime" type="date" value="${builltime}"  />
    </propertyfile>
</target>

After the build, the property file will be updated with the build timestamp. 构建之后,将使用构建时间戳更新属性文件。

I solved this particular problem (in Eclipse 3.8.1 debbuild) by adding a 'builder' .. [Project] --> Properties --> Builders --> New --> Program 我通过添加'构建器'解决了这个特殊问题(在Eclipse 3.8.1 debbuild中).. [Project] - > Properties - > Builders - > New - > Program

Here I added a link to a bash script in my project directory that writes a properties file to my projects resources/properties folder, with key=value pairs for major, minor, svn revision, timestamp etc. Make sure the script builder is above the Java Builder. 在这里,我添加了一个指向项目目录中的bash脚本的链接,该脚本将属性文件写入我的项目资源/属性文件夹,其中包含major,minor,svn修订版,时间戳等的键=值对。确保脚本构建器位于Java Builder。

In my project I read these properties into a version object when requested. 在我的项目中,我在请求时将这些属性读入版本对象。

This works ok, although it is bizarrely erratic unless I select ALL the options under 'Run the builder:' in the 'Build Options' tab. 这可行,但除非我在“构建选项”选项卡中选择“运行构建器:”下的所有选项,否则这是奇怪的不稳定。 Eclipse seems to store a lot of redundant state, especially about the file system. Eclipse似乎存储了很多冗余状态,尤其是关于文件系统。 With these settings, the version is regenerated a lot, but that suits me. 使用这些设置,版本会重新生成很多,但这适合我。 I have the same mechanism generating version properties for each module my project uses. 我有相同的机制为我的项目使用的每个模块生成版本属性。

The biggest antifeature is the inability to trigger this behaviour when exporting a jar file. 最大的反感觉是导出jar文件时无法触发此行为。 It would be good to have an option to trigger a clean/build before exporting. 在导出之前有一个选项来触发清理/构建会很好。

Ps: I have a parallel Ant build, independent of Eclipse, that uses the same bash script to generate the properties file. Ps:我有一个独立于Eclipse的并行Ant构建,它使用相同的bash脚本来生成属性文件。 It doesn't suit me to use Ant or Maven from inside Eclipse. 从Eclipse内部使用Ant或Maven不适合我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM