Tuesday, March 28, 2006

Invoke Custom NAnt Tasks with CruiseControl.NET

Ever wondered how to hook up a custom NAnt task to continuous integration?
Turns out it’s very simple to do, below is my ‘Hello Worldified’ example.

Once the below class is compiled, place it somewhere on your build server.

using System;
using NAnt.Core;
using NAnt.Core.Attributes;

[TaskName ("MyTestTask")]
public class MyTestTask : Task
{
  private string myParameter;

  [TaskAttribute ("myParameter", Required = true)] // Mandatory
  [StringValidator (AllowEmpty = false)] // Validate
  public string MyParameter
  {
    get
    {
      return myParameter;
    }
    set
    {
      myParameter = value;
    }
  }

  protected override void ExecuteTask()
  {
    // Determine build success, show our property...
    Project.Log(Level.Error, MyParameter); // Will fail the build
    Project.Log(Level.Info, MyParameter); // Will log in NAnt Output
    Project.Log(Level.Warning, MyParameter); // Will display warning
  }
}


Make the following changes to your project’s NAnt build file.
This will force NAnt to get a local copy of the DLL, load it into memory and init its attributes…

<copy file="\\MyServer\MyLibraries\MyTestTask.dll" todir="."/>
<loadtasks assembly="MyTestTask.dll"/>
<target name="run-MyTestTask">
  <MyTestTask myParameter="Hello World" />
</target>


Finally, register the new task in your ccnet.config file.

<tasks>
  <nant>
    <targetList>
      <target>run-MyTestTask</target>
    </targetList>
  </nant>
</tasks>


All that is left now, is to change the task so it does something useful =]

Comments on "Invoke Custom NAnt Tasks with CruiseControl.NET"

 

post a comment