`
johnnywww
  • 浏览: 20343 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

用Hudson做持续集成

阅读更多

虽然以前就知道敏捷开发,并且也参加了一些沙龙,在开发过程中也用了TDD做开发,但是一直没有做持续集成,5月10号参加了百度App开发者大会,在会上和别的朋友交流了敏捷开发相关东东,决定还是要做每日构建,于是开始查资料,做持续集成的测试。在网上找到一篇好文章 ,上面有指导文档下载(强烈推荐),经过这两天测试,在指导文档的指引下,终于调通了,我采用Ant+Hudson+Vss+Emma+PMD+FindBugs+Jabber...,下面附上我的Build.xml,主要看target为ci关联的部分。

<?xml version="1.0" encoding="UTF-8"?>
<project name="testhudsonvss" default="deploy" basedir=".">
    <property name="pmd.home" value="E:/Source/Java/component/pmd-4.2.5"/>
    <property name="javancss.home" value="E:/Source/Java/component/javancss-32.53"/>
    <property name="findbugs.home" value="E:/Source/Java/component/findbugs-1.3.9"/>
    <property name="emma.home" value="E:/Source/Java/component/emma-2.0.5312"/>
    <property name="emma.enabled" value="true"/>
    <property name="junit.home" value="E:/Source/Java/component/junit4.6"/>
    <property name="checkstyle.home" value="E:/Source/Java/component/checkstyle-5.3"/>
    <property name="dist.dir" value="dist"/>
    <property name="build.dir" value="build"/>
    <property name="src.build.dir" value="${build.dir}/classes"/>
    <property name="emma.src.build.dir" value="${build.dir}/instr"/>
    <property name="test.build.dir" value="${build.dir}/test"/>
    <property name="lib.dir" value="lib"/>
    <property name="src.dir" value="src"/>
    <property name="test.dir" value="test"/>
    <property name="test.report.dir" value="${test.dir}/report"/>
    <property name="app.name" value="TestHudsonVss"/>
    <path id="app.classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <path id="junit.classpath">
        <fileset dir="${junit.home}">
            <include name="junit-4.6.jar"/>
        </fileset>
    </path>
    <path id="test.classpath">
        <pathelement path="${src.build.dir}"/>
        <path refid="app.classpath"/>
        <path refid="junit.classpath"/>
    </path>
    <path id="findbugs.classpath">
        <fileset dir="${findbugs.home}/lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <path id="javancss.classpath">
        <fileset dir="${javancss.home}/lib"/>
    </path>
    <path id="checkstyle.classpath">
        <fileset dir="${checkstyle.home}">
            <include name="**/checkstyle-5.3-all.jar"/>
        </fileset>
    </path>
    <path id="pmd.classpath">
        <fileset dir="${pmd.home}/lib/">
            <include name="*.jar"/>
        </fileset>
    </path>
    <path id="emma.classpath">
        <fileset dir="${emma.home}/lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <path id="emma.test.classpath">
        <pathelement path="${emma.src.build.dir}"/>
        <path refid="app.classpath"/>
        <path refid="junit.classpath"/>
    </path>
    <target name="clean" description="==清理构建环境==">
        <echo message="==Clean Project=="/>
        <delete dir="${build.dir}" failonerror="off"/>
        <delete dir="${dist.dir}" failonerror="off"/>
        <delete dir="${test.report.dir}" failonerror="off"/>
        <delete dir="checkstyle" failonerror="off"/>
    </target>
    <target name="prepare" description="==准备构建==" depends="clean">
        <echo message="==Prepare Project=="/>
        <mkdir dir="${src.build.dir}"/>
        <mkdir dir="${test.build.dir}"/>
        <mkdir dir="${test.dir}"/>
        <mkdir dir="${dist.dir}"/>
        <mkdir dir="${test.report.dir}"/>
    </target>
    <target name="pmd" description="pmd-check" depends="prepare">
        <echo message="==pmd=="/>
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath"/>
        <pmd shortFilenames="true">
            <ruleset>rulesets/favorites.xml</ruleset>
            <ruleset>basic</ruleset>
            <formatter type="xml" toFile="${dist.dir}/pmd.xml"/>
            <fileset dir="${src.dir}" includes="**/*.java"/>
            <fileset dir="${test.dir}" includes="**/*.java"/>
        </pmd>
    </target>
    <target name="javancss" description="javaNCSS" depends="prepare">
        <echo message="==javancss=="/>
        <mkdir dir="${dist.dir}/javancss"/>
        <taskdef name="javancss" classname="javancss.JavancssAntTask" classpathref="javancss.classpath"/>
        <javancss srcdir="${src.dir}" includes="**/*.java" generateReport="true"
                  outputfile="${dist.dir}/javancss-report.xml" format="xml"/>
        <!--<style  in="${dist.dir}/javancss-report.xml" out="${dist.dir}/javancss/javancss_report.html" style="${javancss.home}/xslt/javancss2html.xsl" mce_style="${javancss.home}/xslt/javancss2html.xsl"/>-->
        <xslt style="${javancss.home}/xslt/javancss2html.xsl" mce_style="${javancss.home}/xslt/javancss2html.xsl" in="${dist.dir}/javancss-report.xml"
              out="${dist.dir}/javancss/javancss_report.html"/>
    </target>
    <target name="checkstyle" depends="prepare">
        <echo message="==CheckStyle=="/>
        <mkdir dir="${dist.dir}/checkstyle"/>
        <taskdef resource="checkstyletask.properties" classpathref="checkstyle.classpath"/>
        <checkstyle config="${checkstyle.home}/springside3_checks.xml" failureProperty="checkstyle.failure"
                    failOnViolation="false">
            <formatter type="xml" tofile="${dist.dir}/checkstyle_report.xml"/>
            <fileset dir="${src.dir}" includes="**/*.java"/>
            <fileset dir="${test.dir}" includes="**/*.java"/>
        </checkstyle>
        <style in="${dist.dir}/checkstyle_report.xml" out="${dist.dir}/checkstyle/checkstyle_report.html"
               style="${checkstyle.home}/contrib/checkstyle-simple.xsl" mce_style="${checkstyle.home}/contrib/checkstyle-simple.xsl"/>
    </target>
    <target name="compile" description="==编译源代码==" depends="prepare">
        <echo message="==Compile Source Code=="/>
        <javac debug="true" destdir="${src.build.dir}" deprecation="true">
            <src path="${src.dir}"/>
            <classpath refid="app.classpath"/>
        </javac>
        <echo message="==Copy Config File=="/>
        <copy todir="${src.build.dir}">
            <fileset dir="${src.dir}" includes="**/*.xml"/>
            <fileset dir="${src.dir}" includes="**/*.properties"/>
            <fileset dir="META-INF/" includes="**/*"/>
        </copy>
    </target>
    <target name="findbugs" description="==动态分析源代码中可能存在的bug==" depends="compile">
        <echo message="==FindBugs=="/>
        <mkdir dir="${dist.dir}/findbugs"/>
        <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="findbugs.classpath">
        </taskdef>
        <findbugs projectname="FindBugs-TestHudsonVss" home="${findbugs.home}" output="html"
                  outputfile="${dist.dir}/findbugs/FindBugs.html" jvmargs="-Xms128m -Xmx256m"
                  stylesheet="fancy-hist.xsl">
            <class location="${src.build.dir}"/>
        </findbugs>
    </target>
    <target name="findbugs-xml" description="==动态分析源代码中可能存在的bug==" depends="compiletest">
        <echo message="==FindBugs=="/>
        <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="findbugs.classpath">
        </taskdef>
        <findbugs projectname="FindBugs-TestHudsonVss" home="${findbugs.home}" output="xml"
                  outputfile="${dist.dir}/findbugs.xml" jvmargs="-Xms128m -Xmx256m">
            <class location="${src.build.dir}"/>
            <class location="${test.build.dir}"/>
        </findbugs>
    </target>
    <target name="compiletest" description="==编译测试代码==" depends="compile">
        <echo message="==Compile Test Code=="/>
        <javac debug="true" destdir="${test.build.dir}" deprecation="true">
            <src path="${test.dir}"/>
            <classpath refid="test.classpath"/>
        </javac>
        <echo message="==Copy Test Config File=="/>
        <copy todir="${test.build.dir}">
            <fileset dir="${test.dir}">
                <include name="**/*.xml"/>
                <include name="**/*.properties"/>
            </fileset>
        </copy>
    </target>
    <target name="test" description="==运行测试代码==" depends="compiletest">
        <echo message="==Run Test Code=="/>
        <junit printsummary="withOutAndErr" errorproperty="test.failed" failureproperty="test.failed">
            <classpath refid="test.classpath"/>
            <classpath location="${test.build.dir}"/>
            <formatter type="xml"/>
            <batchtest todir="${test.report.dir}">
                <fileset dir="${test.build.dir}" excludes="**/*BaseTest.class">
                    <include name="**/*Test.class"/>
                    <exclude name="**/AllTest.class"/>
                </fileset>
            </batchtest>
        </junit>
        <fail message="Tests failed. Check log and/or reports" if="test.failed"/>
    </target>
    <target name="genjunitreport" description="==产生JUnit报告==">
        <echo message="==Generate JUnit Report=="/>
        <junitreport todir="${test.report.dir}">
            <fileset dir="${test.report.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${test.report.dir}/html/"/>
        </junitreport>
    </target>
    <target name="testandreport" depends="test,genjunitreport" description="==运行测试代码并产生报告==">
    </target>
    <target name="instrument" description="==字节码增强==" depends="compile">
        <echo message="==Emma Code Instrument=="/>
        <taskdef resource="emma_ant.properties" classpathref="emma.classpath"/>
        <mkdir dir="${emma.src.build.dir}"/>
        <mkdir dir="${dist.dir}/emma"/>
        <emma enabled="${emma.enabled}">
            <!--<instr inst="${src.build.dir}" destdir="${emma.src.build.dir}" metadatafile="${dist.dir}/emma/metadata.emma"-->
                   <!--merge="true"/>-->
            <instr instrpath="${src.build.dir}" destdir="${emma.src.build.dir}"
                   metadatafile="${dist.dir}/emma/metadata.emma" merge="true">
            </instr>
        </emma>
        <copy todir="${build.dir}/instr" overwrite="false">
            <fileset dir="${src.dir}" includes="**/*.xml"/>
            <fileset dir="${src.dir}" includes="**/*.properties"/>
            <fileset dir="${src.build.dir}">
                <include name="**/*.class"/>
            </fileset>
        </copy>
    </target>
    <!--<target name="emmatestcompile" description="==编译所有的测试代码==" depends="instrument">-->
    <!--<echo message="==Compile Emma Test Code==">-->
    <!--</echo>-->
    <!--<javac debug="true" destdir="${build.dir}/test" deprecation="true" source="1.5" includeantruntime="on">-->
    <!--<src path="${test.dir}"/>-->
    <!--<classpath refid="emma.test.classpath"/>-->
    <!--</javac>-->
    <!--<copy todir="${build.dir}/test">-->
    <!--<fileset dir="${test.dir}">-->
    <!--<include name="**/*.xml"/>-->
    <!--<include name="**/*.properties"/>-->
    <!--</fileset>-->
    <!--</copy>-->
    <!--</target>-->
    <target name="emmatest" depends="compiletest,instrument" description="==编译Emma测试代码==">
        <echo message="==Run EMMA Test Code=="/>
        <junit printsummary="withOutAndErr" errorproperty="test.failed" failureproperty="test.failed" fork="true"
               forkmode="once">
            <jvmarg value="-Demma.coverage.out.file=${dist.dir}/emma/metadata.emma"/>
            <jvmarg value="-Demma.coverage.out.merge=true"/>
            <classpath refid="emma.test.classpath"/>
            <classpath location="${build.dir}/test"/>
            <classpath refid="emma.classpath"/>
            <formatter type="xml"/>
            <batchtest todir="${test.report.dir}">
                <fileset dir="${test.build.dir}" excludes="**/*BaseTest.class">
                    <include name="**/*Test.class"/>
                    <exclude name="**/AllTest.class"/>
                </fileset>
            </batchtest>
        </junit>
        <fail message="Tests Emma failed. Check log and/or reports" if="test.failed"/>
    </target>

    <!-- 生成测试覆盖率报告-->
    <target name="emmareport" depends="emmatest,genjunitreport">
        <echo message="==Generate JUnit Report and Emma Report=="/>
        <taskdef resource="emma_ant.properties" classpathref="emma.classpath"/>
        <emma enabled="${emma.enabled}">
            <report sourcepath="${src.dir}" sort="+block,+name,+method,+class"
                    metrics="method:70,block:80,line:80,class:100">
                <fileset dir="${dist.dir}/emma">
                    <include name="*.emma"/>
                </fileset>
                <xml outfile="${dist.dir}/coverage.xml" depth="package"/>
                <html outfile="${dist.dir}/emma/core-coverage.html" depth="method"
                      columns="name,class,method,block,line" encoding="UTF-8"/>
            </report>
        </emma>
    </target>
    <target name="package" description="==生成java Archive发布包==" depends="compile">
        <echo message="====Crate Jar===="/>
        <jar destfile="${dist.dir}/${app.name}.jar" compress="true" update="true">
            <fileset dir="${src.build.dir}">
            </fileset>
        </jar>
    </target>
    <target name="ci" description="==Continuous Integration=="
            depends="checkstyle,javancss,pmd,findbugs-xml,emmareport">
    </target>
    <target name="deploy" depends="package,testandreport">
    </target>
</project>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics