Chinaunix首页 | 论坛 | 博客
  • 博客访问: 579524
  • 博文数量: 158
  • 博客积分: 2696
  • 博客等级: 少校
  • 技术积分: 1668
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-10 00:49
个人简介

life?is?short?,?play?more!

文章分类

全部博文(158)

文章存档

2021年(1)

2013年(10)

2012年(4)

2011年(11)

2010年(27)

2009年(28)

2008年(52)

2007年(25)

我的朋友

分类: Java

2013-04-24 11:59:59

将如下内容 保存为一个 buildx.xml 文件.

选择该xml文件,  右键选择, run as , ant build...  ,  选择 all

1.  clean
2.  init
3. compile (编译src 和 test 代码)
4. test  (junit 执行,  并用jacoco 分析junit代码覆盖率 )
5. sonar   ( 静态代码检查, 分析test的报告,  并将数据上床到 sonar服务器上)  

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project name="UT coverage with Ant and JaCoCo running tests" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">

  3.     <!-- ========= Define the main properties of this project ========= -->
  4.     <property name="src.dir" value="src" />
  5.     <property name="test.dir" value="test" />
  6.     <property name="lib.junit.dir" value="lib" />
  7.     <property name="build.dir" value="build" />
  8.     <property name="target.dir" value="target" />
  9.     <property name="classes.dir" value="${build.dir}/classes" />
  10.     <property name="reports.dir" value="${build.dir}/reports" />
  11.     <property name="reports.junit.xml.dir" value="${reports.dir}/junit" />

  12.     <!-- Define the Sonar properties -->
  13.     <property name="sonar.projectKey" value="com.lenovo.lihao3.test" />
  14.     <property name="sonar.projectName" value="jacoco test project" />
  15.     <property name="sonar.projectVersion" value="1.0" />
  16.     <property name="sonar.language" value="java" />
  17.     <property name="sonar.sources" value="${src.dir}" />
  18.     <property name="sonar.tests" value="${test.dir}" />
  19.     <property name="sonar.binaries" value="${classes.dir}" />
  20.     <property name="sonar.sourceEncoding" value="UTF-8" />
  21.     <property name="sonar.surefire.reportsPath" value="${reports.junit.xml.dir}" />

  22.     <!-- The following properties are required to use JaCoCo: -->
  23.     <property name="sonar.dynamicAnalysis" value="reuseReports" />
  24.     <property name="sonar.java.coveragePlugin" value="jacoco" />
  25.     <property name="sonar.jacoco.reportPath" value="target/jacoco.exec" />

  26.     <!-- Add your basic Sonar configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
  27.     <property name="sonar.jdbc.url" value="jdbc:h2:tcp://localhost:9092/sonar" />
  28.     <property name="sonar.jdbc.username" value="sonar" />
  29.     <property name="sonar.jdbc.password" value="sonar" />

  30.     <!-- ========= Define "regular" targets: clean, compile, test, ... ========= -->
  31.     <target name="clean">
  32.         <delete dir=".sonar" />
  33.         <delete dir="${build.dir}" />
  34.         <delete dir="${reports.dir}" />
  35.         <delete dir="${target.dir}" />
  36.     </target>

  37.     <target name="init">
  38.         <mkdir dir="${build.dir}" />
  39.         <mkdir dir="${classes.dir}" />
  40.         <mkdir dir="${reports.dir}" />
  41.         <mkdir dir="${reports.junit.xml.dir}" />
  42.         <mkdir dir="${target.dir}" />
  43.     </target>

  44.     <target name="compile" depends="init">
  45.         <javac encoding="UTF-8" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="false" />
  46.         <javac encoding="UTF-8" srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="false" />
  47.     </target>


  48.     <path id="classpath">
  49.         <fileset dir="${lib.junit.dir}" includes="*.jar"/>
  50.     </path>

  51.     <target name="test" depends="compile">

  52.         <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
  53.             <classpath>
  54.                 <path refid="classpath"/>
  55.             </classpath>
  56.         </taskdef>

  57.         <!-- Import the JaCoCo Ant Task -->
  58.         <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
  59.             <!-- Update the following line, or put the "jacocoant.jar" file in your "$HOME/.ant/lib" folder -->
  60.             <classpath path="C:/Users/lihao3/.ant/lib/jacocoant.jar" />
  61.         </taskdef>

  62.         <!-- Run your unit tests, adding the JaCoCo agent -->
  63.         <jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
  64.          <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
  65.             <classpath location="${classes.dir}" />
  66.             <classpath refid="classpath" />

  67.             <formatter type="xml" />
  68.             <batchtest todir="${reports.junit.xml.dir}">
  69.              <fileset dir="${test.dir}">
  70.                 <include name="**/*test.java" />
  71.              </fileset>
  72.             </batchtest>
  73.          </junit>
  74.         </jacoco:coverage>    

  75.     </target>

  76.     <!-- ========= Define Sonar target ========= -->
  77.     <target name="sonar" depends="compile">
  78.         <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
  79.             <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
  80.             <classpath path="C:/Users/lihao3/.ant/lib/sonar-ant-task-2.0.jar" />
  81.         </taskdef>

  82.         <!-- Execute Sonar -->
  83.         <sonar:sonar />
  84.     </target>

  85.     <!-- ========= The main target "all" ========= -->
  86.     <target name="all" depends="clean,compile,test,sonar" />

  87. </project>

sonar dashboard 上显示的结果报告:

Unit tests coverage

Unit test success

 

18 ms 



阅读(6265) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~