虚拟机Ubuntu上安装sonarqube及扫描仪sonar-scanner

注:再安装之前,最好在linux上创建一个普通用户,在普通用户上边安装sonar,因为sonar集成es,es不能在root用户下运行

1. 环境要求

  1. mysql-server-5.7
  2. sonarqube7.3(目前最新版)下载地址:https://www.sonarqube.org/downloads/  下载后解压即可
  3. jdk1.8
  4. sonar-scanner 下载地址:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner下载后解压即可

 2. 安装mysql

这个不多说,安装参考地址:https://www.cnblogs.com/zhuyp1015/p/3561470.html

3.创建数据库

mysql -u root -p
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; 
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;

3.配置环境变量

永久配置环境变量,进入etc/profile

export SONAR_HOME=/jar/sonar/

export SONAR_SCANNER_HOME=/jar/sonar-scanner
export PATH=$SONAR_SCANNER_HOME/bin:$PATH

配置完成保存文件,执行以下命令,环境变量就配置完成啦

source /etc/profile

4.修改配置文件

再sonar文件夹下有个conf文件夹,编辑sonar.properties文件

解除注释(即去掉#)并填写实际值:

sonar.jdbc.username=数据库用户名

sonar.jdbc.password=数据库密码

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonaruseUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

5.启动sonarqube

root@Ubuntu1610:/jar# cd sonar/bin/linux-x86-64
root@Ubuntu1610:/jar/sonar/bin/linux-x86-64# ./sonar.sh start

可以在/sonar/log下查看日志文件,防止出现错误信息

root@Ubuntu1610:/jar/sonar/logs# tail -fn 30 sonar.log
2018.08.31 18:22:31 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
2018.08.31 18:22:31 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up

再sonar.log李最后打印出这个就算成功启动了第一次启动要迁移数据所以需要一段时间

此处sonar7.3出现的错误:

在安装完sonar7.3启动的时候,报了这种错

org.sonar.server.platform.db.migration.step.MigrationStepExecutionException: Execution of migration step #2128 'Purge duplicate rules_parameters and their orphans' failed
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:79)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:67)
        at java.util.Iterator.forEachRemaining(Iterator.java:116)
        at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
        at java.util.stream.ReferencePipeline$Head.forEachOrdered(ReferencePipeline.java:590)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:52)
        at org.sonar.server.platform.db.migration.engine.MigrationEngineImpl.execute(MigrationEngineImpl.java:50)
        at org.sonar.server.platform.db.migration.DatabaseMigrationImpl.doUpgradeDb(DatabaseMigrationImpl.java:105)
        at org.sonar.server.platform.db.migration.DatabaseMigrationImpl.doDatabaseMigration(DatabaseMigrationImpl.java:80)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: You can't specify target table 'p1' for update in FROM clause
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
        at org.sonar.server.platform.db.migration.step.UpsertImpl.execute(UpsertImpl.java:66)
        at org.sonar.server.platform.db.migration.version.v72.PurgeDuplicateRulesParameters.execute(PurgeDuplicateRulesParameters.java:64)
        at org.sonar.server.platform.db.migration.step.DataChange.execute(DataChange.java:44)
        at org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl.execute(MigrationStepsExecutorImpl.java:75)
        ... 11 common frames omitted

这个问题是数据迁移失败,其中有一块数据有问题,因为是刚出来的版本,没有在论坛找到解决办法,最后在sonarqube社区论坛找到了这个错误的解决办法:

在数据库中手动插入不起作用的迁移。

insert into schema_migrations values (2128);

好了,重启一下sonar访问一下http://localhost:9000/sonar

账户密码默认的为admin,admin

6测试

先配置sonar-scanner

进入sonar-scanner/conf/sonar-scanner.properties

修改配置文件,去'#'修改配置文件

sonar.host.url=http://localhost:9002/sonar  <!--因为我的sonar端口号是9002,所以配置的是9002>

然后进入需要检查的代码文件夹,添加一个配置文件

root@Ubuntu1610:~/.jenkins/workspace/pipeline-demo# vim sonar-project.properties 
sonar.projectKey=test  key需要在sonar里创建,一个项目一个key
sonar.projectName=pipeline-demo  项目名称
sonar.projectVersion=0
sonar.sources=.
sonar.language=c   代码语言
sonar.sourceEncoding=UTF-8

在sonar里需要创建sonar.projectKey 步骤如下:

登录sonar http://192.168.1.38:9002/sonar  进入 配置-->项目-->创建项目

创建好后放入配置文件就可以,然后执行

root@Ubuntu1610:~/.jenkins/workspace/pipeline-demo# sonar-scanner 
INFO: Scanner configuration file: /jar/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /root/.jenkins/workspace/pipeline-demo/sonar-project.properties
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_101 Oracle Corporation (64-bit)
INFO: Linux 4.8.0-22-generic amd64
INFO: User cache: /root/.sonar/cache
INFO: SonarQube server 7.3.0
INFO: Default locale: "zh_CN", source code encoding: "UTF-8"
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=79ms
INFO: Server id: 11F655B3-AWWPSdm8_gF6zeUbYKJg
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=38ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: Load/download plugins (done) | time=58ms
INFO: Loaded core extensions: 
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=62ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=30ms
INFO: Load active rules
INFO: Load active rules (done) | time=1528ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=43ms
INFO: Project key: test
INFO: Project base dir: /root/.jenkins/workspace/pipeline-demo
INFO: -------------  Scan pipeline-demo
INFO: Base dir: /root/.jenkins/workspace/pipeline-demo
INFO: Working dir: /root/.jenkins/workspace/pipeline-demo/.scannerwork
INFO: Source paths: .
INFO: Source encoding: UTF-8, default locale: zh_CN
INFO: Load server rules
INFO: Load server rules (done) | time=136ms
INFO: Language is forced to c
INFO: Index files
WARN: File '/root/.jenkins/workspace/pipeline-demo/README.md' is ignored because it doesn't belong to the forced language 'c'
WARN: File '/root/.jenkins/workspace/pipeline-demo/SM4/Makefile' is ignored because it doesn't belong to the forced language 'c'
WARN: File '/root/.jenkins/workspace/pipeline-demo/sonar-project.properties' is ignored because it doesn't belong to the forced language 'c'
WARN: File '/root/.jenkins/workspace/pipeline-demo/SM4/demo1' is ignored because it doesn't belong to the forced language 'c'
WARN: File '/root/.jenkins/workspace/pipeline-demo/SM4/demo' is ignored because it doesn't belong to the forced language 'c'
INFO: 3 files indexed
INFO: Quality profile for c: new_c
INFO: Sensor C (Community) SquidSensor [c]
WARN: Property 'sonar.c.includeDirectories' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated.
WARN: Property 'sonar.c.forceIncludes' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated.
WARN: Property 'sonar.c.cFilesPatterns' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated.
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/main.c:6]: cannot find the sources for '#include <string.h>  '
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/main.c:7]: cannot find the sources for '#include <stdio.h>  '
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.h:9]: cannot find the sources for '#include <string.h>'
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.h:15]: cannot find the sources for '#include <inttypes.h>'
WARN: Metric 'comment_lines_data' is deprecated. Provided value is ignored.
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.c:6]: cannot find the sources for '#include <stdio.h>'
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.c:7]: cannot find the sources for '#include <stdlib.h>'
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.h:9]: cannot find the sources for '#include <string.h>'
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.h:15]: cannot find the sources for '#include <inttypes.h>'
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.h:9]: cannot find the sources for '#include <string.h>'
WARN: [/root/.jenkins/workspace/pipeline-demo/SM4/sm4.h:15]: cannot find the sources for '#include <inttypes.h>'
INFO: Sensor C (Community) SquidSensor [c] (done) | time=1089ms
INFO: Sensor SonarJavaXmlFileSensor [java]
INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=0ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=40ms
INFO: 1 file had no CPD blocks
INFO: Calculating CPD for 2 files
INFO: CPD calculation finished
INFO: Analysis report generated in 295ms, dir size=191 KB
INFO: Analysis reports compressed in 44ms, zip size=43 KB
INFO: Analysis report uploaded in 142ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9002/sonar/dashboard?id=test
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://localhost:9002/sonar/api/ce/task?id=AWWul4M9Cb-ngxoRWtDP
INFO: Task total time: 6.374 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 7.881s
INFO: Final Memory: 20M/667M
INFO: ------------------------------------------------------------------------
root@Ubuntu1610:~/.jenkins/workspace/pipeline-demo# 

最后出现success表示成功了,然后进入sonar http://192.168.1.38:9002/sonar 就能看到检查结果

如需查询详细信息请到官网  https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值