开发者

NAnt <exec> Task Always Returns 0 for Batch Files on Windows XP

I'm encountering an issue with the <exec> task on batch files in my NAnt project files. When running on Windows XP SP 3 (but not Windows Vista or Windows Server 2008) and using NAnt 0.85 or 0.91alpha2, the <exec> task will always succeed (returning an error code of 0) regardless of what the executed script returned.

As an example, I wrote the following NAnt target:

<target name="build">
    <exec program="fail.bat"
    failonerror="false" resultproperty="makeall.result">
</exec>
<echo message="Makeall task returned result ${makeall.result}"/>
<fail if="${int::parse(makeall.result) != 0}">Encountered ${makeall.result} errors.</fail>
</target>

which calls the following batch file:

exit /b 1

Under normal operation (Windows Vista), the result of running NAnt is:

build:

     [exec] C:\Users\Will\Code>exit /b 1 
     [exec] C:\Users\Will\Code\fail.build(6,4):
     [exec] External Program Failed: C:\Users\Will\Code\fail.bat (return code was 1)
     [echo] Makeall task returned result 1

BUILD FAILED - 1 non-fatal error(s), 0 warning(s)

But on two different Windows XP SP3 machines, the result of running NAnt is:

build:

     [exec] C:\Documents and Settings\Will\My Documents\My Code>exit /b 1 
     [echo] Makeall task returned result 0

BUILD SUCCEEDED

Although I'm not discounting the possibility that this is a bug, I find it much more likely that I'm开发者_如何学编程 forgetting some crucial configuration setting on either Windows or NAnt that is causing this behavior. Has anyone else encountered this? Is there a reasonably elegant workaround?


Nant contains 2 functions that might be useful to you:

  • environment::get-operating-system
  • operating-system::get-version

With these you might be able to write a target to generate an expected build result property. Then it should be possible to compare the expected result to the actual result returned. This way you can handle OS specific scenarios.

As for the 'root-cause' of the difference between the two results, I don't know why they would be different between versions of Windows.


This is the format that we use, it always seems to work for us. the dexbuild .bat file contains

---------2.0 ------------- "C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv" C:\YourProject\YourSoultion.sln /Rebuild release 

----------3.5-------------- "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv" C:\YourProject\YourSoultion.sln /Rebuild release 

similar logic for 4.0 

ccnet.config
       <tasks> 
              <nant> 
                <executable>C:\Nant\Nant0.86\bin\nant.exe</executable> 
                <baseDirectory>.</baseDirectory> 
                <buildFile>C:\NANT_SCRIPTS\build.xml</buildFile> 
                <targetList> 
                  <target>DexWeb</target> 
                </targetList> 
                <buildTimeoutSeconds>2000</buildTimeoutSeconds> 
              </nant>        
     </tasks> 

   build.xml file
        <target name="DexWeb"> 
            <exec program="C:\NANT_SCRIPTS\continous\dexbuild.bat" /> 
          </target>  


I was able to get this working using comments on this blog post, see comments from Scott.

at start of batch file I set SETLOCAL enabledelayedexpansion

then using following SQLCMD

FOR /R %SCRIPTDIR% %%G IN (*.sql) DO (
    sqlcmd -S%SERVER% -d %DB% -E -h-1 -w255 -i "%%G" -b -m 1 -r
    echo   %%G
    IF !ERRORLEVEL! NEQ 0 GOTO ERROR
)

and finally labels in batch file

:ERROR
SET ERRORLEVEL=!ERRORLEVEL!
GOTO ERRORExit

:ResultCode
EXIT /B %1

:ERRORExit
ENDLOCAL & CALL :ResultCode %ERRORLEVEL%

Please note return value is still not being passed back to nant but build fails OK. I have tried various combinations of resultproperty and failonerror to no success.

It works on WinXPsp3 with NANT0.90.

SC

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜