windows下sonarqube+sonar-runner安装记录

    sonarqube是一套代码质量管理工具系统,sonar-runner是一个静态扫描代码的工具,它可以把扫描的结果交给sonarqube来管理。

    这里安装的sonarqube版本比较老旧5.1.2,因为我安装最新的sonarqube在启动的时候,需要jdk版本比较高,就放弃了最新版本安装。sonar-runner是2.4。

     sonarqube和sonar-runner都需要连接数据库,这里就选择比较常见的mysql。

     环境准备主要就是jdk1.8,mysql5.7,sonarqube5.1.2,sonar-runner2.4。

     sonarqube:https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-5.1.2.zip

     sonar-runner:https://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip 

     这两个也都是面安装版本,解压sonarqube到d:\tools,然后把sonar-runner解压到sonarqube目录。 

   windows下sonarqube+sonar-runner安装记录_第1张图片

    为了后面在命令行下执行sonar-runner方便,这里把sonar-runner-2.4\bin加入Path环境变量。 

    设置数据库连接信息,编辑sonarqube/conf/sonar.properties文件,主要增加如下配置:

sonar.jdbc.username=root
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=localhost
sonar.web.context=/
sonar.web.port=9000

    配置文件中,这些配置项都有,我们只需要打开注释,添加上自己环境相关的配置。

    还需要修改sonar-runner的配置文件,编辑sonar-runner-2.4/conf/sonar-runner.properties:

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.username=root
sonar.jdbc.password=123456

    接着,启动sonar-qube:找到sonarqube\bin\windows-x86-64\StartSonar.bat,双击运行:

    看到如下信息,表示启动成功:

windows下sonarqube+sonar-runner安装记录_第2张图片

    可以通过浏览器打开http://localhost:9000

    正常情况下,可以进入首页,但是里面右侧都是空白的,我们需要先扫描代码,这里扫描是我们手动扫描本地代码,所以需要在本地代码中加入配置信息sonar-project.properties:

sonar.projectKey=springexample
sonar.projectName=springexample
sonar.projectVersion=1.0.2
sonar.sources=src
sonar.binaries=target
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.scm.disabled=true

    这时候就是sonar-runner登场了,在项目路径下,启动命令行,执行sonar-runner命令:

E:\workspace\java\springexample>sonar-runner
D:\tools\sonarqube\sonar-runner-2.4
SonarQube Runner 2.4
Java 1.8.0_331 Oracle Corporation (64-bit)
Windows 10 10.0 amd64
INFO: Runner configuration file: D:\tools\sonarqube\sonar-runner-2.4\conf\sonar-runner.properties
INFO: Project configuration file: E:\workspace\java\springexample\sonar-project.properties
INFO: Default locale: "zh_CN", source code encoding: "UTF-8"
INFO: Work directory: E:\workspace\java\springexample\.\.sonar
INFO: SonarQube Server 5.1.2
22:50:36.437 INFO  - Load global repositories
22:50:37.328 INFO  - Load global repositories (done) | time=894ms
22:50:37.329 INFO  - Server id: 20230404223924
22:50:37.331 INFO  - User cache: C:\Users\Administrator\.sonar\cache
22:50:37.341 INFO  - Install plugins
22:50:37.497 INFO  - Install JDBC driver
22:50:37.504 INFO  - Create JDBC datasource for jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
22:50:38.495 INFO  - Initializing Hibernate
22:50:39.694 INFO  - Load project repositories
22:50:40.029 INFO  - Load project repositories (done) | time=335ms
22:50:40.037 INFO  - Load project settings
22:50:40.343 INFO  - Load technical debt model
22:50:40.367 INFO  - Apply project exclusions
22:50:40.608 INFO  - -------------  Scan springexample
22:50:40.614 INFO  - Load module settings
22:50:40.721 INFO  - Language is forced to java
22:50:40.728 INFO  - Load rules
22:50:40.849 INFO  - Base dir: E:\workspace\java\springexample
22:50:40.849 INFO  - Working dir: E:\workspace\java\springexample\.sonar
22:50:40.851 INFO  - Source paths: src
22:50:40.851 INFO  - Binary dirs: target
22:50:40.851 INFO  - Source encoding: UTF-8, default locale: zh_CN
22:50:40.851 INFO  - Index files
22:50:40.875 INFO  - 5 files indexed
22:50:41.017 INFO  - Quality profile for java: Sonar way
22:50:41.037 INFO  - Sensor JavaSquidSensor
22:50:41.417 WARN  - sonar.binaries and sonar.libraries are deprecated since version 2.5 of sonar-java-plugin, please use sonar.java.binaries and sonar.java.libraries instead
22:50:41.422 INFO  - Java Main Files AST scan...
22:50:41.424 INFO  - 5 source files to be analyzed
22:50:41.734 INFO  - Java Main Files AST scan done: 312 ms
22:50:41.734 INFO  - 5/5 source files analyzed
22:50:41.736 INFO  - Java bytecode scan...
22:50:41.744 WARN  - Class 'com/example/App' is not accessible through the ClassLoader.
22:50:41.744 WARN  - Class 'com/example/config/ThreadPoolConfig' is not accessible through the ClassLoader.
22:50:41.744 WARN  - Class 'com/example/config/WebSocketConfig' is not accessible through the ClassLoader.
22:50:41.744 WARN  - Class 'com/example/task/AsyncTask' is not accessible through the ClassLoader.
22:50:41.744 WARN  - Class 'com/example/task/AsyncTaskTest' is not accessible through the ClassLoader.
22:50:41.745 WARN  - Class 'com/example/App' is not accessible through the ClassLoader.
22:50:41.745 WARN  - Class 'com/example/config/ThreadPoolConfig' is not accessible through the ClassLoader.
22:50:41.745 WARN  - Class 'com/example/config/WebSocketConfig' is not accessible through the ClassLoader.
22:50:41.745 WARN  - Class 'com/example/task/AsyncTask' is not accessible through the ClassLoader.
22:50:41.745 WARN  - Class 'com/example/task/AsyncTaskTest' is not accessible through the ClassLoader.
22:50:41.746 WARN  - Class 'com/example/App' is not accessible through the ClassLoader.
22:50:41.747 WARN  - Class 'com/example/config/ThreadPoolConfig' is not accessible through the ClassLoader.
22:50:41.747 WARN  - Class 'com/example/config/WebSocketConfig' is not accessible through the ClassLoader.
22:50:41.747 WARN  - Class 'com/example/task/AsyncTask' is not accessible through the ClassLoader.
22:50:41.747 WARN  - Class 'com/example/task/AsyncTaskTest' is not accessible through the ClassLoader.
22:50:41.747 INFO  - Java bytecode scan done: 11 ms
22:50:41.748 INFO  - Java Test Files AST scan...
22:50:41.748 INFO  - 0 source files to be analyzed
22:50:41.748 INFO  - Java Test Files AST scan done: 0 ms
22:50:41.750 INFO  - 0/0 source files analyzed
22:50:41.751 INFO  - Package design analysis...
22:50:41.757 INFO  - Package design analysis done: 6 ms
22:50:41.773 INFO  - Sensor JavaSquidSensor (done) | time=736ms
22:50:41.773 INFO  - Sensor Lines Sensor
22:50:41.775 INFO  - Sensor Lines Sensor (done) | time=2ms
22:50:41.775 INFO  - Sensor QProfileSensor
22:50:41.779 INFO  - Sensor QProfileSensor (done) | time=4ms
22:50:41.779 INFO  - Sensor InitialOpenIssuesSensor
22:50:41.791 INFO  - Sensor InitialOpenIssuesSensor (done) | time=12ms
22:50:41.791 INFO  - Sensor ProjectLinksSensor
22:50:41.802 INFO  - Sensor ProjectLinksSensor (done) | time=11ms
22:50:41.802 INFO  - Sensor VersionEventsSensor
22:50:41.827 INFO  - Sensor VersionEventsSensor (done) | time=25ms
22:50:41.827 INFO  - Sensor SurefireSensor
22:50:41.829 INFO  - parsing E:\workspace\java\springexample\.sonar\build\surefire-reports
22:50:41.830 WARN  - Reports path not found: E:\workspace\java\springexample\.sonar\build\surefire-reports
22:50:41.830 INFO  - Sensor SurefireSensor (done) | time=3ms
22:50:41.830 INFO  - Sensor SCM Sensor
22:50:41.830 INFO  - SCM Sensor is disabled
22:50:41.830 INFO  - Sensor SCM Sensor (done) | time=0ms
22:50:41.830 INFO  - Sensor CPD Sensor
22:50:41.830 INFO  - JavaCpdEngine is used for java
22:50:41.830 INFO  - Cross-project analysis disabled
22:50:41.866 INFO  - Sensor CPD Sensor (done) | time=36ms
22:50:41.869 INFO  - No quality gate is configured.
22:50:41.906 INFO  - Compare to previous analysis (2023-04-03)
22:50:41.913 INFO  - Compare over 30 days (2023-03-05, analysis of Mon Apr 03 20:39:05 CST 2023)
22:50:41.915 INFO  - Compare to previous version (2023-04-03)
22:50:42.163 INFO  - Execute decorators...
22:50:42.688 INFO  - Store results in database
22:50:43.046 INFO  - Analysis reports generated in 32ms, dir size=1 KB
22:50:43.080 INFO  - Analysis reports compressed in 34ms, zip size=4 KB
22:50:43.157 INFO  - Analysis reports sent to server in 77ms
22:50:43.158 INFO  - ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/springexample
22:50:43.158 INFO  - Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report.
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
Total time: 7.840s
Final Memory: 20M/609M
INFO: ------------------------------------------------------------------------

    我们可以刷新http://localhost:9000首页。

windows下sonarqube+sonar-runner安装记录_第3张图片

     扫描结果出现在了右侧,我们点击链接,进入bugs概述页面:

windows下sonarqube+sonar-runner安装记录_第4张图片

    我们应该要关注这里的issues,这里会根据一些规则,统计我们需要修改的bug,点击issues链接,进入bugs详情页面:

windows下sonarqube+sonar-runner安装记录_第5张图片

    这里还可以看到它给出的一些修改建议,如下所示,它要求我们永远不要抛出泛型异常,自定义或者抛出非RuntimeException类异常:

windows下sonarqube+sonar-runner安装记录_第6张图片

你可能感兴趣的:(java,sonarqube,sonar-runner,findbugs,sonar-scan,code-scan)