Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set property and how to get it in nant

Tags:

xml

nant

I have successfully built the project with NAnt.

Now if I compile the project with following command:

nant -D:build.defines=FAKE_AD_AUTH build

I want to run application and in that I come to know that I compiled the project with FAKE_AD_AUTH.

So howdo I know whether the project is built with nant -D:build.defines=FAKE_AD_AUTH build or just nant build?

like image 935
ujjaval Avatar asked Jan 06 '14 05:01

ujjaval


1 Answers

NAnt on its own is a build tool. It doesn't add any attributes to the application it builds, unless you instruct it to. And this "instruction" is totally custom and to some extent unique for each application.

When you define a property in the command-line, it ends up as a normal NAnt property in your build script (a read-only property, to be precise). Then it is up to you how to use it to "label" your application.

If your app has an installation package (MSI), it might make sense to add an MSI property to the package with some build info. Or, you might want to add some database record, or a setting in the config file, etc.

UPDATED 13.01.2014

Okay, here it an example. Let's assume your application has configuration file (XML-based) and it contains a setting called FakeBuild, which influences the behavior of the application, e.g. instead of sending real emails to real recipients it dumps a line to the log file simulating the sending moment.

The configuration file might look like this:

<configuration>
  <settings>
    ...
    <setting name="FakeBuild" value="false">
    ...
  </settings>
</configuration>

This file is a part of your source code, I mean, it lives with the source code in your VCS system. Build script contains instructions to compile the code, hence it knows the path to the configuration file as well.

Now, the build script checks its own input from the command-line, and sets the mentioned setting to true or false respectively. For instance:

<xmlpoke file="${path.to.config}" value="true" xpath="configuration/settings/setting[@name='FakeBuild']/@value" if="${property::exists('build.defines')}"/>

The line above will evaluate only if you pass the NAnt property build.defines in. Obviously, you can modify the way you pass and therefore check for properties.

Hope this sheds more light to the proposed solution.

like image 76
Yan Sklyarenko Avatar answered Sep 19 '22 06:09

Yan Sklyarenko