MSBuild文件运行代码分析而不重叠

时间:2013-04-08 20:26:13

标签: .net msbuild continuous-integration

我正在创建一个MSBuild文件,将我需要在构建服务器上执行的操作的一些任务合并为一步。此过程的一部分需要运行代码分析。我的问题是我已经为多个顶级项目运行代码分析,但它们对其他项目(如我们的实用程序库)具有相同的依赖性。这意味着每次代码分析都会复制Utility Library的代码分析运行。这是我的MSBuild文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Dev</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <RunCodeAnalysis>true</RunCodeAnalysis>
        <CodeAnalysisTargets>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\CodeAnalysis\Microsoft.CodeAnalysis.targets</CodeAnalysisTargets>
        <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
        <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
        <BuildInParallel>true</BuildInParallel>
        <WebProjectTargets>Clean;Build;Package</WebProjectTargets>
        <OtherProjectTargets>Clean;Build</OtherProjectTargets>
        <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
    </PropertyGroup>

    <Target Name="Build">
        <!-- Web Projects -->
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\WebProjects\WebApp1\WebApp1.csproj"
            Properties="
                Configuration=$(Configuration);
                PackageLocation=$(MSBuildProjectDirectory)\Packages\WebApp1.zip;
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(WebProjectTargets)" />
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\WebProjects\WebApp2\WebApp2.csproj"
            Properties="
                Configuration=$(Configuration);
                PackageLocation=$(MSBuildProjectDirectory)\Packages\WebApp2.zip;
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(WebProjectTargets)" />
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\WebProjects\Administrator\Administrator.csproj"
            Properties="
                Configuration=$(Configuration);
                PackageLocation=$(MSBuildProjectDirectory)\Packages\Administrator.zip;
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(WebProjectTargets)" />
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\OtherProjects\Service\Service.csproj"
            Properties="
                Configuration=$(Configuration);
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(OtherProjectTargets)" />
    </Target>
</Project>

在这种情况下,如何防止代码分析多次在同一个程序集上运行?

1 个答案:

答案 0 :(得分:1)

通常,在“打包”我的二进制分发后,我运行代码分析命令行工具。 MSBuildCodeMetrics是一个用于在构建中运行指标的“胚胎”项目。

如果某个用户提交的行数太多或者圈复杂度太多,那么这个想法就是打破构建,但是项目尚未成熟。

无论如何,如果您想通过命令行运行指标,请尝试:

http://msdn.microsoft.com/en-us/library/bb385914.aspx

当然,您需要一些魔法才能将所有程序集复制到临时目录(如最终程序包),然后根据此目录运行指标。