1. >> 安装
1. 从下载最新版本.(本文使用的是3.1.1)
2. 解压下载包, 进入解压文件的bin目录下, 会有对应不同系统的多个版本. 如下图:
(本文使用的是linux-x86-64版本)
3. 将bin/linux-x86-64目录配置到LINUX的PATH的系统变量里.
4. 使用sonar.sh start即可启动(通过解压后文件的logs目录可以看到启动的情况).启动成功后访问. 默认使用的数据库为derby.
5. 将数据库改为MYSQL. 通过修改解压文件的conf目录下的sonar.properties配置. 将
sonar.jdbc.url
sonar.jdbc.driverClassName
sonar.jdbc.validationQuery
sonar.jdbc.username
sonar.jdbc.password
的注释打开并配置. 参考配置如下:
#----- Credentials# Permissions to create tables and indexes must be granted to JDBC user.# The schema must be created first.sonar.jdbc.username: rootsonar.jdbc.password: coke#----- Embedded database Derby# Note : it does not accept connections from remote hosts, so the# sonar server and the maven plugin must be executed on the same host.# Comment the following line to deactivate the default embedded database.#sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true#sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver#sonar.jdbc.validationQuery: values(1)# directory containing Derby database files. By default it's the /data directory in the sonar installation.#sonar.embeddedDatabase.dataDir:# derby embedded database server listening port, defaults to 1527#sonar.derby.drda.portNumber: 1527# uncomment to accept connections from remote hosts. Ba default it only accepts localhost connections.#sonar.derby.drda.host: 0.0.0.0#----- MySQL 5.x/6.x# Comment the embedded database and uncomment the following line to use MySQLsonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true# Optional propertiessonar.jdbc.driverClassName: com.mysql.jdbc.Driversonar.jdbc.validationQuery: select 1另外, 对应的数据库需要自己创建, 否则会启动sonar失败.
ps: 学习参考自:
2. >> 使用Sonar Runner分析项目
1. 从下载最新版本.(本文使用的是1.4)
2. 解压后进入conf目录, 修改sonar-runner.properties配置文件(本例使用mysql). 将
sonar.jdbc.url
sonar.jdbc.driver
sonar.jdbc.username
sonar.jdbc.password
的注释打开并配置. 参考配置如下:
#----- Default Sonar server#sonar.host.url=http://localhost:9000#----- PostgreSQL#sonar.jdbc.url=jdbc:postgresql://localhost/sonar#sonar.jdbc.driver=org.postgresql.Driver#----- MySQLsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8sonar.jdbc.driver=com.mysql.jdbc.Driver#----- Oracle#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE#sonar.jdbc.driver=oracle.jdbc.driver.OracleDriver#----- Global database settingssonar.jdbc.username=rootsonar.jdbc.password=coke#----- Default directory layout#sources=src/main/java#tests=src/test/java#binaries=target/classessonar.sourceEncoding=UTF-83. 将bin目录配置到LINUX的PATH的系统变量里.
4. 在项目的根路径创建名为 sonar-project.properties 的文件. 文件内容格式如下:
# required metadatasonar.projectKey=my:projectsonar.projectName=My projectsonar.projectVersion=1.0 # path to source directories (required)sources=srcDir1,srcDir2 # path to test source directories (optional)tests=testDir1,testDir2 # path to project binaries (optional), for example directory of Java bytecodebinaries=binDir # optional comma-separated list of paths to libraries. Only path to JAR file and path to directory of classes are supported.libraries=path/to/library.jar,path/to/classes/dir # Uncomment this line to analyse a project which is not a java project. # The value of the property must be the key of the language.#sonar.language=cobol # Additional parametersmy.property=value例子:
>> 项目路径如下:
+ src\java 源代码
+ src\test 测试类源代码
+ src\config 项目配置文件
>> 在与src同级的目录里创建名为sonar-project.properties的文件, 图片如下:
>> 执行sonar.sh start开始检查代码啦. 之后在打开Sonar的页面()就可以看到该项目的代码检查啦.