多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 前言 sonar相关的信息分2章,一部分是sonar,一部分是sonar runner,这篇学习的是sonar。 sonar插件依靠于sonar runner插件,且sonar的版本要在2.1.1以上。如果你要执行sonar任务,就在命令行下敲gradle sonarAnalyze。sonarAnalyze任务是独立存在的,就是该任务来分析代码,该任务不依靠其他任务。该任务不依靠源码文件,而是针对class文件和build文件,所以尽量在使用前进行全build。正常情况下,会一天产生一次报告。 # 知识点 ## 1.sonar 插件 ~~~ apply plugin: 'sonar' ~~~ ## 2.配置sonar的服务器和数据库 在build.gradle添加一个sonar(api中的SonarRootModel)任务,可以配置上面的信息 build.gradle ~~~ sonar{ server{ url = "http://localhost:9002/" } database{ url = "jdbc:mysql://localhost:3306/sonar" driverClassName = "com.mysql.jdbc.Driver" username = "sonar" password = "sonar" } } ~~~ server(api中的SonarServer)里的url代表你在浏览器中访问sonar服务器的地址。 database(api中的SonarDataBase)里代表的是数据库的信息,包括访问地址,驱动,用户名和密码。 这里面要根据你的实际属性来填。 ## 3.配置sonar的项目属性 sonar中的project(api中SonarProject)用来设置如何分析代码。 跟上面一样,它也是在sonar任务中添加,如下所示: build.gradle: ~~~ sonar{ server{ url = "http://localhost:9002/" } database{ url = "jdbc:mysql://localhost:3306/sonar" driverClassName = "com.mysql.jdbc.Driver" username = "sonar" password = "sonar" } project { coberturaReportPath = file("$buildDir/cobertura.xml") } } ~~~ ## 4.多项目的分析 在多项目的构建中,你可以在root project项目中配置sonar,让其分析各个子项目,这样的速度会比你一个一个单独分析要快。sonar能够在页面上展现出各个项目的树形结构。sonar中的server和database只需要在根项目配置即可,无需单独在每个子项目配置一篇。可以通过subprojects任务达到这种效果。但是project的一些自定义的设置需要单独去配置。例如下面的代码 ### 设置编码for每一个子项目 build.gradle ~~~ subprojects { sonar { project { sourceEncoding = "UTF-8" } } } ~~~ 如果你想为某一个特定的子项目设置,可以通过gradle中project任务来设置,格式为project('子项目名'),例如下面的代码(也是在root project中build.gradle) build.gradle: ### 特定子项目的配置 ~~~ project(":project1") { sonar { project { skip = true } } } ~~~ 上面代码的作用是在进行sonar分析时,不对project1这个项目进行分析。自然在web页面中的树形结构中也就不会显示该项目。 还有一个语言项,用来配置子项目的编程语言。但是该属性一个子项目只能有一个语言,不能配置多个语言。下面的例子是为project2设置语言为groovy。 build.gradle ~~~ project(":project2") { sonar { project { language = "groovy" } } } ~~~ 当配置的项只有一项时,下面的语法更简洁: ~~~ project(":project2").sonar.project.language = "groovy" ~~~ ## 5.分析自定义的Source Set 默认情况下,sonar只会分析项目中main和test这两个sourceset。你也可以将自己自定义的sourceSets添加到sonar分析的范围中,这样sonar就会一并分析了。 你可以通过下面的方式来将自己自定义的sourcesets追加到sonar分析的目录中: build.gradle: ~~~ sonar.project { sourceDirs += sourceSets.custom.allSource.srcDirs testDirs += sourceSets.integTest.allSource.srcDirs } ~~~ 或者 ~~~ sonar{ server{ url = "http://localhost:9002/" } database{ url = "jdbc:mysql://localhost:3306/sonar" driverClassName = "com.mysql.jdbc.Driver" username = "sonar" password = "sonar" } project { coberturaReportPath = file("$buildDir/cobertura.xml") sourceDirs += sourceSets.custom.allSource.srcDirs testDirs += sourceSets.integTest.allSource.srcDirs } } ~~~ 其中custom和integTest是你自定义的sourceSets。 ## 6.分析非java语言项目 第4点已经讲过了,你可以为每一个子项目设置不同的语言。 ## 7.设置sonar自定义属性 sonar中属性设置都是以key-value的格式设置的,下面的知识就过一遍吧,对于新手来说,这些东西还太遥远。只需要记住有2中定义方法,一种是全局定义方式,包括根项目和子项目,还有一种范围小的,只适用于本项目。分别通过withGlobalProperties和withProjectProperties来定义 ### 全局 build.gradle: ~~~ sonar.withGlobalProperties { props -> props["some.global.property"] = "some value" // non-String values are automatically converted to Strings props["other.global.property"] = ["foo", "bar", "baz"] } ~~~ ### 私有 ~~~ sonar.project.withProjectProperties { props -> props["some.project.property"] = "some value" // non-String values are automatically converted to Strings props["other.project.property"] = ["foo", "bar", "baz"] } ~~~ ## 7.命令行的方式配置sonar (还是不要这么干的好,多累啊) 可设置的属性如下: server.url:服务器地址 database.url:数据库地址 database.driverClassName:驱动名 database.username:数据库用户名 database.password:数据库密码 showSql:打印sql语言 showSqlResults:打印结果 verbose:log级别 forceAnalysis:强制分析 ## 8.执行方式 在命令行下敲下面语句就能执行sonar任务 ~~~ gradle sonarAnalyze ~~~ # 实战 为什么没有实战例子,因为我在公司,你懂的。等晚上再加: ~~~ FAILURE: Build failed with an exception. * What went wrong: Could not resolve all dependencies for configuration ':testCompile'. > Could not resolve junit:junit:4.+. Required by: :TestNG_gradl:1.0 > Failed to list versions for junit:junit. > Unable to load Maven meta-data from https://repo1.maven.org/maven2/juni /junit/maven-metadata.xml. > Could not GET 'https://repo1.maven.org/maven2/junit/junit/maven-meta ata.xml'. > Connection to https://repo1.maven.org refused * 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: 28.194 secs ~~~