AFAIK MSBuild没有内置支持来检索构建脚本的给定点的警告计数.但是,您可以按照以下步骤来实现此目标:
创建一个自定义记录器,用于侦听警告事件并计算警告数
创建一个公开[Output] WarningCount属性的自定义任务
自定义任务以某种方式从自定义记录器获取警告计数的值
最困难的一步是第3步.为此,有几个选项,您可以在IPC - Inter Process Comunication下自由搜索它们.下面是一个如何实现这一目标的工作示例.每个项目都是不同的类库.
共享内存
http://weblogs.asp.net/rosherove/archive/2003/05/01/6295.aspx
我已经创建了一个名为共享内存的包装器,它是一个更大的项目的一部分.它基本上允许将序列化类型和对象图存储在共享内存中并从共享内存中检索(包括您期望的跨进程).更大的项目是否完成是另一回事;-).
SampleLogger
实现跟踪警告计数的自定义记录器.
namespace SampleLogger { using System; using Microsoft.Build.Utilities; using Microsoft.Build.Framework; using DM.SharedMemory; public class MySimpleLogger : Logger { private Segment s; private int warningCount; public override void Initialize(IEventSource eventSource) { eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised); this.s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Create, 65535); this.s.SetData(this.warningCount.ToString()); } void eventSource_WarningRaised(object sender, BuildWarningEventArgs e) { this.warningCount++; this.s.SetData(this.warningCount.ToString()); } public override void Shutdown() { this.s.Dispose(); base.Shutdown(); } } }
SampleTasks
实现自定义任务,该任务读取MSbuild项目中引发的警告数.自定义任务从类库SampleLogger中实现的自定义记录器写入的共享内存中读取.
namespace SampleTasks { using System; using Microsoft.Build.Utilities; using Microsoft.Build.Framework; using DM.SharedMemory; public class BuildMetadata : Task { public int warningCount; [Output] public int WarningCount { get { Segment s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Attach, 0); int warningCount = Int32.Parse(s.GetData() as string); return warningCount; } } public override bool Execute() { return true; } } }
旋转一下.
如果您运行以下命令:
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild test.xml /logger:SampleLogger.dll
这将是输出:
Microsoft (R) Build Engine Version 2.0.50727.3053 [Microsoft .NET Framework, Version 2.0.50727.3053] Copyright (C) Microsoft Corporation 2005. All rights reserved. Build started 30-09-2008 13:04:39. __________________________________________________ Project "F:\temp\SampleLogger\bin\debug\test.xml" (default targets): Target Main: F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #1 F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #2 F:\temp\SampleLogger\bin\debug\test.xml(15,3): error : A total of 2 warning(s) were raised. Done building target "Main" in project "test.xml" -- FAILED. Done building project "test.xml" -- FAILED. Build FAILED. F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #1 F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #2 F:\temp\SampleLogger\bin\debug\test.xml(15,3): error : A total of 2 warning(s) were raised. 2 Warning(s) 1 Error(s) Time Elapsed 00:00:00.01