nantnant-task

FailOnError does not work in custom NAnt task


I have custom NAnt task for test purposes and I would like to cancel NAnt build process if the task is failed. I have created custom ErrorTask:

[NAnt.Core.Attributes.TaskName("errorTask")]
public class ErrorTask : NAnt.Core.Task
{
    public ErrorTask()
    {
        FailOnError = true;
    }

    protected override void ExecuteTask()
    {
        Log(NAnt.Core.Level.Error, "Error!");
    }
}

Here is what I have in NAnt build file:

<target name="errorTarget"> 
    <errorTask failonerror="true" />
    <errorTask failonerror="true" />
</target>

In the result (build.log) I have:

errorTarget:
[errorTask] Error!
[errorTask] Error!
BUILD SUCCEEDED - 2 non-fatal error(s), 0 warning(s)
Total time: 0 seconds.

So, I can see that second task is also run, but I would like to cancel it, because first call returns "Error!". Could you assist me to fix it?

Also, I assume, that it is not necessary to hardcode FailOnError value, it should be enough to use just failonerror attribute in build script, but it does not work for me in any case.

Thank you.


Solution

  • You need to let an exception bubble out of your ExecuteTask function. After your log statement, add this line:

    throw new BuildException("Something terrible has happened!");