問題描述
在 FxCop 10 之后,Microsoft 停止為 FxCop 提供單獨(dú)的安裝程序.官方目前只能在安裝 Visual Studio 2013/2015/2017 后運(yùn)行代碼分析(FxCop 12.0/14.0/15.0).但是,我們堅(jiān)持不要在構(gòu)建代理上安裝 Visual Studio(安裝需要與我們在開發(fā)人員計(jì)算機(jī)上獲得的內(nèi)容保持同步等).
After FxCop 10 Microsoft stopped shipping a separate installer for FxCop. Officially one can currently only run code analysis (FxCop 12.0 / 14.0 / 15.0) after installing Visual Studio 2013 / 2015 / 2017. However, we are adamant about not installing Visual Studio on the build agents (the installation needs then to be kept in-sync with what we have got on the developer computers etc.).
那么我將如何讓 FxCop 12.0/14.0/15.0 在構(gòu)建代理上工作,最好不安裝任何其他東西?不過,我會接受將一些二進(jìn)制文件和 msbuild 文件添加到源代碼控制中.否則:有沒有辦法只使用 Visual Studio 2013/2015/2017 安裝程序安裝 FxCop 的東西?
So how would i go about getting FxCop 12.0 / 14.0 / 15.0 to work on a build agent, preferrably without installing anything else? I would accept adding a few binaries and msbuild files to source control, though. Otherwise: Is there a way to only install the FxCop stuff using Visual Studio 2013 / 2015 / 2017 installer?
注意:我們使用 Teamcity 作為構(gòu)建服務(wù)器.
Note: we are using Teamcity as build server.
由于特定環(huán)境和 FxCop 版本有多個有效答案,我冒昧地將它們鏈接到此處以便于訪問:
As there are multiple valid answers for specific environments and FxCop versions, I've taken the liberty of linking them here for easier access:
- FxCop 15.0 (Visual Studio 2017)
- FxCop 14.0 (Visual Studio 2015)
- FxCop 12.0 (Visual Studio 2013)
- SonarQube
推薦答案
在不安裝 Visual Studio 2017 的情況下運(yùn)行 FxCop 15.0
先決條件:
- MSBuild 15.0 --> 安裝 Microsoft Build Tools 2017,您可能還想查看此處
- Visual C++ Redistributable for Visual Studio 2017 x86或這里 或 帶有 C++ 的 Visual Studio 2017.---- 注意:總是需要 x86 版本,根據(jù)構(gòu)建,可能還需要 x64 redist.如果它丟失,則錯誤消息可能很神秘,例如
System.IO.FileNotFoundException: 無法加載文件或程序集 'Microsoft.VisualStudio.CodeAnalysis.Interop.dll' 或其依賴項(xiàng)之一.找不到指定的模塊.
).除了安裝整個 redist,您還可以單獨(dú)復(fù)制必要的 DLL,但此時我不知道需要哪些.找出確切缺失的部分非常棘手且耗時.
- MSBuild 15.0 --> Install Microsoft Build Tools 2017, you might also want to see here
- Visual C++ Redistributable for Visual Studio 2017 x86
or here OR Visual Studio 2017 with C++. ---- Note: the x86 version is always required, depending on the build x64 redist may be required as well. If it is missing the error message can be cryptic, for example
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.CodeAnalysis.Interop.dll' or one of its dependencies. The specified module could not be found.
). Instead of installing the entire redist you could also copy the necessary DLLs separately, but at this point i don't know which are needed. It's quite tricky and time consuming to find out which ones are exactly missing.
取決于您要構(gòu)建的內(nèi)容:
Depending on what you want to build:
- 適當(dāng)?shù)?Windows SDK,例如 Windows 10 SDK
- 適當(dāng)?shù)?.net SDK/目標(biāo)(.NET Framework 4.6 SDK 包含在 Windows 10 SDK 中)
這些是我必須添加到源代碼管理的文件:(請注意這可能違反某些許可協(xié)議)
These are the files that i had to add to source control: (Please consider that this might violate some license agreements)
(source control) oolsFxCop15
│
├[Engines]
│ │
│ ├IntrospectionAnalysisEngine.dll
│ └PhoenixAnalysisEngine.dll
├[Msbuild]
│ │
│ ├fxcoptask.dll
│ ├Microsoft.CodeAnalysis.Targets
│ ├Microsoft.VisualStudio.CodeAnalysis.dll
│ └Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
├[Repository]
│ │
│ ├[Compatibility]
│ │ │
│ │ ├Desktop2.0.xml
│ │ ├Desktop2.0SP1.xml
│ │ ├Desktop2.0SP2.xml
│ │ ├Desktop3.0.xml
│ │ ├Desktop3.0SP1.xml
│ │ ├Desktop3.0SP2.xml
│ │ ├Desktop3.5.xml
│ │ └Desktop3.5SP1.xml
│ └system32.bin
├[Rules]
│ │
│ ├DataflowRules.dll
│ ├DesignRules.dll
│ ├GlobalizationRules.dll
│ ├InteroperabilityRules.dll
│ ├MaintainabilityRules.dll
│ ├MobilityRules.dll
│ ├NamingRules.dll
│ ├PerformanceRules.dll
│ ├PortabilityRules.dll
│ ├ReliabilityRules.dll
│ ├SecurityRules.dll
│ ├SecurityTransparencyRules.dll
│ └UsageRules.dll
├[x64]
│ │
│ └msdia140.dll (1349 KB)
├[Xml]
│ │
│ ├CodeAnalysisReport.xsl
│ ├FxCopReport.xsl
│ └VSConsoleOutput.xsl
├Architecture-msil.dll
├CodeAnalysis.dll
├CustomDictionary.xml
├FxCopCmd.exe
├FxCopCmd.exe.config
├FxCopCommon.dll
├FxCopSdk.dll
├Microsoft.Cci.dll
├Microsoft.VisualStudio.CodeAnalysis.Common.dll
├Microsoft.VisualStudio.CodeAnalysis.DataflowModels.dll
├Microsoft.VisualStudio.CodeAnalysis.dll
├Microsoft.VisualStudio.CodeAnalysis.Interop.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.dll
├Microsoft.VisualStudio.CodeAnalysis.Phoenix.xml
├m(xù)sdia140.dll (1057 KB)
├m(xù)ssp7en.dll
├m(xù)ssp7en.lex
├phx.dll
└Runtime-vccrt-win-msil.dll
復(fù)制如下:
整個 FxCop 安裝文件夾內(nèi)容來自
entire FxCop installation folder contents from
%programfiles(x86)%Microsoft Visual Studio2017 **在此處插入版本** Team ToolsStatic Analysis ToolsFxCop
來自 Visual Studio 2017 C++ redist(或您的 Visual Studio 2017 安裝,如果您已安裝 VC++ 工作負(fù)載:%ProgramFiles(x86)%Microsoft Visual Studio2017\VCRedistMSVC14.12.25810) 或任何其他地方:(另請參閱法律信息)復(fù)制 msdia140 x86 和 x64 (我的版本是 14.12.25810.0) 到:
from Visual Studio 2017 C++ redist (or your Visual Studio 2017 Installation, if you've got VC++ workload installed: %ProgramFiles(x86)%Microsoft Visual Studio2017\VCRedistMSVC14.12.25810) or any other place: (also see legal information) copy msdia140 x86 and x64 (mine have the version 14.12.25810.0) to:
msdia140.dll (1.12 MiB)
msdia140.dll (1.12 MiB)
amd64msdia140.dll (1.42 MiB)
amd64msdia140.dll (1.42 MiB)
所有文件從 %programfiles(x86)%\Microsoft Visual Studio2017 **INSERT EDITION HERE** MSBuildMicrosoftVisualStudiov15.0CodeAnalysis
到
Msbuildfxcoptask.dll
Msbuildfxcoptask.dll
MsbuildMicrosoft.CodeAnalysis.Targets
MsbuildMicrosoft.CodeAnalysis.Targets
MsbuildMicrosoft.VisualStudio.CodeAnalysis.dll
MsbuildMicrosoft.VisualStudio.CodeAnalysis.dll
MsbuildMicrosoft.VisualStudio.CodeAnalysis.Sdk.dll
MsbuildMicrosoft.VisualStudio.CodeAnalysis.Sdk.dll
另外我將項(xiàng)目msbuild文件(*.csproj)調(diào)整如下:
Additionally i adjusted the project msbuild file (*.csproj) as follows:
<!-- Microsoft.CSharp.targets import is contained in csproj by default. This just goes to show the sequence -->
<Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets"/>
<!-- now this must be added -->
<Import Project="$(ProjectBuildScriptDir)Custom.CodeAnalysis.targets"/>
這就是我們的 Custom.CodeAnalysis.targets 包含的內(nèi)容:
And this is what our Custom.CodeAnalysis.targets contains:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Code analysis settings. -->
<PropertyGroup>
<!-- this must reference the path where you copied the FxCop stuff to -->
<FxCopDir>..FxCop15</FxCopDir>
<CodeAnalysisCulture>en-US</CodeAnalysisCulture>
<CodeAnalysisRuleSet>$(SolutionDir)FxCop.ruleset</CodeAnalysisRuleSet>
<!-- you can and should use another condition here. Otherwise code analysis will be run on every build in VS as well. -->
<!-- in my build setup i do something like Condition=IsRunningOnTeamCity => true -->
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisTreatWarningsAsErrors Condition="'$(IsRunningOnTeamCity)' != 'true'">true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
<Import Project="$(FxCopDir)MsbuildMicrosoft.CodeAnalysis.Targets" />
<Target Name="CodeAnalysisLogHeader" BeforeTargets="RunCodeAnalysis" Condition="$(RunCodeAnalysis) == 'true' OR $(RunCodeAnalysisOnce) == 'true'">
<Message Text="Text, Executing Code Analysis (FxCop) on $(MsBuildProjectName)" Importance="High" />
</Target>
</Project>
Visual Studio 路徑注意事項(xiàng):
Visual Studio 安裝路徑取決于版本.它包括專業(yè)版和企業(yè)版.AFAIR 社區(qū)版有不同的路徑,但也不支持 FxCop.歡迎指正:-)
Notes for Visual Studio Paths:
The path to the visual studio installation is edition dependent. It includes Professional and Enterprise. AFAIR the community edition has a different path, but also does not support FxCop. Corrections welcome :-)
這篇關(guān)于在構(gòu)建代理上運(yùn)行代碼分析 (FxCop > 10) 而不安裝 Visual Studio的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!