从命令行运行 MSBuild 时,有什么方法可以禁用特定的 MSBuild 警告(例如 MSB3253)?我的构建脚本通过以下方式调用 msbuild.exe:
msbuild.exe MySolution.sln /t:Rebuild /p:Configuration=Release
我发现我可以使用 msbuild.exe 的另一个参数来抑制 C# 警告(例如 CS0618):
msbuild.exe MySolution.sln /t:Rebuild /p:Configuration=Release /p:NoWarn=0618
但是,此方法不适用于 MSBuild 警告。也许还有另一个神奇的属性需要设置?
我正在使用.NET 3.5 和 VS2008。
我已经成功地用
/p:WarningLevel=X
抑制了警告级别
msbuild.exe MySolution.sln /t:Rebuild /p:WarningLevel=0 /p:Configuration=Release
^^^^^^^^^^^^^^^^^
Warning
Level Meaning
-------- -------------------------------------------
0 Turns off emission of all warning messages.
1 Displays severe warning messages
2 Displays level 1 warnings plus certain, less-severe warnings, such
as warnings about hiding class members
3 Displays level 2 warnings plus certain, less-severe warnings, such
as warnings about expressions that always evaluate to true or false
4 (the default) Displays all level 3 warnings plus informational warnings
对于 MSB3253,您只需在项目文件(*.csproj)中进行设置即可导致此类警告。
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<!-- some code goes here -->
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
None
</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
<!-- some code goes here -->
</PropertyGroup>
根据 MSDN 论坛中的 this 线程,MSBuild 警告无法被抑制。
更新版本的 MSBuild 通过命令行(如 EM0 所述)或属性支持此功能:
<PropertyGroup>
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);MSB3253</MSBuildWarningsAsMessages>
</PropertyGroup>
详情请参阅此评论。
我没有找到关于此的官方文档,但是在
VerifyFileHash
任务文档中提到了。
替代方案: 将其添加到 .csproj。
<PropertyGroup>
<NoWarn>$(NoWarn);MSB3253</NoWarn>
</PropertyGroup>
对于
/p:WarningLevel=0
不起作用的 Google 用户,您可以使用 /clp:ErrorsOnly
将 msbuild 记录器配置为仅输出错误。请参阅原始堆栈溢出帖子。
在构建有许多未修复的警告的第三方库时非常有用。
在撰写本文时(2021 年),Microsoft 文档 推荐
DisabledWarnings
,这对我有用:
<PropertyGroup>
<DisabledWarnings>3568</DisabledWarnings>
</PropertyGroup>
注意“MS”前缀被省略