近期比較關注代碼的檢測,以前因爲用的findbugs,所以沒有可視化的界面
因此便搜索了一些代碼檢測管理平臺,因而便搜索到了這一款平臺,接下來就開始搭建吧css
軟件下載
登錄網址 https://www.sonarqube.org/downloads/
直接下載最新的社區版便可java
配置數據源
打開配置文件:..\sonarqube-7.4\conf\sonar.properties
增長配置:如下是mysql的配置mysql
#----- DEPRECATED #----- MySQL >=5.6 && <8.0 # Support of MySQL is dropped in Data Center Editions and deprecated in all other editions # Only InnoDB storage engine is supported (not msyISAM). # Only the bundled driver is supported. It can not be changed. #sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false sonar.jdbc.username=root sonar.jdbc.password=root sonar.sorceEncoding=UTF-8 sonar.login=admin sonar.password=admin
原先配置的數據源是Oracle,啓動的時候提示鏈接oracle的jar包不存在,將鏈接oracle的jar放到路徑
..\sonarqube-7.4\extensions\jdbc-driver\oracle\ojdbc14-10.2.0.1.0.jarweb
若是啓動的時候 提示「另外一程序正在使用此文件」
因爲以前啓動的sonar進程未關閉,有衝突;打開資源管理器(ctrl+shift+esc),殺掉java進程從新啓動,問題解決sql
再次啓動的時候,啓動過會兒窗口會自動關閉,那是由於報錯了,打開log文件..\sonarqube-7.4\logs\sonar.log,發現提示「遠程主機強迫關閉了一個現有的鏈接」,以下所示:windows
而後打開web.log,發現提示以下:ruby
這是由於鏈接的oracle的jar的版本太低服務器
還有官方要求oracle的版本:oracle
#----- Oracle 11g/12c # The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/. # Only the thin client is supported, and only the versions 11.2.x or 12.2.x must be used. See # https://jira.sonarsource.com/browse/SONAR-9758 for more details. # If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000 #sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE
想一想算了,不想去找jar包了因而便升級mysql版本到mysql5.7,而後mysql的鏈接上面已經提供了
配置好從新啓動,因爲第一次須要建立表,因此可能有點慢maven
登錄系統後按照以下步驟下載安裝 中文漢化包
安裝好插件重啓sonar,登陸後以下(已經建立了一個項目,首次登陸後界面有些許差別)
接下來建立第一個項目
將
mvn sonar:sonar \ -Dsonar.host.url=http://localhost:9000 \ -Dsonar.login=0006282044b5a5098d23d999c93f9c11eef16025
複製到maven項目的根目錄下啓動檢查便可
檢查後能夠在SonarQube平臺裏看到一些代碼的問題,以下:
SonarLint 是一個插件,能夠集成到開發工具裏,有如下功能
因爲鵝主只使用IDEA,接下來就說明下IDEA如何集成
<profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url> jdbc:mysql://127.0.0.1:3306/sonarqube </sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> <sonar.jdbc.username>root</sonar.jdbc.username> <sonar.jdbc.password>root</sonar.jdbc.password> <sonar.host.url>http://127.0.0.1:9000</sonar.host.url> <!-- your_sonar_host是你的服務器地址,若是你的服務在本機則使用localhost --> </properties> </profile>
安裝插件sonarLint
配置SonarQube servers
若是勾選 Automatically tigger analysis ,將會自動對打開的文件進行分析
mvn clean install
mvn sonar:sonar
至此整個過程講解結束,若是有疑問或者指點的話歡迎留言(`・ω・´)