日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maven结合SonarQube的使用笔记

發布時間:2025/3/21 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maven结合SonarQube的使用笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

前提: 1,SonarQube已經安裝好且已經安裝了sonar-php-plugin,并且在測試服務器上也安裝并配置好了sonar-scanner 2,安裝了Maven 這個非常簡單,直接從官網上下載Maven的zip包,解開然后配置下面這個配置文件即可

export MAVEN_HOME=/usr/local/maven333 export PATH=$MAVEN_HOME/bin:$PATH

一,Maven簡單項目

配置pom.xml,使之產生Junit單元測試報告

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19.1</version><configuration><skip>false</skip><!--<reportsDirectory>${project.basedir}/target/junit</reportsDirectory> --></configuration></plugin></plugins></build>

默認就是產生在target目錄下面的surefire-reports目錄里,所以不需要配置reportsDirectory。

配置pom.xml ,使之產生代碼測試覆蓋率報告,在這里使用Jacoco工具來產生。

<build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.7.2.201409121644</version><executions><execution><id>default-prepare-agent</id><goals><goal>prepare-agent</goal></goals></execution><execution><id>default-report</id><phase>prepare-package</phase><goals><goal>report</goal></goals></execution><execution><id>default-check</id><goals><goal>check</goal></goals><configuration><rules><!-- implementation is needed only for Maven 2 --><rule implementation ="org.jacoco.maven.RuleConfiguration" ><element>BUNDLE</element><limits><!-- implementation is needed only for Maven 2 --><limit implementation ="org.jacoco.report.check.Limit" ><counter>COMPLEXITY</counter><value>COVEREDRATIO</value><minimum>0.60</minimum></limit></limits></rule></rules></configuration></execution></executions></plugin></plugins></build>

默認就產生在target目錄下,覆蓋率報告文件名為jacoco.exec

在項目的根目錄下創建一個配置文件sonar-project.properties

sonar.projectKey=org.sonarqube:parent sonar.projectName=Java :: JaCoco Multi Modules :: Parent sonar.projectVersion=1.0-SNAPSHOTsonar.sources=src/main/java sonar.tests=src/test/java sonar.binaries=target/classessonar.language=java#Tells SonarQube where the unit tests execution reports are sonar.junit.reportsPath=target/surefire-reports#Tells SonarQube where the integration tests code coverage report is #sonar.jacoco.itReportPath=target/jacoco-it.exec sonar.jacoco.reportPath=target/jacoco.exec# Encoding of the source files sonar.sourceEncoding=UTF-8

注意,一定要配置這個sonar.binaries,否則會報: INFO: No JaCoCo analysis of project coverage can be done since there is no class files.

另外,配置sonar.jacoco.itReportPath或sonar.jacoco.reportPath視產生的覆蓋率報告文件而定

二,Maven多模塊項目

配置pom.xml,使之產生Junit單元測試報告,同“Maven簡單項目 ”的配置方法。

配置pom.xml ,使之產生代碼測試覆蓋率報告 ,同“Maven簡單項目 ”的配置方法。

在項目的根目錄下創建一個配置文件sonar-project.properties

sonar.projectKey=com.dangdang:elastic-job sonar.projectName=elastic-job sonar.projectVersion=1.1.0# Set modules IDs sonar.modules=elastic-job-api-core,elastic-job-console,elastic-job-core,elastic-job-spring# Modules inherit properties set at parent level sonar.sources=src/main/java sonar.tests=src/test/java sonar.java.binaries=target/classessonar.language=java#Tells SonarQube where the unit tests execution reports are sonar.junit.reportsPath=target/surefire-reports#Tells SonarQube where the integration tests code coverage report is sonar.jacoco.reportPath=target/jacoco.exec# Encoding of the source files sonar.sourceEncoding=UTF-8

elastic-job-api-core.sonar.projectBaseDir=elastic-job-api/elastic-job-api-core

這里,需要注意的就是配置sonar.modules,這樣下面的sonar.sources,sonar.tests等配置項就是指各個模塊下面的相對目錄。

指定代碼的目錄的目的是為了把代碼文件上傳到Sonar服務器上。

模塊必須是非父模塊,也就是不再包含子模塊了,只有這樣,統計代碼質量才有意義。

在這里,elastic-job-api-core這個子項目的目錄不是在當前目錄下面,而是又深入了一級目錄,就可以使用elastic-job-api-core.sonar.projectBaseDir來指定這個完整的相對路徑。

三,測試步驟都一樣:

1,在頂級目錄下執行

mvn clean test

這時候就會在頂級目錄下得到一個文件夾reports,里面有兩個測試報文文件,分別是測試報告和測試覆蓋率報告

2,在頂級目錄下執行以下命令,就會將兩個測試報告文件和src和tests目錄下的文件都提交給SonarQube服務器(前提是Sonar已經都配置好了) (D:\sonar-scanner-2.6.1\bin\sonar-scanner.bat這個路徑或者在linux就放入PATH環境變量中)

sonar-scanner

轉載于:https://my.oschina.net/xuhh/blog/725969

總結

以上是生活随笔為你收集整理的Maven结合SonarQube的使用笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。