使用Gradle插件进行代码分析
2016-02-28 11:40:38 | 来源:玩转帮会 | 投稿:佚名 | 编辑:dations

原标题:使用Gradle插件进行代码分析

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

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

PMD、CheckStyle 和 FindBugs

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

applyplugin:'checkstyle'
applyplugin:'pmd'
applyplugin:'findbugs'

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

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

findbugs{
findbugsTest.enabled=false
}
pmd{
pmdTest.enabled=false
}

下面,我们从Gradle中执行:

./gradlewcleanfindBugsMainpmdMain

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

输出结果如下:

:clean
:compileJava
Note:Someinputfilesuseuncheckedorunsafeoperations.
Note:Recompilewith-Xlint:uncheckedfordetails.
:processResources
:classes
:findbugsMainFAILED
FAILURE:Buildfailedwithanexception.
*Whatwentwrong:
Executionfailedfortask':findbugsMain'.
>FindBugsruleviolationswerefound.Seethereportat:file:///myprojects/build/reports/findbugs/main.xml
*Try:
Runwith--stacktraceoptiontogetthestacktrace.Runwith--infoor--debugoptiontogetmorelogoutput.
BUILDFAILED
Totaltime:24.597secs

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

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

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

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仅支持一种可用的输出格式。

./gradlewcleanfindBugsMainpmdMain
:clean
:compileJava
Note:Someinputfilesuseuncheckedorunsafeoperations.
Note:Recompilewith-Xlint:uncheckedfordetails.
:processResources
:classes
:findbugsMain
FindBugsruleviolationswerefound.Seethereportat:file:///myprojects/build/reports/findbugs/main.html
:pmdMain
Removedmisconfiguredrule:LoosePackageCouplingcause:Nopackagesorclassesspecified
Removedmisconfiguredrule:LoosePackageCouplingcause:Nopackagesorclassesspecified
174PMDruleviolationswerefound.Seethereportat:file:///myprojects/build/reports/pmd/main.html
BUILDSUCCESSFUL
Totaltime:6.013secs

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

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'
]
}

一个输出示例如下:

与Sonar整合

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

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

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

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进行配置:

applyplugin:"org.sonarqube"
sonarqube{
check
properties{
//SonarSpecificproperties
property'sonar.projectName','ProjectServices'//Thisisthedisplayprojectname
property'sonar.host.url','http://sonar:8080/'//ThisistheSonarServer
property'sonar.projectKey','com.wordpress.mrvivekr:project'//TheKeyusingwhichtheprojectdetailsaretracked
//JDBCProperties
property'sonar.jdbc.url','jdbc:jtds:sqlserver://ABCD:1433/SonarDB;domain=MYCOMP;SelectMethod=Cursor'
property'sonar.jdbc.username','userName'
property'sonar.jdbc.password','Secr#%'
//WhichSonarProfiletouse-thisisoptional
property'sonar.profile','JavaProfile'
}
}

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

./gradlewsonarqube
:compileJavaUP-TO-DATE
:processResourcesUP-TO-DATE
:classesUP-TO-DATE
:compileTestJava
:processTestResources
:testClasses
:test
.......ommitted.....
11:27:36.713INFO-JavaMainFilesASTscandone:812ms
11:27:36.714INFO-13/13sourcefileshavebeenanalyzed
11:27:36.715INFO-Javabytecodescan...
11:27:36.762INFO-Javabytecodescandone:47ms
11:27:36.762INFO-JavaTestFilesASTscan...
11:27:36.762INFO-5sourcefilestobeanalyzed
11:27:36.885INFO-JavaTestFilesASTscandone:123ms
11:27:36.885INFO-5/5sourcefileshavebeenanalyzed
11:27:36.888INFO-Packagedesignanalysis...
11:27:39.868INFO-Packagedesignanalysisdone:2980ms
11:27:39.884INFO-SensorJavaSquidSensor(done)|time=4307ms
11:27:39.884INFO-SensorLinesSensor
11:27:39.888INFO-SensorLinesSensor(done)|time=4ms
11:27:39.888INFO-SensorQProfileSensor
11:27:39.891INFO-SensorQProfileSensor(done)|time=3ms
11:27:39.891INFO-SensorInitialOpenIssuesSensor
11:27:45.592INFO-SensorInitialOpenIssuesSensor(done)|time=5701ms
11:27:45.592INFO-SensorProjectLinksSensor
11:27:45.730INFO-SensorProjectLinksSensor(done)|time=138ms
11:27:45.730INFO-SensorVersionEventsSensor
11:27:47.690INFO-SensorVersionEventsSensor(done)|time=1960ms
11:27:47.690INFO-Sensor[email protected]
11:27:47.694INFO-ExecuteFindbugs3.0.1...
11:27:48.557INFO-Foundfindbugsplugin:/myprojects/build/sonar/findbugs/fb-contrib.jar
11:27:48.596INFO-Foundfindbugsplugin:/myprojects/build/sonar/findbugs/findsecbugs-plugin.jar
11:27:48.612INFO-Findbugsoutputreport:/myprojects/build/sonar/findbugs-result.xml
Thefollowingclassesneededforanalysisweremissing:
javax.crypto.spec.SecretKeySpec
11:27:50.846INFO-ExecuteFindbugs3.0.1done:3153ms
11:29:31.499INFO-Sensor[email protected](done)|time=103809ms
11:29:31.499INFO-SensorSurefireSensor
11:29:31.500INFO-parsing/myprojects/build/test-results
11:29:31.558INFO-SensorSurefireSensor(done)|time=59ms
11:29:31.558INFO-SensorJaCoCoOverallSensor
11:29:31.565WARN-YouarenotusingthelatestJaCoCobinaryformatversion,pleaseconsiderupgradingtolatestJaCoCoversion.
11:29:31.565INFO-Analysing/myprojects/build/jacoco/test.exec
11:29:31.601WARN-YouarenotusingthelatestJaCoCobinaryformatversion,pleaseconsiderupgradingtolatestJaCoCoversion.
.........Ommitted.............
11:30:38.654INFO-ANALYSISSUCCESSFUL,youcanbrowsehttp://sonar:8080/dashboard/index/com.wordpress.mrvivekr:project
11:30:38.654INFO-Notethatyouwillbeabletoaccesstheupdateddashboardoncetheserverhasprocessedthesubmittedanalysisreport.
11:30:38.655INFO-Executingpost-jobclassorg.sonar.plugins.buildbreaker.AlertBreaker
11:30:38.657INFO-Executingpost-jobclassorg.sonar.plugins.buildbreaker.ForbiddenConfigurationBreaker
BUILDSUCCESSFUL
Totaltime:5mins16.499secs

您应该看到类似的输出:

享受代码分析吧!

tags:

上一篇  下一篇

相关:

HBase事务和并发控制机制原理

作为一款优秀的非内存数据库,HBase和传统数据库一样提供了事务的概念,只是HBase的事务是行级事务,可以保

给新手程序员的几点建议

本文由玩赚乐(www.banghui.org)– 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!

13平米的学生宿舍,设施倒是一应俱全

提起学生宿舍,能够划等号的词大概有“空间狭小”、“没法洗澡”、“没厨房”等类似的各种烦恼。不过越来越

写给产品经理的技术书:客户端、服务端和交互相关技术

产品经理有三大领域的技术是需要去攻克的,分别是:客户端相关技术、服务端相关技术、交互相关技术一、客户

产品经理频繁更改需求,为什么会令程序员烦恼?

我们先来看个段子:去饭店,坐下来。“服务员,给我来份宫保鸡丁!”“好嘞!”——————这叫原始需求大

JavaScript中call,apply,bind方法的总结

  why?call,apply,bind干什么的?为什么要学这个?   一般用来指定this的环境,在没有学之前,通常会

并发同步知多少

  找工作的时候是否经常看到要求有高并发,分布式系统的开发设计经验,或者高并发,分布式系统的开发设计

怀孕初期检查 准妈妈这样检查身体好


  怀孕女人最幸福的时刻,每一个准妈妈都期待自己的宝贝出生,但是怀

把握伊朗商机 经部拟缩减敏感货品项目

外贸协会今天(25日)表示,经济部正着手规划缩减敏感地区的战略性高科技货品管制项目,希望提高我国对伊朗出

JavaScript中this指针指向的彻底理解

首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,

站长推荐: