多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
![](https://img.kancloud.cn/5b/4f/5b4f21fa155a5e444e701836a260a249_912x95.jpg) [TOC] # 1. SonarQube上配置 **1. 关闭审查结果上传到SCM功能** ![](https://img.kancloud.cn/41/8b/418bc94b8b4e5512f10643c81236dad6_1543x791.jpg) **2. 添加项目** ![](https://img.kancloud.cn/25/38/2538f121dab6c29df76a0dc59fa89c63_1765x385.jpg) ![](https://img.kancloud.cn/3e/2b/3e2bbf3a63d331c9fa174b0f8a177342_1542x351.jpg) ![](https://img.kancloud.cn/0c/fe/0cfe5139ab1b4dbcfb37139f16db58e7_1511x349.jpg) ![](https://img.kancloud.cn/f2/fd/f2fde62d247977894c2330125845e4af_1399x364.jpg) ``` daae43d6a75f1367245e7d5acc79cb97f3c60bd8 ``` ![](https://img.kancloud.cn/fe/c8/fec8db902dcfa21f3b1c8a524a441709_1737x422.jpg) ``` mvn sonar:sonar \ -Dsonar.projectKey=web-demo-pipeline-sonarqube \ -Dsonar.host.url=http://192.168.1.25:9000/sonarqube \ -Dsonar.login=daae43d6a75f1367245e7d5acc79cb97f3c60bd8 ``` <br/> # 2. Jenkins上配置 **1. 安装插件 SonarQube Scanner** **2. 添加SonarQube凭证** ![](https://img.kancloud.cn/a1/49/a149b96df9684948926a9481fd2af815_1511x663.jpg) >[warning]提醒:不是一个sonarqube项目就需要在jenkins中添加一个sonarqube凭证,首次添加这个凭证后,后面在sonarqube创建的所有项目都可以共享这个凭证与jenkins交互。 **3. 配置SonarQube servers** Manage Jenkins(系统管理) -> Configure System(系统配置) -> SonarQube servers。 ![](https://img.kancloud.cn/7c/f8/7cf8d7f5392f7dad0d0f087430a247a0_1424x546.jpg) **4. 配置SonarQube Scanner** Manage Jenkins(系统管理) -> Global Tool Configuration(全局工具配置) -> SonarQube Scanner。 ![](https://img.kancloud.cn/31/30/313090b111d92212afb80198227af393_1469x581.jpg) <br/> # 3. 项目上配置 在项目添加SonarQube代码审查。 <br/> ## 3.1 非流水线项目 如果是非流水线项目按照本章步骤在项目中添加sonarqube审查代码。 **1. 在项目根目录下添加文件`sonar-project.properties`** ![](https://img.kancloud.cn/9f/5c/9f5c95a6cc6af9735fe9e55f0052d0eb_1387x440.jpg) >[warning]1. 文件名必须是`sonar-project.properties` 。 > 2. 必须放在项目根目录下,不能放在resources目录下。 ```properties # must be unique in a given SonarQube instance sonar.projectKey=web-demo-maven-sonarqube # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=web-demo-maven-sonarqube sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # This property is optional if sonar.modules is set. sonar.sources=. sonar.exclusions=**/test/**,**/target/** sonar.java.source=1.8 sonar.java.target=1.8 # Encoding of the source code. Default is default system encoding sonar.sourceEncoding=UTF-8 ``` **2. 记得把修改提交到gitlab上** <br/> ## 3.2 流水线项目 如果是流水线项目按照本章步骤在项目中添加sonarqube审查代码。 **1. 项目根目录下添加文件`sonar-project.properties`** ![](https://img.kancloud.cn/c6/a9/c6a92a3361c2e7b3aefe7e8229553dcd_1224x325.jpg) >[warning]1. 文件名必须是`sonar-project.properties` 。 > 2. 必须放在项目根目录下,不能放在resources目录下。 ```properties # must be unique in a given SonarQube instance sonar.projectKey=web-demo-pipeline-sonarqube # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=web-demo-pipeline-sonarqube sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # This property is optional if sonar.modules is set. sonar.sources=. sonar.exclusions=**/test/**,**/target/** sonar.java.source=1.8 sonar.java.target=1.8 # Encoding of the source code. Default is default system encoding sonar.sourceEncoding=UTF-8 ``` **2. 在`Jenkinsfile`文件中加入SonarQube代码审查阶段** ```groovy pipeline { agent any stages { stage('拉取代码') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']] , doGenerateSubmoduleConfigurations: false , extensions: [], submoduleCfg: [] , userRemoteConfigs: [[credentialsId: 'e4e02eb6-f6bb-4040-b842-c1423c397493' , url: 'git@gitlab.master.com:itheima_group/web-demo-pipeline-sonarqube.git']]]) } } stage('编译构建') { steps { sh label: '', script: 'mvn clean package' } } stage('SonarQube代码审查') { steps{ script { scannerHome = tool 'sonarqube-scanner' } withSonarQubeEnv('sonarqube-8.9.6.50800') { sh "${scannerHome}/bin/sonar-scanner" } } } stage('项目部署') { steps { deploy adapters: [tomcat9(credentialsId: '748ce750-b1af-4730-9e43-1b6b4905a8dc', path: '', url: 'http://tomcat.master.com:8080')], contextPath: null, war: 'target/*.war' } } } post { always { emailext body: '${FILE,path="src/main/resources/email.html"}', subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS} !', to: 'xxxb@163.com,xxxx10@qq.com' } } } ``` **3. 记得把修改提交到gitlab上** <br/> # 4. Jenkins上构建并到SonarQube查看审查结果 在Jenkins上构建后到SonarQube查看审查结果。 ![](https://img.kancloud.cn/e3/8e/e38e80343c6482f247d7ef311ff379e3_1805x612.jpg)