Avoiding duplicated items in Fxcop analysis using MSBuild
In my previous post, I started with a posts series that describe the tasks we've included in the Southworks SDC Tasks we recently published at Google Code.
Today, I'm going to focus in a useful and interesting task which is RemoveDuplicatedFileNames and the reason of why we had the need to create it.
So let's start.
The Problem
Imagine you have a solution where your assemblies are referenced as depicted in the picture below:
When you compile this solution you'll realize that the Contracts.dll assembly will be generated into the Services and in WebUx folders, that's right?
So far, there is no problem regarding compilation, but what happens if we define an ItemGroup in our MSBuild project that includes all our solution assemblies to be examined by FxCop by using WildCards like this?
<ItemGroup>
<Assemblies Include="$(SampleDirectory)\**\*.dll" />
</ItemGroup>
The answer is that FxCop will analyze the same assembly twice, which will generate duplicated warnings and Code Analysis errors.
Our solution approach
As I told you previously we created a simple task called RemoveDuplicatedFileNames that basically remove items from an ItemGroup on the MSBuild process, to avoid the problem described above.
So let me show you how you should configure your project file to use this task
- Reference the Southworks SDC Tasks assembly RemoveDuplicatedFileNames in your project file
<UsingTask AssemblyFile="$(ToolsPath)\Southworks.Sdc.Tasks.dll" TaskName="RemoveDuplicatedFileNames"/>
- Create your ItemGroup including your assembly files
<ItemGroup> <Assemblies Include="$(SampleDirectory)\**\*.dll" /> </ItemGroup>
- Inside the target that runs FxCop include the following lines
<RemoveDuplicatedFileNames Input="@(Assemblies)"> <Output TaskParameter="FilteredItems" ItemName="CodeAnalysisItems" /> </RemoveDuplicatedFileNames>
- Finally, instead of using the Assemblies defined in the first point, you should use the new filtered ItemGroup generated in the previous point
<FxCop Assemblies="@(CodeAnalysisItems)" OutfileName="$(CCNetArtifactDirectory)\fxcop.xml" ProjectFilePath="$(CCNetArtifactDirectory)\project.fxcop" ToolPath="$(FxCopPath)" ProjectTemplateFilePath="$(ToolsPath)\template.fxcop" />
Note: The FxCop task is not part of Southworks SDC Tasks, you can get it from the Microsoft SDC Tasks at Codeplex. There are many useful tasks for your build process!
Verification
in order to verify if your assemblies are no duplicated you should add a Message task on the same target to display the contained values on both ItemGroup, Assemblies and CodeAnalysisItems.
<Message Text="Unfiltered Assemblies" />
<Message Text="=====================" />
<Message Text="@(Assemblies)" />
<Message Text="Filtered Assemblies" />
<Message Text="===================" />
<Message Text="@(CodeAnalysisItems)" />