如何禁止显示特定的 MSBuild 警告

当从命令行运行 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。

64221 次浏览

According to this thread in the MSDN Forum MSBuild warnings can't be suppressed.

I've managed to supress the warning level with /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

For MSB3253 you can just set in project file (*.csproj) that cause such warning.

  <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<!-- some code goes here -->
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
None
</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
<!-- some code goes here -->
</PropertyGroup>

For those Googling this now (like me): the upcoming MSBuild 15.0 (to be released with Visual Studio 2017, I presume) will finally implement the /NoWarn option to suppress specific warnings (as well as /WarnAsError to treat either specific warnings or all warnings as errors).

More recent versions of MSBuild support this via command line (as mentioned by EM0) or with properties:

<PropertyGroup>
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);MSB3253</MSBuildWarningsAsMessages>
</PropertyGroup>

For details see this comment.

I didn't find official documentation about this, but it is mentioned in the VerifyFileHash task documentation.

Alternative: Add this to .csproj.

<PropertyGroup>
<NoWarn>$(NoWarn);MSB3253</NoWarn>
</PropertyGroup>

At the time of this post (2021), Microsoft docs recommend DisabledWarnings, this worked for me:

<PropertyGroup>
<DisabledWarnings>3568</DisabledWarnings>
</PropertyGroup>

Note that the "MS" prefix is omitted