准备工作:去到官网下载1.sonarqube4.
2.sonar-runner2.4以前,2.4之后改为sonar-scanner2.5
3.java,jdk1.7
4.mysql 5.7.10,免安装版
第一阶段,配置mysql:1.复制my-default.ini,改为my.ini
复制以下内容替换掉my.ini以前内容:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = D:/sonarQube/mysql-5.7.10-win32 #mysql路径
datadir = D:/sonarQube/mysql-5.7.10-win32/data #在mysql中创建一个data文件夹注意data前/
# port = .....
# server_id = ....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
2.管理员模式的cmd,输入mysql路径\bin\mysqld.exe --install安装(安装后路径里面会新建一个data文件夹),移除用 前面的路径 --remove。
3.打开mysql : 还是在cmd下输入 net start mysql。
第二阶段,解压sonarqube:去到bin目录下,根据操作系统选择启动服务,点击StartSonar.bat,启动sonarqube。
第三阶段,配置sonar-scanner:将conf下的配置文件向之前改sonarqube类似的更改,取消注释mysql,然后启动。
第四阶段,在项目下新建,sonar-project.properties,加上如下代码:
# required metadata
# My project------项目名称
sonar.projectKey=JAVA
sonar.projectName=JAVA
sonar.projectVersion=1.0
# path to source directories (required)
# src---------修改成你的源文件夹路径,多个路径逗号隔开
sonar.sources=F:/MyJava/JAVA
# path to test source directories (optional)
# testDir1,testDir2--------修改成你的测试文件夹路径, 如果测试可以取消注释
# tests=testDir1,testDir2
# path to project binaries (optional), for example directory of Java #bytecode
# binaries--------编译后的class目录
binaries=bin
# path to project libraries (optional)
#libraries=junit.jar
# advanced parameters# required metadata
# Language
sonar.language=java
# Encoding of the source files
sonar.sourceEncoding=UTF-8
第五阶段,已经可以实现对java项目的管理,在项目目录下cmd,然后输入sonar-runner,成功后,代码会在sonarqube上面管理。
第六阶段,要实行对maven项目的管理,还需要配置maven的setting.xml文件。加上如下代码,数据库还是以mysql示范:
<!--sonar配置maven 文件项 -->
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- database-->
<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>http://localhost:9000</sonar.host.url>
</properties>
</profile>
第七阶段,在maven项目文件下cmd输入:mvn sonar:sonar,然后大功告成