Sonar是一个集成的代码质量工具,主要用于Java,但现在也可以通过支持多种其他语言,例如javascript, C#等。
Jenkins是最流行的持续集成工具。
有两种方式在Jenkins Job中建立Sonar步骤: Standalone sonar runner 或 sonar-maven-plugin
(本文假设你已经安装了Jenkins)
安装 Sonar
- Need to install a sonar instance, download from http://www.sonarsource.org/downloads
- start sonar service. E.g on a 64bit linux server, use:
1
$ /bin/linux-x86-64/sonar.sh start
- the you can access http://127.0.0.1:9000 as Sonar URL. Note: By default the built-in H2 database does not support other server to access this sonar instance remotely by the IP/URL, unless you manually config it to use your own database instance like mysql.
- if you want to analyze javascript other than JAVA, download a sonar plugin: http://docs.codehaus.org/display/SONAR/JavaScript+Plugin
- download and install sonar-runner if you don’t use sonar-maven-plugin: http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner
- config sonar in Jenkins: http://127.0.0.1:8080/jenkins/configure
A javascript project with standalone runner
- add a post-build step of “Invoke Standalone Sonar Analysis”
- in the Project properties field
- assume you already use Cobertura running unit tests
1 | \# required metadata |
A JAVA project with standalone runner
- add a post-build step of “Invoke Standalone Sonar Analysis”
- in the Project properties field
- assume you already use Cobertura running unit tests, so to reuse the report
1 | \# required metadata |
A Java(Maven) project with sonar-maven-plugin
- add below properties in pom.xml (assume using emma but not reuse the report. For other tool like jacoco, cobertura, see http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Unit+Tests )
- add a post-build action of “Sonar”
1 | UTF-8 |