且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有什么办法来定义在编译时常数为Java

更新时间:2021-08-29 17:41:23

我赞成的基于标准的方法。把你的版本信息(以及其他有用的出版商的东西一起,如版本号,颠覆修订号,作者,公司的详细信息等)在罐子里的清单文件

I would favour the standards based approach. Put your version information (along with other useful publisher stuff such as build number, subversion revision number, author, company details, etc) in the jar's Manifest File.

这是有据可查的,并了解Java规范。创建清单文件(一个的强大的工具支持的.html>例如核心Ant任务,或的Maven Jar插件)。这些可以通过自动设置一些属性的帮助 - 我有Maven的配置把罐子的Maven的版本号,Subversion修订和时间戳入清单,我在制作的时候

This is a well documented and understood Java specification. Strong tool support exists for creating manifest files (a core Ant task for example, or the maven jar plugin). These can help with setting some of the attributes automatically - I have maven configured to put the jar's maven version number, Subversion revision and timestamp into the manifest for me at build time.

您可以读取与标准Java API调用运行清单的内容 - 是这样的:

You can read the contents of the manifest at runtime with standard java api calls - something like:

import java.util.jar.*;

...

JarFile myJar = new JarFile("nameOfJar.jar");    // various constructors available
Manifest manifest = myJar.getManifest();
Map<String,Attributes> manifestContents = manifest.getAttributes();

对于我来说,感觉就像一个更加Java标准的做法,所以很可能会被证明更容易让后续的code维护者可循。

To me, that feels like a more Java standard approach, so will probably prove more easy for subsequent code maintainers to follow.