包含美元分隔变量的 Java 属性
我将应用程序设置存储在我在 Ant 和 Java 应用程序中使用的属性文件中。也许这不是一个好的做法,但我发现它非常方便避免重复。该文件包含以下变量:
usefulstuff.dir = ${user.home}/usefulstuff
以便其他人可以在 *nix 系统上运行该程序,前提是他们的主目录中具有有用的文件夹。
现在,令人着迷的是,这个属性文件在 Ant 中工作得很好(变量被解析为 /home/username
),而当我直接在Java 应用程序,我得到一个包含 ${user.home}/usefulstuff
的字符串,这确实不是很有用。
我在 Ant 中使用以下代码加载 props:
<loadproperties srcFile="myProps.properties"/>
在 Java 应用程序中:
FileInputStream ins = new FileInputStream(propFilePath);
myProps.load(ins);
ins.close();
我遗漏了什么吗?也许有比 load()
更好的方法在 Java 应用程序中加载属性吗?
I'm storing my app settings in properties file that I use in Ant and in the Java app. Maybe it's not good pratice, but I find it very handy to avoid duplications. The file contains variables such as:
usefulstuff.dir = ${user.home}/usefulstuff
So that other people can run the program on *nix systems, provided that they have the usefulstuff folder in their home directory.
Now, the fascinating thing is that this properties file works fine in Ant (the variable gets resolved to /home/username
), while when I load the same file directly in the Java app, I get a string containing ${user.home}/usefulstuff
, which is not very useful indeed.
I load the props with this code in Ant:
<loadproperties srcFile="myProps.properties"/>
And in the Java app:
FileInputStream ins = new FileInputStream(propFilePath);
myProps.load(ins);
ins.close();
Am I missing anything? Maybe is there a better way to load properties in a Java app than load()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为这在 Ant 中起作用是特别“迷人”的 - Ant 是故意这样做的:
和
如果您想要相同的行为,则需要实现相同的逻辑。您可以直接使用 Ant 中的类,只要它们能够满足您的要求,并且您愿意提供相关的二进制文件(并遵守许可证)。
否则,您可能需要使用正则表达式来查找所有匹配项 - 或者(可能更简单)迭代所有系统属性并对它们进行简单的替换。
I don't think it's particularly "fascinating" that this works in Ant - Ant is deliberately written to do so:
and
If you want the same behaviour, you'll need to implement the same sort of logic. It's possible that you could use the classes from Ant directly, if they do what you want - and if you're happy to ship the relevant binaries (and abide by the licence).
Otherwise, you might want to use a regular expression to find all the matches - or (probably simpler) iterate over all of the system properties and do a simple replacement on them.
正如 Jon 所说,自己编写属性处理应该很简单。例如:
As Jon said, it should be straighforward to write the property handling yourself. For eg: