John Lam 1997 — 2026

NAnt target inheritance

The engine that controls my new publishing system is a set of NAnt scripts. My primary reason for creating my publishing system was to ensure accuracy in the technical content that I produce. I have always felt that it was unforgivable to have code that doesn’t compile and run in a technical publication. My recent experience with LaTeX and numerous errors in its published documentation only reinforces my belief that users are incredibly frustrated when they need to learn a new concept and correct careless syntax errors.

I set a very high bar for simplicity and extensibility of my build scripts. Ideally, you would create a new directory and code within that directory would be auto-magically compiled, unit tested and deployed based on a set of predefined rules. This is the simplicity angle. However, not all code fits into a standard template of “compile this stuff into a DLL assembly that references all of the standard System.* assemblies”. This is the extensibility angle.

I would gain simplicity through default targets such as Compile, which simply compiles all *.cs files in a directory into a library assembly that references the standard System.* assemblies. I would gain extensibility through custom targets such as CustomCompile, which would be supplied in a custom .build file in the directory. In effect, I would allow users to override the behavior of the default target through custom targets. But wait, how do you override a target in NAnt?

The answer is: you can’t. But you can fake it and get almost the same semantics. NAnt 0.85 adds custom functions and expressions. You can test for the existence of a target, and invoke it if it exists. In effect, you would write:

<if test=”${target::exists(‘CustomCompile’)}”>
  <call target=”CustomCompile”/>
</if>
<ifnot test=”${target::exists(‘CustomCompile)}”>
  <csc target="library" output="output.dll">
    <references>
      <include name="${nant.settings.currentframework.frameworkdirectory}/system.*.dll"/>
      <exclude name="${nant.settings.currentframework.frameworkdirectory}/system.enterpriseservices.thunk.dll"/>
    </references>
    <sources>
      <include name=”*.cs"/>
    </sources>
  </csc>
</ifnot>

If you’re lucky enough to be using MSBuild (I’m not), Alex Kipman & co. natively expose the ability to override targets.