Wednesday, October 15, 2008

nant tips and tricks - part #1

Here are some of the simple tasks and example syntax I've used to help clean up or perform certain tasks in nant

Setting a property up to have a default value or override it via environment variable.
Since nant doesn't have an else to go with if, here's a simple alternative
I used to do this by specifying the property on the command line when launching nant - but using environment variables was much cleaner when the list of variables got long because our TeamCity Build Agent can define environment variables on the fly

Deleting and recreating a directory without generating error messages
Here's a simple target that plays nice when deleting an existing directory so you can tell what happened in the logs without erroneous error messages. Note how the variable name in the directory:exists does not have the ${} because inside the expression already

Starting a process on a remote computer
This execute will launch notepad.exe on the remote computer by logging in with the supplied credentials and using WMI via wmic.exe

Psexec.exe from sysinternals (now Microsoft) is another alternative to launch programs remotely, but is known to have problems in some scripted setups. Both psexec.exe and wmic.exe worked for me in nant, but neither worked when the nant script was called by my TeamCity build agent (java) :(

4 comments:

Eugene said...

That could be an security issue, because TeamCity build agent is installed under SYSTEM account by default.

Steve said...

Which part? the fact env variables can be configured? Well, its all based on what you allow an admin to configure.. so you're trusting your admin to start with.

If you don't want the agent to have admin powers, don't install as system.

flukus said...

For default properties I just provide the default and set the overide proprty to false. Then only cruise control has to parse in the arguments.

Steve said...

@flukus - if you do that, they wouldn't be defaults, they'd be constants :) The reasoning for my example is it makes it easy to run the nant script inside and outside of the CI environment for debugging purposes or other uses. The script runs without errors or warnings. If you load properties in via .properties files you will get overwrite warnings. If you try to use the environment variables without checking them first, you will also get warnings or errors in the logs. The example method allows for clean logs and multiuse