ANT – Use Reference as Property Values OR NOT

Looking at the source code of Apache Nutch, you can see it is built by Ant and except for the build.xml who defines all the targets..etc. There is another file in the project root folder called default.properties. This is the place where people declare the project variables and later will be referred to i the build.xml using a format like ${variable}. You can click here for more information about Ant properties.

Those variables won’t be included until it got called by

 <property file="${basedir}/default.properties" />

A interesting test you can do is trying to print the variable after each line of code in the build.xml. And you can see the variable won’t be valid until read in the configuration file.

ant_propertiesfile_debug

Also, as you can see from the screenshot below, whatever you declared properties won’t take effect until Ant load in the default.properties file. Or in another way, if you reference it too early, the variable has not been initialized and then will be printed as the plain text. As you can see when we tried to build the project, it failed to read in the name variable as the project name, then even if we pass the name variable in the ant command line, why it is still not working!nutch_ant_property_file_debug_print It is actually the way how Ant got implemented. When you look at the source code of Ant, for the attributes in the project tag like name and default attribute, it won’t parse and try to get the reference, but instead of passing the raw string directly.

Here is a screenshot of the setname function of Ant Project class, but you will get a pretty good idea that Ant treats property and project attribute differently.

And_Project

Guess the conclusion is we have to hard code the project attributes without using references. 🙂

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s