<!-- ========= Sonar分析客户端 ========= -->
<target name="sonar" depends="sonar.compile">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="${sonar.lib.dir}/sonar-ant-task-2.0.jar" />
</taskdef>
<!-- 执行 Sonar -->
<sonar:sonar key="${sonar.projectKey}" version="1.0" xmlns:sonar="antlib:org.sonar.ant"/>
</target>
3) 直接运行ant build或者在命令行输入ant sonar可以分析Java工程,结束后上传数据至指定sonar网站。
四)加入单元测试统计
<!-- JaCoCo相关配置: -->
<property name="sonar.dynamicAnalysis" value="reuseReports" />
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.jacoco.reportPath" value="target/jacoco.exec" />
<!-- testng定义: -->
<taskdef name="testng" classname="com.beust.testng.TestNGAntTask">
<classpath>
<path refid="sonar.classpath"/>
</classpath>
</taskdef>
<!-- 引入 JaCoCo Ant Task -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="${sonar.lib.dir}/jacocoant.jar" />
</taskdef>
<!-- 运行单元测试, 添加 JaCoCo agent -->
<jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
<testng outputDir="${testng.report.dir}" classpathref="sonar.runpath" excludedgroups="${EX}">
<xmlfileset dir="${test.resources.dir}" includes="testng.xml"/>
<jvmarg value="-Dtest.resources.dir=${test.resources.dir}" />
</testng>
</jacoco:coverage>
</target>
<!-- ========= Sonar分析客户端 ========= -->
<target name="sonar" depends="sonar.compile">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="${sonar.lib.dir}/sonar-ant-task-2.0.jar" />
</taskdef>
<!-- 执行 Sonar -->
<sonar:sonar key="${sonar.projectKey}" version="1.0" xmlns:sonar="antlib:org.sonar.ant"/>
</target>