代码分析在大多数项目中通常是作为最后一个步骤(如果做了的话)完成的。其通常难以配置及与现有代码整合。

本文旨在勾勒出使用 Gradle 整合 PMD 与 FindBugs 的步骤,并将其与一个现有的 Sonar 构建工具进行整合。

PMD、CheckStyle 和 FindBugs

首先要做的就是在我们的 build.gradle 文件中添加插件:

1
2
3
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'findbugs'

这些插件能在我们的代码中启用 PMD、CheckStyle 和 FindBugs。

ostatsu
翻译于 1个月前

1人顶

 

 翻译的不错哦!

默认情况下,这些插件将对测试程序和主程序都进行分析。对于我们,需要避免在测试程序上运行FindBugs和PMD,我们使用如下命令:

1
2
3
4
5
6
findbugs{
    findbugsTest.enabled=false
}
pmd {
    pmdTest.enabled=false
}

下面,我们从Gradle中执行:

1
./gradlew clean findBugsMain pmdMain

这将对工程进行清理,然后对源代码执行FindBugs和PMD。

输出结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
:clean
:compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processResources
:classes
:findbugsMain FAILED
 
FAILURE: Build failed with an exception.
 
* What went wrong:
Execution failed for task ':findbugsMain'.
> FindBugs rule violations were found. See the report at: file:///myprojects/build/reports/findbugs/main.xml
 
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
 
BUILD FAILED
 
Total time: 24.597 secs

由于违背FindBugs规则,还没运行PMD构建已经失败。FindBugs和PMD的输出格式为XML。

结果报告保存在<proj_home>/build/reports文件夹。

社会主义好
翻译于 1周前

1人顶

 

 翻译的不错哦!

为了生成可读的HTML文档并忽略错误继续构建,我们在build.gradle文件中加入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
findbugs{
    ignoreFailures=true
    findbugsTest.enabled=false
 
}
 
tasks.withType(FindBugs) {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}
tasks.withType(Pmd){
    reports{
        xml.enabled=true
        html.enabled=true
    }
}
pmd {
   ignoreFailures = true
    pmdTest.enabled=false
}

task.withType用于对每种任务进行配置,您可以对PMD启用HTML和XML报告,对FindBugs启用HTML报告。

注意:在输出报告时,FindBugs仅支持一种可用的输出格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
./gradlew clean findBugsMain pmdMain
:clean
:compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processResources
:classes
:findbugsMain
FindBugs rule violations were found. See the report at: file:///myprojects/build/reports/findbugs/main.html
:pmdMain
Removed misconfigured rule: LoosePackageCoupling  cause: No packages or classes specified
Removed misconfigured rule: LoosePackageCoupling  cause: No packages or classes specified
174 PMD rule violations were found. See the report at: file:///myprojects/build/reports/pmd/main.html
 
BUILD SUCCESSFUL
 
Total time: 6.013 secs

对于PMD,您可以使用ruleSet选项设置规则集。下面是一些常用的规则集。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pmd {
   ignoreFailures = true
    pmdTest.enabled=false
    ruleSets = [
            'java-basic',
            'java-braces',
            'java-clone',
            'java-codesize',
            'java-comments',
            'java-controversial',
            'java-coupling',
            'java-design',
            'java-empty',
            'java-finalizers',
            'java-imports',
            'java-optimizations',
            'java-strictexception',
            'java-strings',
            'java-typeresolution',
            'java-unnecessary',
            'java-unusedcode'
    ]
}

一个输出示例如下:

社会主义好
翻译于 1周前

0人顶

 

 翻译的不错哦!

与Sonar整合

PMD,Checkstyle和Sonar都是很有用的工具。但是,从一个组织的角度,我们需要追踪项目代码质量的变化和一段时间内的技术债务。我们需要能够对照其它类似的团队。

为了能够做到这些,我们使用Sonar。本文假设您已经安装好了Sonar。

对于这种情况,Altassian提供了相应的Sonar插件。为了使用它,我们需要添加必要的插件。

1
2
3
4
5
6
7
8
9
10
11
buildscript {
    repositories {
        mavenCentral()
        maven { url "http://mvnrepo.nordstrom.net/nexus/content/groups/public" }
        maven { url "http://repo.maven.apache.org/maven2" }
    }
    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2"
    }
 
}

构建脚本安装完毕,我们队sonarqube进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apply plugin: "org.sonarqube"
 
sonarqube{
    check
    properties {
        // Sonar Specific properties
        property 'sonar.projectName''Project Services' // This is the display project name
        property 'sonar.host.url','http://sonar:8080/' // This is the Sonar Server
        property 'sonar.projectKey''com.wordpress.mrvivekr:project' // The Key using which the project details are tracked
 
       // JDBC Properties
        property 'sonar.jdbc.url','jdbc:jtds:sqlserver://ABCD:1433/SonarDB;domain=MYCOMP;SelectMethod=Cursor'
 
        property 'sonar.jdbc.username','userName'
        property 'sonar.jdbc.password','Secr#%'
 
      //Which Sonar Profile to use - this is optional
        property 'sonar.profile','JavaProfile'
    }
}

注意:sonar.profile是预配置sonar配置(和规则),应由您的公司预先配置完成。但这是可选而非必须。我的JDBC连接使用SQLServer连接。

1
./gradlew sonarqube
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources
:testClasses
:test
....... ommitted .....
11:27:36.713 INFO  - Java Main Files AST scan done: 812 ms
11:27:36.714 INFO  - 13/13 source files have been analyzed
11:27:36.715 INFO  - Java bytecode scan...
11:27:36.762 INFO  - Java bytecode scan done: 47 ms
11:27:36.762 INFO  - Java Test Files AST scan...
11:27:36.762 INFO  - 5 source files to be analyzed
11:27:36.885 INFO  - Java Test Files AST scan done: 123 ms
11:27:36.885 INFO  - 5/5 source files have been analyzed
11:27:36.888 INFO  - Package design analysis...
11:27:39.868 INFO  - Package design analysis done: 2980 ms
11:27:39.884 INFO  - Sensor JavaSquidSensor (done) | time=4307ms
11:27:39.884 INFO  - Sensor Lines Sensor
11:27:39.888 INFO  - Sensor Lines Sensor (done) | time=4ms
11:27:39.888 INFO  - Sensor QProfileSensor
11:27:39.891 INFO  - Sensor QProfileSensor (done) | time=3ms
11:27:39.891 INFO  - Sensor InitialOpenIssuesSensor
11:27:45.592 INFO  - Sensor InitialOpenIssuesSensor (done) | time=5701ms
11:27:45.592 INFO  - Sensor ProjectLinksSensor
11:27:45.730 INFO  - Sensor ProjectLinksSensor (done) | time=138ms
11:27:45.730 INFO  - Sensor VersionEventsSensor
11:27:47.690 INFO  - Sensor VersionEventsSensor (done) | time=1960ms
11:27:47.690 INFO  - Sensor org.sonar.plugins.findbugs.FindbugsSensor@7894f007
11:27:47.694 INFO  - Execute Findbugs 3.0.1...
11:27:48.557 INFO  - Found findbugs plugin: /myprojects/build/sonar/findbugs/fb-contrib.jar
11:27:48.596 INFO  - Found findbugs plugin: /myprojects/build/sonar/findbugs/findsecbugs-plugin.jar
11:27:48.612 INFO  - Findbugs output report: /myprojects/build/sonar/findbugs-result.xml
The following classes needed for analysis were missing:
  javax.crypto.spec.SecretKeySpec
11:27:50.846 INFO  - Execute Findbugs 3.0.1 done: 3153 ms
11:29:31.499 INFO  - Sensor org.sonar.plugins.findbugs.FindbugsSensor@7894f007 (done) | time=103809ms
11:29:31.499 INFO  - Sensor SurefireSensor
11:29:31.500 INFO  - parsing /myprojects/build/test-results
11:29:31.558 INFO  - Sensor SurefireSensor (done) | time=59ms
11:29:31.558 INFO  - Sensor JaCoCoOverallSensor
11:29:31.565 WARN  - You are not using the latest JaCoCo binary format version, please consider upgrading to latest JaCoCo version.
11:29:31.565 INFO  - Analysing /myprojects/build/jacoco/test.exec
11:29:31.601 WARN  - You are not using the latest JaCoCo binary format version, please consider upgrading to latest JaCoCo version.
......... Ommitted .............
11:30:38.654 INFO  - ANALYSIS SUCCESSFUL, you can browse http://sonar:8080/dashboard/index/com.wordpress.mrvivekr:project
11:30:38.654 INFO  - Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report.
11:30:38.655 INFO  - Executing post-job class org.sonar.plugins.buildbreaker.AlertBreaker
11:30:38.657 INFO  - Executing post-job class org.sonar.plugins.buildbreaker.ForbiddenConfigurationBreaker
 
BUILD SUCCESSFUL
 
Total time: 5 mins 16.499 secs

您应该看到类似的输出:

享受代码分析吧!

http://www.oschina.net/translate/code-analysis-with-gradle

https://dzone.com/articles/code-analysis-with-gradle

使用 Gradle 插件进行代码分析(转)的更多相关文章

  1. Eclipse插件(导出UML图,打开文件资源管理器插件,静态代码分析工具PMD,在eclipse上安装插件)

    目录 能够导出UML图的Eclipse插件 打开文件资源管理器插件 Java静态代码分析工具PMD 如何在eclipse上安装插件 JProfiler性能分析工具 从更新站点安装EclEmma 能够导 ...

  2. jQuery File Upload 插件 php代码分析

    jquery file upload php代码分析首先进入构造方法 __construct() 再进入 initialize()因为我是post方式传的数据  在进入initialize()中的po ...

  3. CNI bridge 插件实现代码分析

    对于每个CNI 插件在执行函数cmdAdd之前的操作是完全一样的,即从环境变量和标准输入内读取配置.这在http://www.cnblogs.com/YaoDD/p/6410725.html这篇博文里 ...

  4. Docker Libnetwork Bridge插件实现代码分析----创建网络部分

    // drivers/bridge/bridge.go // Create a new network using bridge plugin 1.func (d *driver) CreateNet ...

  5. Docker Libnetwork Bridge插件实现代码分析----初始化部分

    Bridge driver数据结构如下所示: type driver struct { config *configuration network *bridgeNetwork natChain *i ...

  6. Android 热修复Nuwa的原理及Gradle插件源码解析

    现在,热修复的具体实现方案开源的也有很多,原理也大同小异,本篇文章以Nuwa为例,深入剖析.  Nuwa的github地址 https://github.com/jasonross/Nuwa 以及用于 ...

  7. Gradle的构建过程都不会?带你全面了解Android如何自定义Gradle 插件

    目前 Android 工程的默认构建工具为 Gradle,我们在构建 APK 的时候往往会执行 ./gradlew assembleDebug 这样的命令.. 那么这个命令到底代表着什么含义呢?命令的 ...

  8. 完整全面的Java资源库(包括构建、操作、代码分析、编译器、数据库、社区等等)

    构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...

  9. Android官方技术文档翻译——Gradle 插件用户指南(5)

    昨晚把第五章未译完的几句话攻克了.只是第六章没怎么译,明后天又是周末,假设周一前第六章翻译完的话,周一再发第六章. 本文译自Android官方技术文档<Gradle Plugin User Gu ...

随机推荐

  1. PHP中如何实现 “在页面中一边执行一边输出” 的效果

    <?php    set_time_limit(0);  //在有关数据库的大量数据的时候,可以将其设置为0,表示无限制.    ob_end_clean();     //在循环输出前,要关闭 ...

  2. Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)

    我们知道,我们编写的应用程序都是有一定内存限制的.程序占用了过高的内存就easy出现OOM(OutOfMemory)异常.因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来 ...

  3. 【 D3.js 入门系列 --- 8 】 对话操作(事件)

    本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 这一节介绍怎样进行对话的操作,如鼠标单击,鼠标滑过等. 对一个被 ...

  4. 祖国版SoloWheel:Airwheel爱尔威火星车 拆箱&上手经验_运动户外_晒物广场_什么值得买

    http://m.baidu.com/from=844b/bd_page_type=1/ssid=0/uid=3151E6C0905477A13653132D762BB6FB/pu=sz%401320 ...

  5. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...

  6. C#反射机制详解(转)

    两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B型超声波,它可以透过肚皮通过向你体内发射B型超声波,当超声波遇到内脏壁的时 ...

  7. hdoj 1395 2^x mod n = 1 【暴力】

    策略 : 观察可知,1 或者是能被2整除的数都不会求余等于1, 仅仅须要推断一下是不是除1之外的奇数,在依次查找2^x(mod(n)) ? = 1就能够了 难点:假设每次都是在原来的基础上×2 再推断 ...

  8. Knockout应用开发指南 第三章:绑定语法(1)

    原文:Knockout应用开发指南 第三章:绑定语法(1) 第三章所有代码都需要启用KO的ko.applyBindings(viewModel);功能,才能使代码生效,为了节约篇幅,所有例子均省略了此 ...

  9. U14Linux的帐号与用户组

    1.在/etc/group和/etc/gshadow中查找mousegroup: grep mousegroup /etc/group /etc/gshadow (grep的使用) 2.其实Linux ...

  10. C#索引器的应用:自已写一个表格

    C#中索引器,在一个类中有很多的同一类型成员的时候,比较适用索引器. 环境:我们假设有一个动物园,里边有很多动物. 用法: 1.先定义一个类,这是成员的类型.在这里就是要定义一个Animal类: pu ...