如何启用“将警告视为错误"?我的TransformXml任务? [英] How can I enable "Treat Warnings as Errors" for my TransformXml task?

查看:220
本文介绍了如何启用“将警告视为错误"?我的TransformXml任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下(大大简化的)MSBuild脚本:

I'm using the following (heavily simplified) MSBuild script:

<?xml version="1.0"?>
<Project DefaultTargets="Transform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"  />

    <Target Name="Transform">       
        <TransformXml Source="../web.config"
            Transform="Config\Production\webapp.xml"
            Destination="Config\Build\Production\web.config" />
    </Target>
</Project>

这非常有用,无需通过MSDeploy即可转换单个配置文件.

This works great to transform a single configuration file without having to go through MSDeploy.

但是,让我们说我在Transform中我拼错了一个变量.我会收到警告:

However, let's say in my Transform I misspell a variable. I will receive a warning:

C:\ MyApp \ DevOps \ Config \ Production \ webapp.xml(15,6):警告:源文档中没有元素与'/configuration/appSettings/add [@ key ='MyUnknownVariable']'[C :\ MyApp \ DevOps \ ConfigBundle.msbuild]

C:\MyApp\DevOps\Config\Production\webapp.xml(15,6): warning : No element in the source document matches '/configuration/appSettings/add[@key='MyUnknownVariable']'[C:\MyApp\DevOps\ConfigBundle.msbuild]

虽然很高兴收到此警告,但我确实需要将其作为错误,因为这将成为我们自动构建和部署过程的一部分,因此在应用程序停止运行之前,几乎没人会发现它.

While it's nice that we get a warning for this, I really need this to be an error because this will become part of our automated build and deployment process so it's unlikely anyone would spot it until an application stops working.

我想要一种将这些警告视为错误的方法.基于我在互联网上看到的一些信息,我尝试了一个带有两个不同的TreatWarningsAsErrors符号的PropertyGroup:

I would like a way to treat these warnings as errors. Based off some things I've seen on the internet, I've tried a PropertyGroup with two different TreatWarningsAsErrors symbols:

<PropertyGroup>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>

我尝试在命令行上进行设置(我怀疑与上面的设置相同):

I've tried setting this on the command-line (which I suspect is the same as the above):

C:\MyApp\DevOps>msbuild /p:TreatWarningsAsErrors="true" ConfigBundle.msbuild

不幸的是,这些工作都没有;警告将继续发送.基于此,我猜想这取决于单个MSBuild任务,以尊重TreatWarningsAsErrors指令,在这种情况下,TransformXml不会.

None of these work unfortunately; the warning continues to be sent. Based off that I'm guessing that it's down to the individual MSBuild task to respect the TreatWarningsAsErrors directive and in this case, TransformXml does not.

例如,如果我可以找到一种方法来学习上一个任务是否发出警告,则可以执行以下操作:

If I could find a way to learn if the last task threw a warning for example I could do something like:

<Message Text="##teamcity[message text='Config Transform Failed!' status='ERROR']" Condition="$WarningOccurred == 'true'" />

它不是完全完美,但是肯定会阻止构建成功完成.希望有人对此有帮助的建议.

It's not exactly perfect but that would certainly stop the build from finishing successfully. Hopefully someone will have a helpful suggestion for this.

推荐答案

TreatWarningsAsErrors

TreatWarningsAsErrors

用于指示不同语言的编译器将警告视为错误

Is used to instruct the different language Compilers to treat warnings as errors

CodeAnalysisTreatWarningsAsErrors

CodeAnalysisTreatWarningsAsErrors

用于指示代码分析将警告视为错误.

Is used to instruct Code Analysis to treat warnings as errors.

没有常规开关可将所有MsBuild警告视为错误.通常,MsBuild的调用者会将记录器传递给MsBuild,以检测到已发生警告,例如Team Build所做的.然后,调用工具可以通过整个构建结果,也可以使整个构建结果失败.

There is no general switch to treat all MsBuild warnings as errors. Generally the invoker of MsBuild would pass a logger to MsBuild to detect that a warning has occurred, this is what Team Build does for example. Then the calling tool can either pass or fail the overall build result.

这篇关于如何启用“将警告视为错误"?我的TransformXml任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆