将如下内容 保存为一个 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服务器上)
sonar dashboard 上显示的结果报告:
选择该xml文件, 右键选择, run as , ant build... , 选择 all
1. clean
2. init
3. compile (编译src 和 test 代码)
4. test (junit 执行, 并用jacoco 分析junit代码覆盖率 )
5. sonar ( 静态代码检查, 分析test的报告, 并将数据上床到 sonar服务器上)
点击(此处)折叠或打开
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="UT coverage with Ant and JaCoCo running tests" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">
-
- <!-- ========= Define the main properties of this project ========= -->
- <property name="src.dir" value="src" />
- <property name="test.dir" value="test" />
- <property name="lib.junit.dir" value="lib" />
- <property name="build.dir" value="build" />
- <property name="target.dir" value="target" />
- <property name="classes.dir" value="${build.dir}/classes" />
- <property name="reports.dir" value="${build.dir}/reports" />
- <property name="reports.junit.xml.dir" value="${reports.dir}/junit" />
-
- <!-- Define the Sonar properties -->
- <property name="sonar.projectKey" value="com.lenovo.lihao3.test" />
- <property name="sonar.projectName" value="jacoco test project" />
- <property name="sonar.projectVersion" value="1.0" />
- <property name="sonar.language" value="java" />
- <property name="sonar.sources" value="${src.dir}" />
- <property name="sonar.tests" value="${test.dir}" />
- <property name="sonar.binaries" value="${classes.dir}" />
- <property name="sonar.sourceEncoding" value="UTF-8" />
- <property name="sonar.surefire.reportsPath" value="${reports.junit.xml.dir}" />
-
- <!-- The following properties are required to use JaCoCo: -->
- <property name="sonar.dynamicAnalysis" value="reuseReports" />
- <property name="sonar.java.coveragePlugin" value="jacoco" />
- <property name="sonar.jacoco.reportPath" value="target/jacoco.exec" />
-
- <!-- Add your basic Sonar configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
- <property name="sonar.jdbc.url" value="jdbc:h2:tcp://localhost:9092/sonar" />
- <property name="sonar.jdbc.username" value="sonar" />
- <property name="sonar.jdbc.password" value="sonar" />
-
- <!-- ========= Define "regular" targets: clean, compile, test, ... ========= -->
- <target name="clean">
- <delete dir=".sonar" />
- <delete dir="${build.dir}" />
- <delete dir="${reports.dir}" />
- <delete dir="${target.dir}" />
- </target>
-
- <target name="init">
- <mkdir dir="${build.dir}" />
- <mkdir dir="${classes.dir}" />
- <mkdir dir="${reports.dir}" />
- <mkdir dir="${reports.junit.xml.dir}" />
- <mkdir dir="${target.dir}" />
- </target>
-
- <target name="compile" depends="init">
- <javac encoding="UTF-8" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="false" />
- <javac encoding="UTF-8" srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="false" />
- </target>
-
-
- <path id="classpath">
- <fileset dir="${lib.junit.dir}" includes="*.jar"/>
- </path>
-
- <target name="test" depends="compile">
-
- <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
- <classpath>
- <path refid="classpath"/>
- </classpath>
- </taskdef>
-
- <!-- Import the JaCoCo Ant Task -->
- <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
- <!-- Update the following line, or put the "jacocoant.jar" file in your "$HOME/.ant/lib" folder -->
- <classpath path="C:/Users/lihao3/.ant/lib/jacocoant.jar" />
- </taskdef>
-
- <!-- Run your unit tests, adding the JaCoCo agent -->
- <jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
- <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
- <classpath location="${classes.dir}" />
- <classpath refid="classpath" />
-
- <formatter type="xml" />
- <batchtest todir="${reports.junit.xml.dir}">
- <fileset dir="${test.dir}">
- <include name="**/*test.java" />
- </fileset>
- </batchtest>
- </junit>
- </jacoco:coverage>
-
- </target>
-
- <!-- ========= Define Sonar target ========= -->
- <target name="sonar" depends="compile">
- <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
- <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
- <classpath path="C:/Users/lihao3/.ant/lib/sonar-ant-task-2.0.jar" />
- </taskdef>
-
- <!-- Execute Sonar -->
- <sonar:sonar />
- </target>
-
- <!-- ========= The main target "all" ========= -->
- <target name="all" depends="clean,compile,test,sonar" />
-
- </project>
sonar dashboard 上显示的结果报告:
转载:http://blog.chinaunix.net/uid-1835840-id-3616622.html