有什么办法来定义在编译时常数为Java [英] Is there any way to define a constant value to Java at compile time

查看:125
本文介绍了有什么办法来定义在编译时常数为Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以前写的C / C ++库我钻进不必返回编译日期/时间的方法的习惯。这始终是一个编译成库,以便将区分建立图书馆。

When I used to write libraries in C/C++ I got into the habit of having a method to return the compile date/time. This was always a compiled into the library so would differentiate builds of the library. I got this by returning a #define in the code:

C ++

#ifdef _BuildDateTime_
   char* SomeClass::getBuildDateTime() {
      return _BuildDateTime_;
   }
#else
   char* SomeClass::getBuildDateTime() {
      return "Undefined";
   }
#endif

然后在编译我有一个'-D_BuildDateTime _ = 日期'的构建脚本。

有没有办法实现这个或Java中类似,而无需记住手动编辑的任何文件或分发任何单独的文件。

Is there any way to achieve this or similar in Java without needing to remember to edit any files manually or distributing any seperate files.

一个建议,我从同事得到的是让蚂蚁文件创建classpath中的文件和包放进瓶子里,并经该方法读取。

One suggestion I got from a co-worker was to get the ant file to create a file on the classpath and to package that into the JAR and have it read by the method.

喜欢的东西(假设创建被称为DateTime.dat的文件):

Something like (assuming the file created was called 'DateTime.dat'):

// I know Exceptions and proper open/closing 
// of the file are not done. This is just 
// to explain the point!
String getBuildDateTime() {
    return new BufferedReader(getClass()
            .getResourceAsStream("DateTime.dat")).readLine();
}

在我看来,这是一个黑客通过一种类似命名的文件中的 的这个JAR之外的人可以绕过/打破,但在类路径中。

To my mind that's a hack and could be circumvented/broken by someone having a similarly named file outside the JAR, but on the classpath.

不管怎样,我的问题是,是否有办法在编译时注入一个恒定的成类

Anyway, my question is whether there is any way to inject a constant into a class at compile time

修改

我考虑使用外部生成的文件中的JAR的原因一个黑客是因为这个的的)图书馆和将被嵌入到客户端应用程序。这些客户端应用程序可以定义自己的类加载器意味着我不能依赖标准JVM类加载规则。

The reason I consider using an externally generated file in the JAR a hack is because this is) a library and will be embedded in client apps. These client apps may define their own classloaders meaning I can't rely on the standard JVM class loading rules.

我个人的preference会去使用由serg10的建议从JAR文件的日期。

My personal preference would be to go with using the date from the JAR file as suggested by serg10.

感谢您的帮助!

推荐答案

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

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.

这篇关于有什么办法来定义在编译时常数为Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆