ine*_*ine 4 c# post-build-event visual-studio build-events visual-studio-2017
我在 Visual Studio 2017 中有一个 C# 解决方案。我还有一个名为的批处理脚本foobar.bat,其中包含以下代码:
echo foobar : error 1: This is a test error.
Run Code Online (Sandbox Code Playgroud)
我的目标是在构建特定项目时只让上面的测试错误消息出现在 Visual Studio 的错误列表中,并在它出现时停止构建。所以我把[path to script]\foobar.bat项目的post-build事件命令行放进去,然后build。现在我在 Visual Studio 错误列表中收到两条错误消息:
The command "[path to script]\foobar.bat" exited with code -1.This is a test error.在这种情况下,查看仅打印出我的构建后事件内容的第一条错误消息并没有帮助。我想抑制这个初始错误,以便只有我的自定义错误消息显示在错误列表中(或者至少将其更改为更有用的内容)。
这是我尝试过的:
2>nul到我的批处理脚本的末尾没有任何效果。1>nul到我的批处理脚本的末尾会抑制这两个错误,这不是我想要的。&set errorlevel=0到我的批处理脚本的末尾没有任何效果。exit 0到我的批处理脚本的末尾没有任何效果。<Target
Name="PostBuildEvent"
Condition="'$(PostBuildEvent)'!=''"
DependsOnTargets="$(PostBuildEventDependsOn)">
<Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" IgnoreExitCode="true" />
</Target>
Run Code Online (Sandbox Code Playgroud)
最后一个选项几乎让我得到了我想要的。但是,尽管有错误消息,错误列表也不会弹出,构建也不会失败。似乎任何会导致初始错误消息不出现的事情也会导致构建不再失败。是这样吗?或者有什么方法可以让构建失败而不显示初始错误消息?
您需要编辑 .csproj 文件并在Target PostBuildEvent上面的最后一个要点之后添加这些任务。
此解决方案通过获取ExitCode和Output您的EXEC任务,并利用它们来触发错误的任务,然后将停止生成和记录消息。
该Exec任务需要三个参数:
IgnoreStandardErrorWarningFormat并IgnoreExitCode防止在此步骤记录错误ConsoleToMsBuild需要参数来获取输出(ConsoleToMSBuild在 VS 2017 中拼写)。所以任务看起来像这样:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="$(PostBuildEvent)" IgnoreStandardErrorWarningFormat="true" IgnoreExitCode="true" ConsoleToMsBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="OutputMessage" />
<Output TaskParameter="ExitCode" PropertyName="ExitCode" />
</Exec>
<Error Text="$(OutputMessage)" Condition="$(ExitCode) == 10" />
<!-- <Error Text="(optional text) : $(OutputMessage)" Condition="$(ExitCode) == 11" />
<Error Text="(optional text) : $(OutputMessage)" Condition="$(ExitCode) == 12" /> -->
</Target>
Run Code Online (Sandbox Code Playgroud)
并编辑文件foobar.bat:
echo foobar : error 1: This is a test error.
exit /b 10 <-- /b parameter needed if used in a .bat file
Run Code Online (Sandbox Code Playgroud)
重要的部分是exit将设置我们以后要使用的代码。
您可以有多个Error任务来处理更多条件日志记录,也可以按原样使用输出。
| 归档时间: |
|
| 查看次数: |
1537 次 |
| 最近记录: |