- SonarQube:
- 基础配置:
- 安装部署:可通过官方下载包部署在服务器上,支持多种操作系统。例如在Linux环境下,解压安装包并按照官方文档配置相关参数启动服务。
- 项目接入:在微服务项目的构建脚本(如Maven或Gradle)中添加SonarQube插件配置。以Maven为例,在
pom.xml
文件中添加插件配置,指定SonarQube服务器地址、项目密钥等信息。例如:
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.9.1.2184</version>
<configuration>
<sonar.host.url>http://sonarqube-server:9000</sonar.host.url>
<sonar.login>your-token</sonar.login>
</configuration>
</plugin>
</plugins>
</build>
- **扫描规则配置**:登录SonarQube管理界面,根据项目技术栈(如Java、Python等)选择合适的质量配置文件,也可自定义规则集,例如针对特定代码规范或安全漏洞规则。
- Checkstyle:
- 基础配置:
- 引入依赖:如果是Java项目使用Maven构建,在
pom.xml
文件中添加Checkstyle依赖,如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<configLocation>config/checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- **配置规则文件**:创建`checkstyle.xml`规则文件,可基于Google Java Code Style等通用规则模板进行修改,定义代码格式、命名规范等规则。例如规范类名必须大写字母开头等规则。该文件通常放在项目的`src/main/resources`目录下或自定义配置路径。