准备环境
jdk1.8
mysql5.7
maven 3.3.9
下载sonar相关安装包
服务端:
sonarqube:http://www.sonarqube.org/downloads/
客户端工具:
sonar-runner:http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.3/sonar-runner-dist-2.4.zip
汉化:
中文汉化包:https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/tag/sonar-l10n-zh-plugin-1.27,下载之后将插件放到sonarqube的extensions/plugins目录下,重启sonar即可。
将sonarqube、sonar-runner包解压到/usr/local/下
>>unzip sonar-runner-dist-2.4.zip
>>unzip sonarqube-7.7.zip
安装mysql:
创建sonar数据库
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' ;
mysql> CREATE USER 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' ;
mysql> FLUSH PRIVILEGES;
修改配置文件
>>vim /usr/local/sonarqube-7.7/conf/sonar.properties
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=snoar
sonar.jdbc.password=snoar
sonar.web.host=ip地址 #docker的IP
sonar.web.port=9000
>>vim /usr/local/sonarqube-7.7/conf/wrapper.conf
wrapper.java.command=/usr/bin/java ##把wrapper.java.command修改成本机的jdk1.8路径
>>vim sonar-runner-2.4/conf/sonar-runner.properties
sonar.host.url=http://ip:9000
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
配置环境变量
>>vim ~/.zshrc
export SONAR_HOME=/usr/local/sonarqube-7.7
export SONAR_RUNNER_HOME=/usr/local/sonar-runner-2.4
export PATH=$PATH:$SONAR_RUNNER_HOME/bin
启动SonarQube
SonarQube不能以root用户启动,所以必须先创建一个非root用户
>>useradd sonar
>>chown -R sonar:sonar sonarqube-7.7/
启动:
>>su sonar
>>./usr/local/sonarqube-6.7.4/bin/linux-x86-64/sonar.sh start
关闭:
>>./usr/local/sonarqube-6.7.4/bin/linux-x86-64/sonar.sh stop
查看启动日志:
>>tail -f /usr/local/sonarqube-7.7/logs/sonar.log
登录http://ip:9000
Maven项目执行sonar扫描
如下,在maven的 setting.xml中加入SonarQube连接的配置
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://localhost:9000</sonar.host.url>
<sonar.jdbc.url>
jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
</properties>
</profile>
<profiles>
在需要扫描的目标项目的pom.xml中引入sonar-maven-plugin 插件
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.6.0.1398</version>
</plugin>
在项目根目录下执行如下命令,成功之后即可生成sonar报告,可以去 web页面查看分析结果报告:
>> mvn install sonar:sonar -DskipTests
或者 在mvn命令中指定sonar-maven-plugin:
>> mvn clean install org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar
Gradle项目执行sonar扫描
在项目的根目录下build.gradle里加入以下配置
buildscript {
repositories {
mavenCentral()
//或者 maven {url "https://plugins.gradle.org/m2/"}
}
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7.1" //插件后面的版本号可配
}
}
apply plugin: "org.sonarqube"
sonarqube{
properties{
property "sonar.host.url", "http://ip:9000" //sonarqube 服务器地址
property "sonar.sourceEncoding", "UTF-8"
property "sonar.sources", "src"
}
}
配置完成之后,在根目录下执行:
>>gradle sonarqube --info
跳过测试 加 -x testgradle sonarqube -xtest
在docker上搭建Jenkins(已安装jdk8)
安装tomcat
1)mkdir -p /usr/local/tomcat8
2)cd /usr/local/tomcat8
3)wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.41/bin/apache-tomcat-8.5.41.tar.gz ##tomcat压缩包版本的版本可按需选择
4)tar -zvxf apache-tomcat-8.5.38.tar.gz -C .
5)cd apache-tomcat-8.5.38/bin
6)./startup.sh #启动tomcat 或 ./shutdown.sh 停止tomcat
7)ps aux | grep tomcat ##查看tomcat是否启动成功
安装Jenkins到webapps目录下
>> wgethttp://mirrors.jenkins-ci.org/war/latest/jenkins.war -P /usr/local/tomcat8/apache-tomcat-8.5.41/webapps
访问Jenkins主页http://ip:8080/jenkins按说明初始化jenkins即可
1、关闭Jenkins
2、重启Jenkins
http://ip:8080/jenkins/restart
3、重新加载配置信息
网友评论