Killer Nant Scripting Part 7: Custom Tasks in C#

You can use NAnt with either VB or C# to write your own custom tasks. This truly is a killer feature, once you are able to implement custom tasks in C# you can make NAnt do almost anything. This allows you to address some of NAnts weaknesses, primarly it is a build and deployment scripting platform, and not a programming platform. Using a object oriented language in conjunction makes it truly robust, and ultimately usable technology.

Extending NAnt one step further you can even create your own custom NAnt assemblies (.dll), for truly flexible tasks. We will focus on doing it in the script, becuase the name of the game is getting it done quickly, without alot of overhead, it’s scripting damnit. If we wanted something else we would just fire up Visual Studio and create a application to do it for us. If you find your self doing extensive amount of programming in NAnt you may want to reconsider NAnt for the task at hand.

NAnt scripting in .NET:

First a script element needs to be created outside of a NAnt task using the needed C#.

<script language="C#" prefix="demo" ><code><![CDATA[ [Function("generateguid")]public static string getguid(  ) {

   return Guid.NewGuid().ToString; }]]></code></script>

<target name="Action.Print.Demo"><echo message="*** My Guid is: ${demo::generateguid()} ***" /></target>

Then the task is called by calling the prefix: ‘demo’ and the NAnt function name: ‘generateguid’. Many C# actions can be called this way, I usually use them for generating output, or other tasks nant is not great at.

View All of the Killer Nant Scripting Articles

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Profile Picture

About Ian Lintner


I am a software developer, mostly web,  in Des Moines, Iowa. I take a very opinionated stand concerning development, you will never regret a simple design or architecture. My education was at Drake University in Biology and Computer Science. Offline I am recently married to my wife Heather. I try my hand at many hobbies currently I am gardening till the snow comes in.



My Current Projects


Des Moines Twitter Trends