email notification from NANT

Setting up a NANT build script to send an email when the script completes is something I have done for several clients. When I search the web I find people are talking about it but there are not a lot of complete examples. So here is my version of how to do it. I hope it saves someone out there some time.

First these were the requirements I set out to achieve.

  • Generate a log file
  • The log file must have the date and time of the build in it's name
  • Send email notification of success or failure.
  • Attach the log file to the email

There were several NANT tasks and properties I used to accomplish this.

  • record  (You will need NAntContrib to use the record task)
    • A task that records the builds output to a file
  • mail
    • Sends an SMTP message
  • nant.onsuccess property
    • The name of a target to be executed when the build succeeds.
  • nant.onfailure property
    • The name of a target to be executed when the build fails.
  • tstamp
    • Sets properties with the current date and time.

The link below contains an example, with comments explaining what each section is doing. Keep in mind the rest of the script has been removed for clarity, therefore all you see is the entries that do what I explained above. See attachment for sample NANT Script.

As an aside you don't have to use the record task from NAntContrib you can just pipe the output from NANT to a file using the logfile command line argument. [nant.exe -l:c:\logfiles\Build.log] This output is perfectly acceptable, the problem is you can't attach it to an email and send it because it's still open and being written to. But it actually contains more information than the log file in my example. You see once you close the log files you stop writing to them. Therefore on a script failure the reason for the error does not get written to the log. I'm still looking for a way to get a complete log file that I can attach to an email and send from within a script. If you know how to do it please let me know.