How to set property and how to get it in nant
Asked Answered
W

1

6

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?

Wildee answered 6/1, 2014 at 5:13 Comment(0)
D
4

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.

Dichogamy answered 6/1, 2014 at 7:44 Comment(3)
:- From CommandPromptam calling this command := "nant -D:build.defines=FAKE_AD_AUTH" and I have written in my build script :- <property name="build.defines" value="true" overwrite="false" /> <echo message="build.defines: ${build.defines}" /> so now in My Program.cs can i access this "build.defines" vaue ?Wildee
No, you can't. You C# code knows nothing about the build script you assemble it with. You can either generate some code in your NAnt script before compilation, but that's quite fragile, IMHO. Alternatively, you can put some "label" to the resources your application works with: a record in the database, a property of the installation package, a setting to the config file, etc.Dichogamy
Thanks for the reply .. but can you pass me a working example so i can get the idea.Wildee

© 2022 - 2024 — McMap. All rights reserved.