使用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. MongoDB 安装教程

    前言: MongoDB是一个基于分布式文件存储的数据库.由C++旨在为WEB应用提供可扩展的高性能数据存储解决方案. 官方网站:https://www.mongodb.com/ 本次教程只针对wind ...

  2. LINUX内核分析第三周学习总结——构造一个简单的Linux系统MenuOS

    LINUX内核分析第三周学习总结——构造一个简单的Linux系统MenuOS 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163. ...

  3. 《Linux内核设计与实现》学习记录一

    chapter1 Linux内核简介 前言:Unix是一个具有相似应用程序编程接口(API)并且基于相似设计理念的操作系统家族. 1.1 Unix的历史 1.Unix演化版实现了任务管理.换页机制.T ...

  4. 《Linux内核分析与设计》读书笔记二

    第五章 5.1 与内核通信57 系统调用在用户空间进程和硬件设备之间添加了一个中间层,该层主要作用有三个: 首先它为用户空间提供了一种硬件的抽象接口,举例来说当需要读写文件的时候,应用程序就可以不去管 ...

  5. simhash-- 一种文档去重的算法

    最早看数学之美的时候,书中就提到了这个算法,当时没有做过相关地工作,没什么具体的印象.一年前转岗时面试时别人提到了这个算法,知道了simhash可以用来解决网页等海量数据的去重问题,很高效. 然后自己 ...

  6. “数学口袋精灵”第二个Sprint计划---第一天

    “数学口袋精灵”第二个Sprint计划----第一天进度 任务分配: 冯美欣:欢迎界面的音效 吴舒婷:游戏界面的动作条,选择答案后的音效 林欢雯:完善算法代码的设计 进度:   冯美欣:上网百度音乐资 ...

  7. 转帖 OKR

    什么是OKR OKR全称是Objectives and Key Results,即目标与关键成果法.OKR是一套定义和跟踪目标及其完成情况的管理工具和方法.1999年Intel公司发明了这种方法,后来 ...

  8. linq partition by

    static void Main(string[] args) { var beatles = (new[] { new { id=1 , inst = "guitar" , na ...

  9. The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. 使用System.getProperty方法,如何配置JVM系统属性

    原创文章,欢迎转载,转载请注明出处! 很多时候我们需要在项目中读取外部属性文件,用到了System.getProperty("")方法.这个方法需要配置JVM系统属性,那么如何配置 ...