介绍
SonarQube 能干啥????看下面这张图就应该知道他能干啥了!
对就是各种分析你写的代码,帮助我们检查隐藏的问。好处不多说了,装上试试吧!
1、SonarQube的安装(这里只说linux系统)
首先我们先来下载SonarQube
下载地址:https://www.sonarqube.org/downloads/ 目前比较新的是7.2.1
SonarQube 的目录是这样的我们需要配置一下SonarQube的环境变量
我的是在Linux 上,目录在/etc/profile下
vi /etc/profile
export SONAR_SCANNER_HOME=/home/sonarUser/sonarscanner/sonar-scanner-2.8
export PATH=$PATH:$SONAR_HOME/linux-x86-64
这段代码放到 profile 最后就可以了(当然这个地址需要自己换哈)
保存好后执行
source /etc/profile
让其配置生效 执行
$ sonar.sh
Usage: /home/sonar/sonarqube-7.2.1/bin/linux-x86-64/sonar.sh { console | start | stop | restart | status | dump }
配置sonar
在sonarqube-7.2.1/conf的目录下有sonar.properties
vi sonar.properties
修改我们用的是mysql
# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=xxx
sonar.jdbc.password=xxx
#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- DEPRECATED
#----- MySQL >=5.6 && <8.0
# Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://xxx.xxx.xx.xx:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
保存即可。
启动 SonarQube
在启动中你有可能会启动失败失败原因如下(原因是sonarqube 从6.x后使用了es 但是es 是不能用root 用户启动的)
在 sonarqube-7.2.1的根目录下有logs es.log 和 sonar.log
es.log
java.lang.RuntimeException: can not run elasticsearch as root
3 at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.6.3.jar:5.6.3]
4 at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:195) ~[elasticsearch-5.6.3.jar:5.6.3]
5 at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:342) [elasticsearch-5.6.3.jar:5.6.3]
6 at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:132) [elasticsearch-5.6.3.jar:5.6.3]
7 at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:123) [elasticsearch-5.6.3.jar:5.6.3]
8 at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:70) [elasticsearch-5.6.3.jar:5.6.3]
9 at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134) [elasticsearch-5.6.3.jar:5.6.3]
10 at org.elasticsearch.cli.Command.main(Command.java:90) [elasticsearch-5.6.3.jar:5.6.3]
11 at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) [elasticsearch-5.6.3.jar:5.6.3]
12 at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) [elasticsearch-5.6.3.jar:5.6.3]
解决以上问题
$ adduser sonarUser
$ passwd sonarUser
$ chown -R sonarUser:sonarUser sonarqube-7.2.1
依次执行以上命令 使用sonarUser 账户启动SonarQube
如果不出意外的话就成了!请访问:http://xxx.xxx.xxx.xx:9000/ 我这个端口没有修改
哦哦对了,我这个是弄完后的截图当然还有其他插件需要下载,比如汉化
我们等了SonarQube用户名和密码都是 Admin
安装后重启服务即可!
2、maven 的集成
需要修改你本地的maven setting.xml
找到 <profiles>在其内部添加如下代码
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:mysql://XX.XX.XX.XX:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>XXX</sonar.jdbc.username>
<sonar.jdbc.password>XXX</sonar.jdbc.password>
<sonar.host.url>http://XXXX.XX.XX.XX:9000</sonar.host.url>
</properties>
</profile>
这样就可以了来试试
找到你项目的根目录就是有pom.xml的哪个目录
执行
mvn clean install sonar:sonar
等待出现以下内容
[INFO] 8 files had no CPD blocks
[INFO] Calculating CPD for 70 files
[INFO] CPD calculation finished
[INFO] Analysis report generated in 301ms, dir size=526 KB
[INFO] Analysis reports compressed in 286ms, zip size=288 KB
[INFO] Analysis report uploaded in 584ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://xxx.xxx.xxx:9000/dashboard?id=angproject%3Aangproject
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://xxx.xxx.xxx:9000/api/ce/task?id=AWS6pbIZAOMtYibgnlk7
[INFO] Task total time: 54.795 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:13 min
[INFO] Finished at: 2018-07-21T10:22:58+08:00
[INFO] Final Memory: 63M/857M
[INFO] ------------------------------------------------------------------------
来刷新一下SonarQube
第一截图出现了。到此安装就完毕了。谢谢大家的观看