使用ant构建的java web项目如何做sonar代码质量扫描?
以下就是实际遇到并成功使用的案例
一、做sonar扫描的准备工作
    1.给web项目增加build.xml构建脚本。
    2.下载jar包:jacocoant.jar;sonar-ant-task-2.2.jar
    3.搭建一个sonar服务器
二、在build.xml中编写jacoco和sonar的脚本

  案例的build.xml脚本

<?xml version="1.0" encoding="UTF-8"?>
<project name="Simple Java Project analyzed with the Sonar Ant Task" default="all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant" xmlns:sonar="antlib:org.sonar.ant"> <!-- ========= Define the main properties of this project ========= -->
<property name="src.dir" value="src" />
<property name="build.dir" value="target" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="webRoot.dir" value="${basedir}/WebRoot"/>
<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib"/>
<property name="reports.junit.xml.dir" value="${basedir}/target/junit"/> <path id="project.classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<!--<fileset dir="${basedir}/lib">
<include name="*.jar" />
</fileset> -->
<pathelement path="${basedir}/WebRoot/WEB-INF/lib"/>
</path> <property name="sonar.host.url" value="http://172.31.65.167:9000/" /> <!-- Define the Sonar properties -->
<property name="sonar.projectKey" value="com.ceair.ma:ma" />
<property name="sonar.projectName" value="ma" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="src" />
<property name="sonar.binaries" value="target/classes" /> <property name="sonar.working.directory" value="target/sonar" />
<!--property name="sonar.surefire.reportsPath" value="sonar/build/surefire-reports/findbugs-result.xml" /-->
<property name="sonar.sourceEncoding" value="UTF-8" /> <!-- sonar使用jacoco配置: -->
<property name="sonar.dynamicAnalysis" value="reuseReports" />
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.jacoco.reportPath" value="jacoco.exec" /> <!-- Add your basic Sonar configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
<property name="sonar.jdbc.url" value="jdbc:mysql://172.31.65.167:3306/test?useUnicode=true&amp;characterEncoding=utf8" />
<property name="sonar.jdbc.username" value="test" />
<property name="sonar.jdbc.password" value="000000" />
<property name="sonar.junit.reportsPath" value="target/sonar" /> <!-- 控制台打印sonar分析的详细信息-->
<!--<property name="sonar.verbose" value="true" /> --> <!-- ========= Define "regular" targets: clean, compile, ... ========= -->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="init">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<mkdir dir="${reports.junit.xml.dir}" />
</target> <target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.6" target="1.6" debug="on"
deprecation="false" optimize="false" failonerror="true" >
<compilerarg line="-encoding UTF-8"/>
<classpath refid="project.classpath"></classpath>
</javac>
</target> <target name="test" depends="compile"> <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath>
<path refid="project.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="${basedir}/third/jacocoant.jar" />
</taskdef> <!-- Run your unit tests, adding the JaCoCo agent -->
<jacoco:coverage>
<junit fork="true" forkmode="once" printsummary="on" showoutput="true">
<!--<classpath location="${classes.dir}" />
<classpath refid="project.classpath" />--> <formatter type="xml" usefile="true"/>
<classpath>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<pathelement path="${classes.dir}"/>
</classpath>
<batchtest todir="${reports.junit.xml.dir}">
<fileset dir="${classes.dir}">
<include name="**/*Test*.*" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage> <!--<jacoco:report>
<executiondata>
<file file="jacoco.exec" />
</executiondata>
<structure name="ma project coverage report">
<classfiles>
<fileset dir="${basedir}/target/classes" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<html destdir="${basedir}/target/report" />
<csv destfile="${basedir}/target/report/report.csv" />
<xml destfile="${basedir}/target/report/report.xml" />
</jacoco:report> --> </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="${basedir}/third/sonar-ant-task-2.2.jar" />
</taskdef> <!-- Execute Sonar -->
<sonar:sonar />
</target> <!-- ========= The main target "all" ========= -->
<target name="all" depends="clean,compile,test,sonar" /> </project>

build.xml

1.因为ant中没有像maven那样天然集成sonar,所以在脚本中需要加入很多sonar需要的属性。
    2.需要一个target任务执行jacoco:coverage并生成代码覆盖率文件jacoco.exec;这里注意jacoco.exec是在<jacoco:coverage destfile="${basedir}/jacoco.exec">指定,如果没有destfile属性则默认在项目根目录生成名为jacoco.exec文件。这个生成的文件要和<property name="sonar.jacoco.reportPath" value="jacoco.exec" />对应,这步是把jacoco.exec交给sonar去解析。

        <jacoco:coverage>
<junit fork="true" forkmode="once" printsummary="on" showoutput="true">
<formatter type="xml" usefile="true"/>
<classpath>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<pathelement path="${classes.dir}"/>
</classpath>
<batchtest todir="${reports.junit.xml.dir}">
<fileset dir="${classes.dir}">
<include name="**/*Test*.*" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>

  3.这步非必须;如果要生成jacoco报表还可以添加一个<jacoco:report>,可以生成html、xml、csv类型的报表。

            <jacoco:report>
<executiondata>
<file file="jacoco.exec" />
</executiondata>
<structure name="ma project coverage report">
<classfiles>
<fileset dir="${basedir}/target/classes" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<html destdir="${basedir}/target/report" />
<csv destfile="${basedir}/target/report/report.csv" />
<xml destfile="${basedir}/target/report/report.xml" />
</jacoco:report>

  4、代码覆盖率做完了就可以做sonar代码质量扫描了。

        <target name="sonar" depends="compile">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="${basedir}/third/sonar-ant-task-2.2.jar" />
</taskdef> <!-- 执行 Sonar -->
<sonar:sonar />
</target>
三、运行这个build脚本,成功后就可以去sonar服务器查看结果了。
具体参见 http://blog.chinaunix.net/uid-1835840-id-3616622.html

ant+sonar+jacoco代码质量代码覆盖率扫描的更多相关文章

  1. 测试框架:使用SONAR分析代码质量

    介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,比如pmd-cpd.checkstyl ...

  2. 使用 Sonar 检测代码质量

    经历了一段时间的加班赶项目进度之后,今天终于闲下来了.忽然不知道干啥.于是,想着做点什么吧.突然想起了码云上面有个代码分析的功能,用的是 Sonar 于是想来玩玩这个. 一.下载Sonar,和初始化, ...

  3. 通过Sonar的代码质量报告学习【如何写安全高质量的代码】

    1.不要用.size(),改用isEmpty() Using Collection.size() to test for emptiness works, but using Collection.i ...

  4. sonar的安装与代码质量检测实例

    说明:sonar依赖数据库. mysql优化 1.笔者使用的是mysql数据库.首先对mysql做简单的优化配置. [root@localhost bin]# cat /etc/my.cnf [mys ...

  5. DevOps之持续集成SonarQube代码质量扫描

    一.SonarQube介绍       SonarQube是一个用于代码质量检测管理的开放平台,可以集成不同的检测工具,代码分析工具,以及持续集成工具.SonarQube 并不是简单地把不同的代码检查 ...

  6. 代码质量检测-Sonar

    一. Sonar简介 sonarqube系统是一个代码质量检测工具 由以下四个组件组成(https://docs.sonarqube.org/display/SONAR/Architecture+an ...

  7. sonar+Jenkins 构建代码质量自动化分析平台

    1.Sonar 介绍 Sonar 是一个用于管理代码质量的开源工具,可以分析代码中的bug和漏洞以及Code Smells,支持20多种编程语言的检测,如java,c/c++,python,php等语 ...

  8. DEVOPS技术实践_05:sonar静态代码扫描

    一.SonarQube静态代码扫描平台 1.1 安装 https://www.sonarqube.org/官网 1.2 下载软件包 https://www.sonarqube.org/download ...

  9. Windows安装使用SonarQube7.4 对java项目进行代码质量扫描

    我这里使用7.4因为使用JDK是1.8 其它版本看下依赖版本就好 1.下载7.4版本安装包 https://binaries.sonarsource.com/CommercialDistributio ...

随机推荐

  1. GitHub笔记(一)——本地库基础操作

    零.基础概念理解——可以访问廖雪峰老师的网站https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c01 ...

  2. UI Recorder 安装教程(二)

    前言: UI Recorder支持无线native app(Android, iOS)录制, 基于macaca实现:https://macacajs.com/ 本次教程只针对无线native app( ...

  3. PAT甲题题解-1074. Reversing Linked List (25)-求反向链表

    题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...

  4. 剑指offer:从上往下打印二叉树

    题目描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 解题思路: 实际就是二叉树的中序遍历问题.之前在leetcode刷过类似题目. 利用队列完成即可. 代码: /* struct Tr ...

  5. Qrcode生成二维码的参数总结 及最小尺寸的测试

    Qrcode生成二维码,做过很多实验,探索最小规格的二维码到底是多少尺寸,和最高规格的二维码到底是多大尺寸.现在我总结总结: 有两种思路: 1.生成规格高的二维码,然后压缩到自己想要的尺寸的二维码.这 ...

  6. 第二个Sprint冲刺第 八天(燃尽图)

    因为今天停电了,所以我们也休息一天!

  7. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  8. Jenkins之手动安装

    Download and run Jenkins Download Jenkins. Open up a terminal in the download directory. Run java -j ...

  9. 微信小程序填坑之旅一(接入)

    一.小程序简介 小程序是什么? 首先“程序”这两个字我们不陌生.看看你手机上的各个软件,那就是程序.平时的程序是直接跑在我们原生的操作系统上面的.小程序是间接跑在原生系统上的.因为它嵌入在微信中,受微 ...

  10. BZOJ2423 HAOI2010最长公共子序列(动态规划)

    大讨论.注意去重. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...