您的位置:首页 > 编程语言

持续集成代码检查实践

2010-11-16 20:20 393 查看
近日,学习了一下CruiseControl的使用,顺便加入了一些代码检查的内容,pmd,findbugs,checkstyle等等。在网上也是找了不少材料,有的正确,有的不对,有时又参看插件包的中文档。现将代码贴在这,找到相应的jar包后就可以直接运行了,当然也将结果集成到CruiseControl的结果显示中。

目录结构

工程根目录

|

|——src

|——build

|——config

|——docs

|——lib

|——checkstyle

|——pmd

|——findbugs

|——cobertura

build目录用来做ant编译的结果目录,生成的各种报告也在其中保存。lib下放了各自的jar包,lib根目录将jsp和servlet相关的jar包放入,当然这些目录结构都可以自己再修改,当然,以下代码也不一定就行直接使用,那只是因为其中的目录关系没有调好。不过整个config目录下的内容是可以随便拷贝到任何一个工程的根目录下使用的。

当然如果有问题还可以一起讨论。

build.properties

project.dir=D:/CruiseControl/projects/Test

# The source code for the examples can be found in this directory
src.dir=${project.dir}/src

default.target.dir=${project.dir}/build

# The path to cobertura.jar
cobertura.dir=./lib
lib.dir=./lib
# Classes generated by the javac compiler are deposited in this directory
classes.dir=${default.target.dir}/classes

# Instrumented classes are deposited into this directory
instrumented.dir=${default.target.dir}/instrumented

# All reports go into this directory
reports.dir=${default.target.dir}/reports

# Unit test reports from JUnit are deposited into this directory
reports.xml.dir=${reports.dir}/junit-xml
reports.html.dir=${reports.dir}/junit-html

# Coverage reports are deposited into these directories
coverage.xml.dir=${reports.dir}/cobertura-xml
coverage.html.dir=${reports.dir}/cobertura-html

#
pmd.xml.dir=${reports.dir}/pmd-xml
pmd.html.dir=${reports.dir}/pmd-html
#
findbugs.xml.dir=${reports.dir}/findbugs-xml
findbugs.html.dir=${reports.dir}/findbugs-html
#
checkstyle.xml.dir=${reports.dir}/checkstyle-xml
checkstyle.html.dir=${reports.dir}/checkstyle-html


build.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="JFreeChart" default="all" basedir=".">
<import file="check_out.xml"/>
<import file="cobertura.xml" />
<import file="pmd.xml" />
<import file="findbugs.xml" />
<import file="checkstyle.xml" />
<import file="telnet.xml" />

<target name="clean" description="Remove all files created by the build/test process.">
<delete dir="${classes.dir}" />
<delete dir="${instrumented.dir}" />
<delete dir="${reports.dir}" />
<delete file="cobertura.log" />
<delete file="cobertura.ser" />
</target>
<!-- 更新代码-->
<target name="getCode">
<antcall target="checkout" />
</target>
<target name="all" depends="getCode,clean">
<!--单元测试及覆盖率-->
<antcall target="coverage" />
<!-- 静态检查-->
<antcall target="pmd" />
<!--查找bugs-->
<antcall target="findbugs" />
<!-- 编码规范-->
<antcall target="checkstyle" />
<!--执行远程命令-->
<!--
<antcall target="telnet"/>
-->
</target>

</project>


check_out.xml

<?xml version="1.0"?>
<project name="checkout">
<property name="build" value="build" />
<property name="src" value="src" />
<property name="cvs.package" value="Test" />
<property name="passwd" value="1qaz" />
<property name="cvs.dir" value=":pserver:Administrator:1qaz@127.0.0.1:/cvsrepo" />
<target name="init" description="create directory">
<mkdir dir="${build}" />
<mkdir dir="${cvs.package}" />
<cvspass cvsroot="${cvs.dir}" password="${passwd}" />
</target>
<target name="clean" description="clean dir" depends="init">
<delete dir="${build}" quiet="false" />
</target>
<target name="checkout" description="Update package from CVS">
<!--test-->
<cvs cvsroot="${cvs.dir}" package="${cvs.package}" dest="../../." command="checkout" cvsrsh="ssh" />
</target>
</project>


cobertura.xml

<?xml version="1.0" encoding="UTF-8"?>

<project>

<property file="build.properties" />
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="**/*.jar" />
<include name="*.jar" />
</fileset>
</path>

<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />

<target name="cobertura_init">
<mkdir dir="${classes.dir}" />
<mkdir dir="${instrumented.dir}" />
<mkdir dir="${reports.xml.dir}" />
<mkdir dir="${reports.html.dir}" />
<mkdir dir="${coverage.xml.dir}" />
<mkdir dir="${coverage.html.dir}" />
</target>

<target name="cobertura_compile" depends="cobertura_init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
<classpath refid="cobertura.classpath" />
</javac>
</target>

<target name="instrument" depends="cobertura_init,cobertura_compile">

<!--
Instrument the application classes, writing the
instrumented classes into ${build.instrumented.dir}.
-->
<cobertura-instrument todir="${instrumented.dir}">
<!--
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
-->
<ignore regex="org.apache.log4j.*" />

<fileset dir="${classes.dir}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
-->
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>

<target name="test" depends="cobertura_init,cobertura_compile">
<junit fork="yes" dir="${basedir}" failureProperty="test.failed">
<!--
Note the classpath order: instrumented classes are before the
original (uninstrumented) classes.  This is important.
-->
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />

<!--
The instrumented classes reference classes used by the
Cobertura runtime, so Cobertura and its dependencies
must be on your classpath.
-->
<classpath refid="cobertura.classpath" />

<formatter type="xml" />
<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
<batchtest todir="${reports.xml.dir}" unless="testcase">
<fileset dir="${src.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>

<junitreport todir="${reports.xml.dir}">
<fileset dir="${reports.xml.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.html.dir}" />
</junitreport>
</target>

<target name="coverage-check">
<cobertura-check branchrate="34" totallinerate="100" />
</target>

<target name="coverage-report">
<!--
Generate an XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
</target>

<target name="alternate-coverage-report">
<!--
Generate a series of HTML files containing the coverage
data in a user-readable form using nested source filesets.
-->
<cobertura-report destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</cobertura-report>
</target>

<target name="coverage" depends="cobertura_compile,instrument,test,coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports." />

</project>


pmd.xml

<project name="pmd">

<property file="build.properties" />

<path id="pmd.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="**/*.jar" />
<include name="*.jar" />
</fileset>
</path>

<target name="pmd_init">
<mkdir dir="${classes.dir}" />
<mkdir dir="${pmd.xml.dir}" />
<mkdir dir="${pmd.html.dir}" />

</target>

<target name="pmd" depends="pmd_init">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath" />
<pmd>
<ruleset>rulesets/basic.xml</ruleset>
<ruleset>rulesets/braces.xml</ruleset>
<ruleset>rulesets/javabeans.xml</ruleset>
<ruleset>rulesets/unusedcode.xml</ruleset>
<ruleset>rulesets/strings.xml</ruleset>
<ruleset>rulesets/design.xml</ruleset>
<ruleset>rulesets/coupling.xml</ruleset>
<ruleset>rulesets/codesize.xml</ruleset>
<ruleset>rulesets/imports.xml</ruleset>
<ruleset>rulesets/naming.xml</ruleset>
<formatter type="html" toFile="${pmd.html.dir}/pmd_report.html"  />
<formatter type="xml" toFile="${pmd.xml.dir}/pmd_report.xml" />

<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</pmd>
</target>

</project>


findbugs.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="findbugs">

<property file="build.properties" />

<path id="findbugs.classpath">
<fileset dir="${cobertura.dir}">
<include name="*.jar" />
<include name="findbugs/*.jar" />
</fileset>
</path>

<target name="findbugs_init">
<mkdir dir="${classes.dir}" />
<mkdir dir="${findbugs.xml.dir}" />
<mkdir dir="${findbugs.html.dir}" />
</target>

<target name="findbugs_compile" depends="findbugs_init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
<classpath refid="findbugs.classpath" />
</javac>
</target>

<target name="findbugs_jar" depends="findbugs_compile">
<jar jarfile="${default.target.dir}/JFreeChart.jar" basedir="${classes.dir}" />
</target>

<target name="findbugs" depends="findbugs_jar">
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="findbugs.classpath" />
<!--检查方式一,生成xml报告-->

<findbugs classpathref="findbugs.classpath" pluginlist="${lib.dir}/coreplugin-1.0.jar" output="xml" outputFile="${findbugs.xml.dir}/findbugs.xml">
<sourcePath path="${src.dir}" />
<class location="${default.target.dir}/JFreeChart.jar" />

</findbugs>
<xslt in="${findbugs.xml.dir}/findbugs.xml" out="${findbugs.html.dir}/findbugs.html"  style="docs/default.xsl" mce_style="docs/default.xsl" />
<!--
<findbugs home="${cobertura.dir}/findbugs" output="html" outputFile="${findbugs.html.dir}/findbugs-report.html">

<auxClasspath refid="findbugs.classpath" />

<sourcePath path="${src.dir}" />

<class location="${default.target.dir}/JFreeChart.jar" />

</findbugs>

-->
</target>

</project>


checkstyle.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="checkstyle">

<target name="checkstyle_init">
<mkdir dir="${classes.dir}" />
<mkdir dir="${checkstyle.xml.dir}" />
<mkdir dir="${checkstyle.html.dir}" />

</target>

<taskdef resource="checkstyletask.properties" classpath="${lib.dir}/checkstyle/checkstyle-all-5.0.jar" />

<target name="checkstyle" depends="checkstyle_init" description="Generates a report of code convention violations.">

<checkstyle config="docs/sun_checks.xml" failureProperty="checkstyle.failure" failOnViolation="false">
<formatter type="xml" tofile="${checkstyle.xml.dir}/checkstyle_report.xml" />
<fileset dir="${src.dir}" includes="**/*.java" />
</checkstyle>

<xslt in="${checkstyle.xml.dir}/checkstyle_report.xml" out="${checkstyle.html.dir}/checkstyle_report.html"  style="docs/checkstyle-simple.xsl" mce_style="docs/checkstyle-simple.xsl" />

</target>

</project>


telnet.xml

<project>

<property name="user" value="boboyang" />
<property name="passwd" value="198617" />
<property name="server" value="x4100.unix-center.net" />
<property name="path" value="value" />

<property name="telnet.prompt" value="-bash-3.00$" />

<target name="telnet">
<telnet server="${server}" userid="${user}" password="${passwd}">

<read>${telnet.prompt}</read>
<write>ls</write>
<read>${telnet.prompt}</read>

</telnet>
</target>
</project>


另外对于telnet和ftp功能需要一个特殊的commons-net-1.1.0.jar

好了代码就这么多,自己找下jar包,还有一些docs目录的文件,在下载的相应包中就有,主要是checkstyle使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐