问题描述
从 TeamCity 运行 StyleCop 有没有人成功过?
Has anyone had any success with running StyleCop from TeamCity?
我知道 StyleCop 支持命令行模式,但是我不确定这将如何集成到 TeamCity 的报告输出中.
I know StyleCop supports a command line mode, however i am not sure how this will integrate into the report output by TeamCity.
我在此处查看了此插件:https://bitbucket.org/metaman/teamcitydotnetcontrib/src/753712db5df7/stylecop/
I've checked out this plugin found here: https://bitbucket.org/metaman/teamcitydotnetcontrib/src/753712db5df7/stylecop/
但是无法运行.
我使用的是 TeamCity 6.5.1(最新).
I am using TeamCity 6.5.1 (latest).
推荐答案
我不知道你对 MSBuild 有多熟悉,但是你应该可以在 TC 6 及更高版本中添加一个新的 Build Step,并将 MSBuild 设置为构建运行器,并将其指向一个 .proj 文件,该文件执行类似于以下操作:
I don't know how familiar you are with MSBuild, but you should be able to add a new Build Step in TC 6 and above, and set MSBuild as the build runner, and point it to a .proj file which does something similar to the following:
<Target Name="StyleCop">
<!-- Create a collection of files to scan -->
<CreateItem Include="$(SourceFolder)\**\*.cs">
<Output TaskParameter="Include" ItemName="StyleCopFiles" />
</CreateItem>
<StyleCopTask
ProjectFullPath="$(MSBuildProjectFile)"
SourceFiles="@(StyleCopFiles)"
ForceFullAnalysis="true"
TreatErrorsAsWarnings="true"
OutputFile="StyleCopReport.xml"
CacheResults="true" />
<Xslt Inputs="StyleCopReport.xml"
RootTag="StyleCopViolations"
Xsl="tools\StyleCop\StyleCopReport.xsl"
Output="StyleCopReport.html" />
<XmlRead XPath="count(//Violation)" XmlFileName="StyleCopReport.xml">
<Output TaskParameter="Value" PropertyName="StyleCopViolations" />
</XmlRead>
<Error Condition="$(StyleCopViolations) > 0" Text="StyleCop found $(StyleCopViolations) broken rules!" />
</Target>
如果您不想因 StyleCop 错误而导致构建失败,请将 Error
任务设置为 Warning
.
If you don't want to fail the build on a StyleCop error, then set the Error
task to be Warning
instead.
您还需要将以下内容添加到您的 .proj 文件中:
You'll also need to add the following to your .proj file:
<UsingTask TaskName="StyleCopTask" AssemblyFile="$(StyleCopTasksPath)\Microsoft.StyleCop.dll" />
Microsoft.StyleCop.dll
包含在 StyleCop 安装中,您需要适当设置路径.
Microsoft.StyleCop.dll
is included in the StyleCop installation, and you'll need to set your paths appropriately.
要在 TeamCity 中查看输出的 StyleCop 结果,您需要使用适当的 .xsl 文件(在上面的脚本中称为 StyleCopReport.xsl)将 .xml StyleCop 报告转换为 HTML.
To see the outputted StyleCop results in TeamCity, you will need to transform the .xml StyleCop report to HTML using an appropriate .xsl file (called StyleCopReport.xsl in the script above).
要在 TeamCity 中显示 HTML 文件,您需要创建一个 artifact 来自这个 .html 输出,然后 在构建结果中包含该工件.
To display the HTML file in TeamCity, you'll need to create an artifact from this .html output, and then include that artifact in the build results.
.NET 中的持续集成一书是很好的资源.
这篇关于如何在 TeamCity 中使用 StyleCop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!