.properties 文件是什么以及包含什么?

发布于 2024-08-03 04:26:44 字数 113 浏览 5 评论 0原文

我想知道.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

方觉久 2024-08-10 04:26:44

.properties 文件是一个简单的键值对集合,可以通过 java.util.Properties 类。

属性文件在各种 Java 应用程序中广泛用于多种用途,通常用于存储配置或本地化数据。

ANT 属性 是 ANT 特定的概念,与属性文件不同,尽管 ANT可以从这样的文件中读取属性,通常是为了有一个中心点来存储经常使用和更改的配置数据。然后,您可以在不同的 ANT 脚本中使用这样的行:

<property file="foo.properties"/>

然后所有脚本都可以使用这些属性。

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:

<property file="foo.properties"/>

And all the scripts can then use those properties.

风尘浪孓 2024-08-10 04:26:44

属性用于外部化可配置的数据,如果您将该数据放入代码中,则每次想要更改属性值时都必须构建代码。
属性的主要优点是它们位于源代码之外,您可以随时更改它们。
要使用属性,请参阅 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.

鲜肉鲜肉永远不皱 2024-08-10 04:26:44

属性文件允许您将函数(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.

伤感在游骋 2024-08-10 04:26:44

假设您的 Ant 文件包含类似

<property file="build.properties"/>
<property name="prop1" value="value1"/>

if build.properties 包含一行的内容

prop1=valueFromPropertyFile

,那么对 prop1 的所有引用都会为您提供 valueFromPropertyFile。

最佳实践是在 build.xml 中设置所有属性,并且可以选择是否存在 build.properties 以覆盖不同环境的特定设置。这些属性也可以在命令行上重写:

ant -Dprop1=valueFromCommandLine

命令行优先于 build.properties,后者优先于 build.xml 中的任何内容。

Web 域中的具体示例:

  • 在生产、测试​​和开发之间部署不同的 IP 地址。
  • 设置一个标志来编译并关闭和打开调试信息
  • 设置是否部署爆炸式 war/ear 或打包版本的标志
  • 选择是否运行冒烟测试、单元测试、集成测试、验收测试或所有内容
  • 环境之间的不同密码
  • 是否创建并填充数据库或使用现有数据库
  • 来打包哪个 web.xml,具体取决于是否部署到 Tomcat、GlassFish、JBoss 等。

Given your Ant file contains something like

<property file="build.properties"/>
<property name="prop1" value="value1"/>

if build.properties contains a line

prop1=valueFromPropertyFile

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:

ant -Dprop1=valueFromCommandLine

Command line takes precedence over build.properties which takes precedence over whatever is in build.xml.

Specific examples in the web domain:

  • Different IP address to deploy to between production, test, and development.
  • Setting a flag to compile with debugging information off and on
  • Setting a flag on whether to deploy an exploded war/ear or the packaged version
  • Choosing whether to run smoke tests, unit tests, integration tests, acceptance tests or everything
  • Different passwords between environments
  • Whether to create and populate the database or use an existing one
  • Which web.xml to package depending if deploying to Tomcat, GlassFish, JBoss, etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文