i2ijeya
i2ijeya

Reputation: 16410

What is and does a .properties file contain?

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.

Upvotes: 14

Views: 27024

Answers (4)

Shekhar
Shekhar

Reputation:

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.

Upvotes: 5

Rob Spieldenner
Rob Spieldenner

Reputation: 1698

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.

Upvotes: 3

Nick Holt
Nick Holt

Reputation: 34301

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.

Upvotes: 3

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

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.

Upvotes: 19

Related Questions