sonarqubSonar簡介
Sonar是一個用於代碼質量管理的開源平臺,用於管理源代碼的質量,可以從七個維度檢測代碼質量
這裏省略!
Sonarqube搭建
預置條件
1).已安裝JAVA環境
2).已安裝有MySQL數據庫
1、數據庫配置:
# 創建數據庫sonar
create database sonar character set utf8 collate utf8_general_ci;
# 創建數據庫用戶sonar密碼sonar
create user 'sonar' identified by 'sonar';
# 賦權給用戶sonar對數據庫sonar有所有權限
grant all on sonar.* to 'sonar'@'%' identified by 'sonar';
# 授權sonar用戶可以在本地連接數據庫
grant all on sonar.* to 'sonar'@'localhost' identified by 'sonar';
# 刷新權限
flush privileges;
2、修改sonar配置文件:sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://ip:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL
修改wrapper配置文件指定jdk的路徑;
3、啓動sonar;
在執行 ./ sonar.sh start 後,發現9000端口並未打開,執行 sh sonar.sh status 顯示sinar 並未啓動。
給 sonarqube 目錄的擁有組改成非root用戶,比如我的另一個賬戶 sonar;
sudo chown -R sonar sonarqube-xxx;
如果發現沒有權限,也可以加一個 sudo chmod -R 777 sonarqube-xxx;
如果發現sudo無法使用;
解決方法
一、 切換到root用戶
終端中執行 su 命令,然後輸入密碼,從普通用戶切換爲根用戶
二、爲sudoers配置文件添加寫權限
sudoers文件位於 /etc 目錄下,其爲系統配置sudo用戶的一個只讀配置文件。在root身份下執行 chmod +w /etc/sudoers 命令爲該文件添加寫權限。
三、編輯sudoers配置文件
執行 vi /etc/sudoers 命令對配置文件進行編輯。在文件中先找到
Allows people in group wheel to run all commands
這段配置,然後在配置下方新起一行添加所需要增加 sudo權限的用戶,格式如下
用戶名 ALL=(ALL) ALL
四、退出保存
編輯完配置文件保存退出後,使用 su 普通用戶名 切換回普通用戶重新執行sudo命令成功;
執行./ sonar.sh start啓動sonarqube;
sonar-maven
pom文件中添加Pungins;
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
在無網的環境下,儘量寫上版本號,
maven在build後會自動去Downloading 這個maven-metadata.xml文件,這個文件可以看作版本信息,作爲一個版本比對。
但maven服務器在掛了或者沒有網,會一直卡在DOWNLOADING和retry。
解決方案:
找到本地的maven配置文件settings.xml,打開後找到下面:
<repository>
<id>snapshots</id>
<name>Snapshots</name>
<url>url</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
找到xml中的updatePolicy標籤,改爲never即可。
<repository>
<id>snapshots</id>
<name>Snapshots</name>
<url>url</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
改完後再去build就不會去download maven-metadata.xml文件了,根據自己需求改。這個屬性爲更新策略,aways:每次,never:從不,daily:每日。
修改maven的setting.xml文件如下;根據實際情況更改!
<id>UFindNexus</id> <repositories> <repository> <id>UFindNexus</id> <url>http://ip:port/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> </repositories> <!--插件是單獨解析的。您也可以爲這些庫配置存儲庫,如果需要,可以使用不同的更新策略。--> <pluginRepositories> <pluginRepository> <id>UFindNexus</id> <url>http://ip:port/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> <checksumPolicy>warn</checksumPolicy> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <checksumPolicy>fail</checksumPolicy> <updatePolicy>daily</updatePolicy> </snapshots> </pluginRepository> </pluginRepositories> </profile> <!--sonar maven --> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url> <sonar.jdbc.username>sonar</sonar.jdbc.username> <sonar.jdbc.password>sonar</sonar.jdbc.password> <sonar.host.url>http://ip:port2</sonar.host.url> </properties>
</profile> </profiles>
<activeProfiles> <activeProfile>UFindNexus</activeProfile> <activeProfile>sonar</activeProfile> </activeProfiles> |
運行命令mvn sonar:sonar;等待執行;(在無網的環境下先在有網環境下將相關JAR包下載好放入無網的本地或者服務器私服.);
如果報如下錯誤:No Plungin found for prefix 'sonar' ....
那麼需要在pom <build>中添加
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
</plugins>
</pluginManagement>
同樣報錯誤說明私服沒有此jar包,準備好這個plugin放入私服中.
執行mvn sonar:sonar;等待完成;(在IDEA中可以edit configuration處添加Maven->sonar:sonar -e,但是需要IDEA的maven配置項的setting.xml是配置正確的).
如果報
was cached in the local repository, resolution will not be reattempted until the update interval of UFindNexus has elapsed or updates are forced
已緩存在本地存儲庫中,在UFindnexus的更新間隔已過或強制更新之前,不會重新嘗試解決方案。
說明未下載成功,將下關jar上傳至私服,然後刪除相關的本地後綴爲UPDATE的文件即可。