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的文件即可。