A) Install the sonar runner
A1) Download link
reference link
http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+SonarQube+Runner
A2) Install SonarRunner
1) wget form
http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.3/sonar-runner-dist-2.3.zip .
$ cd ~/.tools
$ wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.3/sonar-runner-dist-2.3.zip
$ cd ~/.tools
$ wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.3/sonar-runner-dist-2.3.zip
2) uncompress
$ unzip sonar-runner-dist-2.3.zip
file list
~/.tools/sonar-runner-2.3/
|-- bin
| |-- sonar-runner
| `-- sonar-runner.bat
|-- conf
| `-- sonar-runner.properties
`-- lib
`-- sonar-runner-dist-2.3.jar
$ unzip sonar-runner-dist-2.3.zip
file list
~/.tools/sonar-runner-2.3/
|-- bin
| |-- sonar-runner
| `-- sonar-runner.bat
|-- conf
| `-- sonar-runner.properties
`-- lib
`-- sonar-runner-dist-2.3.jar
3) update global setting, these setting can be put in the parameter when sonar is running.
conf/sonar-runner.properties
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://localhost:9000
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
#sonar.login=admin
#sonar.password=admin
uncomment
highlight line
How to install sonar? This will be in the next reading note.
4) Create a sonar home
$ vi ~/.bash_profile
export SONAR_RUNNER_HOME=~/.tools/sonar-runner-2.3
PATH=$SONAR_RUNNER_HOME/bin
export PATH
5) check install status
$ sonar-runner -v
SonarQube Runner 2.3
Java 1.6.0_12 Sun Microsystems Inc. (64-bit)
Linux 2.6.18-164.el5 amd64
Installation is ok
B) Analyzing with SonarRuner
command line:
$ sonar-runner
B1) Simple project
1) Create a configuration file in the root directory of the project
sonar-porject.properties
# Required metadata
sonar.projectKey=java-sonar-runner-simple
sonar.projectName=Simple Java project analyzed with the SonarQube Runner
sonar.projectVersion=1.0
# Comma-separated paths to directories with sources (required)
sonar.sources=src
# Language
sonar.language=java
# Encoding of the source files
sonar.sourceEncoding=UTF-8
Highlight line should be change denpend your project. And this info will show in the sonar page.
B2) Multi-module project
1) way #1
set all the configuration in the properties in the root folder
|-- java-sonar-runner-modules-different-structures
| |-- Module 2
| | `-- sources
| | `-- Helloworld2.java
| |-- module1
| | `-- src
| | `-- Helloworld1.java
| |-- sonar-project.properties
sonar-project.properties file
sonar.projectKey=java-sonar-runner-modules-different-structures
sonar.projectName=Multi-module Java project analyzed with the SonarQube Runner (modules have different structures)
sonar.projectVersion=1.0
# Set modules IDs
sonar.modules=module1,module2
# Modules inherit properties set at parent level
sonar.sources=src
sonar.sourceEncoding=UTF-8
sonar.language=java
# Default module base directory is <curent_directory>/<module_ID>
# It has to be overriden for module2
module2.sonar.projectBaseDir=Module 2
# Overrides some parent properties in module2
module2.sonar.sources=sources
module2.sonar.projectName=Module #2
2) way #2
set the configuration in the multiple properties files. These files will be put in the each module.
|-- java-sonar-runner-modules-own-configuration-file
| |-- Module 2
| | |-- sonar-project.properties
| | `-- sources
| | `-- Helloworld2.java
| |-- module1
| | |-- sonar-project.properties
| | `-- src
| | `-- Helloworld1.java
| |-- sonar-project.properties
root property
sonar.projectKey=java-sonar-runner-modules-own-configuration-file
sonar.projectName=Multi-module Java project analyzed with the SonarQube Runner (each module has its own configuration file)
sonar.projectVersion=1.0
# Set modules IDs
sonar.modules=module1,module2
# Modules inherit properties set at parent level
sonar.sources=src
sonar.sourceEncoding=UTF-8
sonar.language=java
# By default, the base directory for a module is <current_dir>/<module_ID>.
module2.sonar.projectBaseDir=Module 2
module1 property
sonar.projectName=Module #1 (name can be any one, other property will be inherited from root sonar.properties)
module2 property
sonar.projectName=Module #2
sonar.sources=sources
Noteworthy:
1) Children module will inherit their parent's properties
2) Module base directory(src directory) can be specified for special case
B3) Multi-module & Multi-language project
The other setting is the same with Multi-module project's configuration.
The different is each module's language. It can be set in the sonar.properties
module1.sonar.language=java
module2.sonar.language=js
B4) Running Other Tasks
#Before SonarQube 3.6, it was only possible to run a project analysis. Since SonarQube 3.6, it is possible to run other tasks such as:# To run the computation of views (Views plugin is required)
sonar-runner views
# To run the computation of reports (Report plugin is required)
sonar-runner report
# To run the computation of developer data (Developer Cockpit plugin is required)
sonar-runner devcockpit
B5) Advanced Usage
1) if thers's no sonar-project.properties file. Some setting can be set as parameter, like below:
sonar-runner -Dsonar.projectKey=myproject -Dsonar.sources=src1
2) if sonar settubg name is not sonar-project.properties. You can set it
Ex:
sonar-runner -Dproject.settings=../myproject.properties
C) Troubleshooting
c1) out of memory
you can set SONAR_RUNNER_OPTS
Ex:
export SONAR_RUNNER_OPTS="-Xmx888m -XX:MaxPermSize=888m"
D) Noteworthy
d1) sonar runner cannot support cobertura, only maven support.
if you want to use sonar-runner to analyz, you need to generate a report before sonar analyz.
rex ding@2013-12-25