How can I enable “Treat Warnings as Errors” for my TransformXml task?

那年仲夏 提交于 2019-12-10 15:39:01

问题


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>

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

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): 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.

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

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.


回答1:


TreatWarningsAsErrors

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

CodeAnalysisTreatWarningsAsErrors

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

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.



来源:https://stackoverflow.com/questions/20517870/how-can-i-enable-treat-warnings-as-errors-for-my-transformxml-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!