Is it possible to override nant targets from included buildfile

nant

I have a generic common.xml file that holds a number of generic nant targets that are re-used among multiple builds. What I want to do is 'override' some of these nant targets and include additional steps either before or after the existing target is executed.

Are nant targets used from the current file first? ie. If i create a nant target in the current buildfile with the same name as a target in an included file does that one get called and the included one ignored? If that's the case I can just do and call the included target but it would seem like then that would be a recursive call rather then to an included task.

Thoughts?

Best Answer

I had the same question (and found the same results), but I also found a workaround. Allow me to illustrate with an example.

You have a ProjectFile.build and a CommonFile.build. Let's say you want to overwrite a target called "Clean".

You would need to create a new file (call it CommonFile_Clean.build) which contains:

<?xml version="1.0"?>
<project>
  <target name="Clean">
        <echo message="Do clean stuff here" />
  </target>
</project>

In CommonFile.build, you conditionally include CommonFile_Clean.build:

<?xml version="1.0"?>
<project>
    <echo message="checking Clean definition..." />
    <if test="${not target::exists('Clean')}">
            <echo message="Clean target not defined." />
        <include buildfile="CommonFile_Clean.build" />      
    </if>
</project>

In ProjectFile.build, you can either define the Clean target (in which case CommonFile_Clean.build will not be used) or use the default implementation as defined in CommonFile_Clean.build.

Of course, if you have a large number of targets, this will be quite a bit of work.

Hope that helps.

Related Topic