在持续集成软件开发方法中,每个新功能都是单独开发的,然后在以后集成在一起。 在执行此过程时,每个开发人员都需要彻底检查静态分析方面的代码,并在IBM Rational Team Concert™之类的系统中进行报告或跟踪。 Java编码语言具有400多种标准静态分析错误类型。 确保在检查过程中解决每个错误并指出每个缺陷可能非常繁琐。
将FindBugs,CheckStyle和单元测试覆盖率检查(Cobertura)与Rational Team Concert集成在一起可以减少项目中交付的代码的审阅过程。 一些开发人员不了解编码最佳实践。 这可能会导致性能问题,例如内存泄漏或CPU使用率下降。 这些类型的问题是通过与Rational Team Concert的工具集成自动检测到的,这会为此类问题造成缺陷。
这种集成提高了代码质量。 这对于项目及其最终用户来说是一件好事。
总体设计与说明
学到更多。 开发更多。 连接更多。
新的developerWorks Premium会员计划可通过Safari图书在线访问强大的开发工具和资源,包括500项顶级技术标题(数十种专门针对Web开发人员),获得顶级开发人员活动的折扣,最近O'Reilly的视频重播,会议等。 立即注册 。
要了解实现细节,请将Rational Team Concert作为基本系统。 将Rational Team Concert视为基本系统意味着将构建引擎,源存储库和票务系统视为一个整体。 如下面的图1和图2所示,Rational Team Concert提供了通过构建引擎组件请求对源存储库中已配置步骤进行构建的功能。 这些配置的构建步骤是我们想要添加脚本以运行质量保证工具的地方。 我们可以在顺序模式或并行模式下运行这些工具。 因为这些工具彼此独立,所以我更喜欢以并行模式运行它们。 对于Cobertura,它可以与在构建期间完成的单元测试一起使用,因此它需要与单元测试同时运行。
图1.集成框图
图2.工具集成流程图
各个工具的明智实现和集成细节
查找错误
FindBugs是由Bill Pugh和David Howemeyer创建的开源程序,用于查找Java代码中的错误。 它使用静态分析来识别Java程序中数百种不同的潜在错误类型。 潜在错误分为四个等级:“最严重”,“最严重”,“麻烦”和“关注”。 我们建议在maven build的安装阶段运行它。
如何将FindBugs与Rational Team Concert集成
在项目的构建文件中添加一个FindBugs工具条目,该条目将在项目构建期间运行FindBugs。 清单1包含构建XML部分,它将触发FindBugs并生成带有HTML报告的输出文件。
清单1.构建过程中的FindBug(逐行说明)
<tasks>
1 <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath" />
<mkdir dir="${basedir}/target" />
2 <property name="findbugs.home" value="${env.FINDBUGS_HOME}" />
3 <path id="findbugs.lib" >
<fileset dir="${findbugs.home}/lib" >
<include name="**/*.jar" />
</fileset>
</path>
4 <property name="proj.file" value="project.fbp" />
5 <property name="current.scan" value="target/scan-out.xml" />
6 <property name="temp.db" value="target/temp-db.xml" />
7 <property name="report.file" value="target/findbugs-report.html" />
<!-- scan the target code -->
8 <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
<classpath refid="findbugs.lib" />
</taskdef>
9 <findbugs home="${findbugs.home}"
projectFile="${proj.file}"
excludeFilter="excludes.xml"
includeFilter="includes.xml"
output="xml"
outputFile="${current.scan}" />
<!-- finally generate HTML report off the temporary db -->
10 <taskdef name="convertXmlToText" classname=
"edu.umd.cs.findbugs.anttask.ConvertXmlToTextTask">
<classpath refid="findbugs.lib" />
</taskdef>
11 <convertXmlToText home="${findbugs.home}"
input="${temp.db}"
output="${report.file}"
format="html:fancy-hist.xsl"
jvmargs="-Djavax.xml.transform.TransformerFactory=
org.apache.xalan.processor.TransformerFactoryImpl" />
</tasks>
让我们更仔细地看清单1。
第1行:定义任务定义资源,该资源引用maven中的类路径:maven.plugin.classpath。 第2行:定义FindBugs主页,它指向FindBugs的可执行文件和库。 第3行:包括FindBugs主目录路径中的所有.jar文件。 它们将在以后的处理中使用。 第4行:定义项目文件位置,所有与项目相关的信息都将放置在该文件位置。 下一节将提供一个示例。 第5行:将输出XML文件位置定义为scan-out.xml。 第6行:定义可用于维护错误历史记录的数据库文件。 第7行:定义报告文件的位置。 第8行:FindBugs任务定义(声明)。 第9行:执行FindBugs工具。 第10行:报告任务定义(声明)。 第11行:报告生成。 它将数据从XML转换为HTML。
项目文件( project.fbp )应该包含清单2中的信息。它列出了要扫描的所有类文件夹,用于编译这些类的相关.jar文件以及这些类的源目录。
清单2. project.fbp示例
<Project projectName="MyProject">
<Jar>../MyProject/target/classes</Jar>
<AuxClasspathEntry>../MyProject/lib/servlet-api-2.5.jar</AuxClasspathEntry>
<SrcDir>../MyProject/src</SrcDir>
</Project>
文件scan-out.xml包含在项目上运行的FindBug工具的输出。 清单3是该文件的示例。
清单3. scan-out.xml示例
<BugInstance type="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD" priority="2" abbrev="UrF"
category="STYLE">
<Class classname="com.ibm.myproject.task.Process">
<SourceLine classname="com.ibm.myproject.task.Process" start="21" end="57"
sourcefile="Process.java" sourcepath="com/ibm/myproject/task/Process.java"/>
</Class>
<Field classname="com.ibm.myproject.task.Process" name="conn" signature=
"Lcom/com/ibm/myproject/task/Connection;" isStatic="false">
<SourceLine classname="com.ibm.myproject.task.Process" sourcefile="MyHTTP.java"
sourcepath="com/ibm/myproject/task/Process.java"/>
</Field>
<SourceLine classname="com.ibm.myproject.task.Process" start="25" end="25"
startBytecode="11" endBytecode="11" sourcefile="Process.java" sourcepath=
"com/ibm/myproject/task/Process.java"/>
</BugInstance>
创建一个名为BugSet.txt的文本文件,其中包含scan-out.xml中报告的每个错误的一行。 使用XSL或脚本执行此操作。 将每个错误从BugSet文本文件转换为Rational Team Concert WorkItem JSON格式,并使用自动售票功能将其提交给Rational Team Concert,如下所述。
CheckStyle
CheckStyle是用于软件开发的静态代码分析工具,用于验证Java源代码是否符合编码规则。
如何将CheckStyle与Rational Team Concert集成
在将在项目构建期间运行CheckStyle的项目的构建文件中添加CheckStyle工具条目。 清单4是build XML部分,该部分触发CheckStyle并生成输出文件和HTML报告。
清单4.在构建过程中使用逐行说明的CheckStyle
<tasks>
1 <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref=
"maven.plugin.classpath" />
<mkdir dir="${basedir}/target" />
2 <property name="checkstyle.home" value="${env.CHECKSTYLE_HOME}" />
3 <path id="checkstyle.lib" >
<fileset dir="${checkstyle.home}" >
<include name="**/*.jar" />
</fileset>
</path>
4 <property name="config_loc" value="${env.BUILD_SCRIPTS}/common/checkstyle" />
<!-- this is for Eclipse-CS to be able to find excludes.xml -->
5 <property name="target.dir" value="${basedir}/target" />
6 <property name="scan.xml" value="${target.dir}/scan.xml" />
<taskdef resource="checkstyletask.properties" classpathref="checkstyle.lib"/>
<!-- Task: Scan -->
7 <checkstyle config="${config_loc}/myproject-checkStyle-config.xml" classpathref=
"checkstyle.lib" failOnViolation="false">
<fileset dir="${env.BUILD_HOME}/myproject/src" includes="**/*.java" />
<formatter type="xml" toFile="${scan.xml}" />
</checkstyle>
</tasks>
让我们更仔细地看清单4。
第1行:定义任务定义资源,该资源引用maven中的类路径:maven.plugin.classpath。 第2行:定义CheckStyle主页,它指向CheckStyle的可执行文件和库。 第3行:包括CheckStyle主目录路径中的所有.jar文件,这些文件将在以后的处理中使用。 第4行:定义配置位置,在该位置放置诸如excludes.xml之类的配置文件。 第5行:定义将在其中生成所有输出的目标目录。 第6行:定义输出XML文件的位置。 在此示例中,它是scan.xml。 第7行:执行位于定义的源文件夹下的实际扫描。 它仅选择Java文件。 输出将以XML格式生成。 在此示例中,它将进入scan.xml。
对于checkstyle扫描输出,它将生成详细的输出,以便可以使用XSL提取转换。 清单5中显示了一个示例XSL文件。该示例生成一个.txt文件,其中包含错误列表。
清单5.示例XSL文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1" indent="no" />
<xsl:template match="/">
<!-- we loop over all the files reported -->
<xsl:for-each select="checkstyle/file">
<xsl:variable name="fname" select="@name" />
<!-- we then loop over all the errors within the file -->
<xsl:for-each select="error">
<xsl:variable name="errtype">
<xsl:choose>
<xsl:when test=
"@source='com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck'">
Javadoc</xsl:when>
<xsl:when test=
"@source='com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck'">
Javadoc</xsl:when>
<xsl:otherwise>Other</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$errtype" /><xsl:text> problem(s) in
</xsl:text><xsl:value-of select="$fname"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
将每个缺陷从DefectSet文本文件转换为Rational Team Concert WorkItem JSON格式,并使用自动售票功能将其提交给Rational Team Concert,如下所述。
科贝图拉
Cobertura是一个开源工具,可通过代码测试来测量代码覆盖率。 它通过检测字节码来实现。 该工具基于jcoverage,可帮助程序员衡量其测试的有效性以及测试所达到的代码量。
将Cobertura与Rational Team Concert集成在一起?
在项目的构建文件中添加一个Cobertura工具条目,该条目将在项目构建期间运行Cobertura。 清单6是build XML部分,该部分触发Cobertura并使用HTML报告生成输出文件。
清单6.构建期间的Cobertura
<tasks>
1 <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref=
"maven.plugin.classpath" />
<mkdir dir="${project.build.directory}/coverage"/>
2 <taskdef name="cobertura-report" classname="net.sourceforge.cobertura.ant.ReportTask"
classpathref="maven.compile.classpath"/>
3 <cobertura-report datafile="${project.build.directory}/cobertura.ser" format="xml"
destdir="${project.build.directory}/coverage/xml">
<fileset dir="${basedir}/../myproject/app1/src">
<include name="**/*.java" />
</fileset>
</cobertura-report>
</tasks>
让我们更仔细地看清单6。
第1行:定义任务定义资源,该资源引用maven的类路径:maven.plugin.classpath。 第2行:定义Cobertura任务。 第3行:Cobertura XML报告生成器基于其中存储了检测数据的cobertura.ser文件构建。
Rational Team Concert自动售票
为文本文件中的每一行创建一个JSON,这将针对所报告的每个错误。 清单7是JSON模板。
清单7. Rational Team Concert缺陷JSON
{
"dc:title":"[Static Analysis] @BUGSUBJ@",
"dc:description":"@BUGSUBJ@<br\/><br\/>This problem was found in build:
@BUILDID@.<br\/>
<br\/>Please see the FindBugs report attached to the above build for more
details;<br\/>
1. in Rational Team Concert client, go to Team Artifacts > My Project >
Builds<br\/>
2. find the build definition: @BLDDEFN@, and double click it<br\/>
3. in the Builds view that opens, find a build with the 'Label' value matching the
build# above, and double click it<br\/>
4. in the Build Result that opens, go to the 'Downloads' tab, and double click the
'logs' in the list<br\/>
5. now your browser should come up, and you just click the
findbugs-myproject.html<br\/>"
"rtc_cm:plannedFor":
{
"_comment":"Specifies 'Development' as planned-for for the ticket",
"rdf:resource":"https://myjazzrtc.ibm.com:9000/ccm/oslc/iterations/
_gMAnkOlIEeKwsc_eM5wLCA"
},
"rtc_cm:ownedBy":
{
"_comment":"Specifies 'Developer1' (=_0k3H0MheEeKc1_BomI__Fw) as the
owner of the ticket",
"rdf:resource":"https://myjazzrtc.ibm.com:9000/ccm/oslc/users/_0k3H0MheEeKc1_BomI__Fw"
}
}
该脚本文件读取错误报告文本文件中的每一行,并调用Rational Team Concert的REST API来创建缺陷。
清单8是一个示例脚本显示方式的示例。
清单8. Rational Team Concert REST调用脚本
# put dynamic info in the payload
BUGDESC=`cat $QTXT1`
BLDDEFN=`grep ^buildDefinitionId= $BUILD_HOME/jazz.properties | awk -F= '{print $2}'`
BUILDID=`grep ^buildLabel= $BUILD_HOME/jazz.properties | awk -F= '{print $2}'`
sed -e "s%@BUGSUBJ@%$BUGDESC%g" -e "s/@BUILDID@/$BUILDID/g" -e "s/@BLDDEFN@/$BLDDEFN/g"
new-defect.json >$PAYLOAD
PLSZ=`wc $PAYLOAD | awk '{print $3}'`
if [ $PLSZ -lt 1000 ]; then
echo "...well, payload seems too small to be valid - please check.."
cat $PAYLOAD
fi
# call the REST API
curl -s -D - -k -b $COOKIES -H "Content-Type: application/x-oslc-cm-change-request+
json" -H "Accept: text/xml" -X POST --data-binary @$PAYLOAD $HOST/oslc/contexts/$PROJ/
workitems >$JAZRESP
结论
FindBugs,Cobertura,CheckStyle是用于代码质量保证的广泛使用的工具。 与Rational Team Concert的集成有助于程序员提高质量。 易于产生缺陷,避免了人工干预,并且可以防止额外的工作。 与Rational Team Concert的集成可以帮助初学者提高最大的收益,因为它可以教给他们有效的编码方法。
翻译自: https://www.ibm.com/developerworks/rational/library/integration-rational-team-concert-quality-tools/index.html
相关资源:微信小程序源码-合集6.rar