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目录。 

   

    为了后面在命令行下执行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,双击运行:

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

    可以通过浏览器打开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首页。

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

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

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

<think>嗯,用户想了解如何配置和使用SonarQubeSonar-Scanner进行代码质量检查。首先,我需要回忆一下这两个工具的基本概念和它们之间的关系。根据引用内容,SonarQube是一个代码质量管理平台,而Sonar-Scanner是它的扫描器,用来分析代码并将结果传给SonarQube处理。用户可能已经知道这些,但可能需要具体的步骤指导。 接下来,我应该分步骤说明如何搭建环境和配置。首先,用户需要安装SonarQube服务器,这可能涉及到下载、安装、启动服务,还有中文设置的问题。引用里提到了SonarQube的下载和中文设置,所以这部分需要详细说明,比如修改配置文件中的语言设置。 然后是Sonar-Scanner的安装和配置。用户可能需要在不同的操作系统上操作,但问题里提到的是Windows,所以需要说明下载Windows版本的Scanner,解压后配置环境变量,以及如何编辑配置文件连接到SonarQube服务器。这里可能需要指定conf文件中的服务器地址和项目信息。 接下来是具体的使用步骤,比如在项目目录下创建sonar-project.properties文件,配置项目参数,然后运行扫描命令。用户可能需要知道如何编写这个配置文件,包括项目标识、名称、版本、源代码目录等。同时,结合引用中的信息,Sonar-Scanner执行后,结果会上传到SonarQube进行分析,用户需要登录Web界面查看结果。 可能还需要提到一些常见问题,比如端口冲突,SonarQube默认使用9000端口,如果被占用需要修改配置。另外,SonarQube的插件管理,比如安装中文包或其他语言支持,但用户可能已经通过引用中的中文设置部分解决了这个问题。 用户的问题可能还涉及到如何集成到持续集成流程中,比如与Jenkins或Maven的配合,但根据引用内容,可能没有这方面的信息,所以暂时不深入,或者提示用户可以进一步探索。 最后,生成相关问题的时候,需要围绕安装、配置、使用、优化和集成等方面提问,帮助用户深入理解。例如,如何解决端口冲突,如何分析多语言项目,或者如何进行自定义规则配置。 需要注意的是,回答的结构要清晰,分步骤说明,每个步骤给出具体的操作命令或配置示例,并引用提供的参考资料。同时,确保使用正确的中文术语,避免技术术语错误,比如“环境变量”配置,“配置文件”编辑等。</think>### SonarQubeSonar-Scanner 集成配置步骤 #### 一、环境准备 1. **安装 Java 环境** SonarQube 依赖 Java 11+ 运行环境,需提前安装并配置 `JAVA_HOME` 环境变量[^1]。 2. **下载与安装 SonarQube** - 从官网下载 SonarQube 社区版(例如 `sonarqube-9.9.x.zip`)并解压到本地目录。 - 启动服务: ```bash # Windows 使用 bin\windows-x86-64\StartSonar.bat # Linux/macOS 使用 bin/[OS]/sonar.sh start ``` - 访问 `http://localhost:9000` 验证服务是否运行。 3. **中文配置(可选)** 在 `conf/sonar.properties` 中添加: ```properties sonar.language=zh ``` #### 二、Sonar-Scanner 配置 1. **下载与安装** - 从官网下载对应操作系统的 Sonar-Scanner 并解压。 - 配置环境变量:将 `sonar-scanner/bin` 目录添加到系统 `PATH` 中[^2]。 2. **编辑配置文件** 修改 `conf/sonar-scanner.properties`: ```properties sonar.host.url=http://localhost:9000 sonar.login=your_project_token # 在 SonarQube 界面生成 ``` #### 三、项目集成示例 1. **创建项目配置文件** 在代码项目根目录新建 `sonar-project.properties`: ```properties sonar.projectKey=my_project sonar.projectName=My Project sonar.projectVersion=1.0 sonar.sources=src sonar.sourceEncoding=UTF-8 ``` 2. **执行代码扫描** 在项目目录下运行: ```bash sonar-scanner ``` 扫描完成后,结果会自动上传至 SonarQube 平台[^2]。 #### 四、结果查看与优化 1. **访问 SonarQube 控制台** 登录 `http://localhost:9000`,查看项目的代码质量报告(如漏洞、代码重复率、测试覆盖率等)。 2. **自定义规则(可选)** 在 SonarQube 的 **Rules** 菜单中,启用或禁用特定编程语言的检查规则。 --- ### 注意事项 1. **端口冲突处理** 若端口 9000 被占用,需修改 `conf/sonar.properties` 中的 `sonar.web.port` 参数并重启服务。 2. **多语言支持** SonarQube 默认支持 Java、Python 等语言,需安装对应插件以扩展分析能力。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值