.properties 文件是什么以及包含什么?
我想知道.properties文件有什么用以及在哪里使用?
目前我正在使用 ANT 进行动态 Web 应用程序,当我在“build.xml”文件中设置属性时,必须使用 .properties 文件。
I want to know what is the use of .properties file and where it is used?
Currently I am doing dynamic web application with ANT and I have to use a .properties file when I set a property inside the "build.xml" file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.properties 文件是一个简单的键值对集合,可以通过
java.util.Properties
类。属性文件在各种 Java 应用程序中广泛用于多种用途,通常用于存储配置或本地化数据。
ANT 属性 是 ANT 特定的概念,与属性文件不同,尽管 ANT可以从这样的文件中读取属性,通常是为了有一个中心点来存储经常使用和更改的配置数据。然后,您可以在不同的 ANT 脚本中使用这样的行:
然后所有脚本都可以使用这些属性。
A .properties file is a simple collection of key-value pairs that can be parsed by the
java.util.Properties
class.Properties files are widely used for many purposes in all kinds of Java applications, often to store configuration or localization data.
ANT properties are an ANT-specific concept and not identical to properties files, though ANT properties can be read from such a file, usually in order to have a central point to store frequently used and changing configuration data. Then you can have a line like this in different ANT scripts:
And all the scripts can then use those properties.
属性用于外部化可配置的数据,如果您将该数据放入代码中,则每次想要更改属性值时都必须构建代码。
属性的主要优点是它们位于源代码之外,您可以随时更改它们。
要使用属性,请参阅
java.util。属性
类。Properties are used to externalize the data which is configurable and if you put that data in your code you have to build the code each time you want to change the value of the property.
The main advantage of properties is that they are outside your source code and you can change them anytime.
To use properties refer to
java.util.Properties
class.属性文件允许您将函数(build.xml 的作用)与其用于执行该函数的数据(属性)分开。
例如,一个项目的不同开发人员可能有不同的项目根目录。构建需要知道哪个目录是根目录,之后它执行的功能将基于该目录。因此,您可以将此信息放入属性文件中,而不是为每个开发人员提供自定义的 build.xml。
Properties files allow you to separate function (what the build.xml does) from the data it uses to perform that function (the properties).
For example, different developers on a project might have a different project root directories. The build would need to know which directory is the root, after which the functions it performs would be based on that directory. So rather than having a custom build.xml per developer, you would put this information in a properties file.
假设您的 Ant 文件包含类似
if build.properties 包含一行的内容
,那么对 prop1 的所有引用都会为您提供 valueFromPropertyFile。
最佳实践是在 build.xml 中设置所有属性,并且可以选择是否存在 build.properties 以覆盖不同环境的特定设置。这些属性也可以在命令行上重写:
命令行优先于 build.properties,后者优先于 build.xml 中的任何内容。
Web 域中的具体示例:
Given your Ant file contains something like
if build.properties contains a line
Then all references to prop1 will give you valueFromPropertyFile.
Best practices would be for all properties to be set in build.xml and have build.properties existence be optional to override specific settings for different environments. These properties can also be overridden on the command line with:
Command line takes precedence over build.properties which takes precedence over whatever is in build.xml.
Specific examples in the web domain: