sonar+jacoco+jenkins

2017-01-04 15:15:20

1. sonar安装流程

最新版本sonar需要jdk8,首先需安装jdk8,安装步骤如下:

a、官网下载jdk8的tar.gz安装压缩包,解压 tar x -C /opt -f jdk-8u66-linux-x64.tar.gz
b、安装jdk

update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_111/bin/java 100
update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_111/bin/java 100

c、配置默认的jdk,for both java and javac

# update-alternatives --config java
There is 1 program that provides 'java'.
Selection Command
-----------------------------------------------
*+ 1 /opt/jdk1.8.0_111/bin/java
Enter to keep the current selection[+], or type selection number: 1

# update-alternatives --config javac
There is 1 program that provides 'javac'.
Selection Command
-----------------------------------------------
*+ 1 /opt/jdk1.8.0_111/bin/javac
Enter to keep the current selection[+], or type selection number: 1

d、配置环境变量

export JAVA_HOME=/opt/jdk1.8.0_111/
export JRE_HOME=/opt/jdk1.8.0_111/jre/
export PATH=$PATH:/opt/jdk1.8.0_111/bin/:/opt/jdk1.8.0_111/jre/bin/

e、jdk安装验证

# java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

2. sonar安装部署

a、官网下载sonar最新版本
b、解压 tar x -C /opt -f sonarqube-6.1.zip
c、创建软链接ln -s /opt/sonarqube-6.1/bin/linux-x86-64/sonar.sh /usr/bin/sonar,运行sonar start,默认端口9000,http://ip:9000 验证是否正常
d、创建sonar数据库和用户名,并分配权限,mysql版本5.6+

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;

e、设置sonar配置文件,配置数据库连接,sonar默认是h2内存数据库,用mysql或oracle需配置

# vim /opt/sonarqube-6.1/conf/sonar.properties
sonar.jdbc.username: sonar
sonar.jdbc.password: sonar
sonar.jdbc.url: jdbc:mysql://ip:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
# Optional properties
sonar.jdbc.driverClassName: com.mysql.jdbc.Driver

f、重启sonar,日志中如下异常

2016.10.24 13:16:13 WARN  web[][o.a.c.l.WebappClassLoaderBase] The web application [ROOT] appears to have started a thread named [elasticsearch[Silver Samurai][trans
port_client_timer][T#1]{Hashed wheel timer #1}] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:java.lang.Thread.sleep(Native Method)
org.jboss.netty.util.HashedWheelTimer$Worker.waitForNextTick(HashedWheelTimer.java:445)
org.jboss.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:364)
org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
java.lang.Thread.run(Thread.java:745)

解决方法,增加内存

# vim /opt/sonarqube-6.1/conf/sonar.properties
sonar.web.javaOpts=-Xmx1024m -Xms1024m -XX:+HeapDumpOnOutOfMemoryError

g、安装中文插件
首先admin登录,进入 Administration–>System–>Update Center,搜索Chinese Pack,安装并重启生效
h、安装Sonar-Scanner(客户端),解压后可用

wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
ln -s /opt/sonar-scanner-2.8/bin/sonar-scanner /usr/bin/sonar-scanner

3. 集成maven

a、设置maven的setting配置文件

<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://ip:9000
</sonar.host.url>
</properties>
</profile>

b、配置maven插件

<plugin>
    <groupId>org.sonarsource.scanner.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>3.1.1</version>
</plugin>

c、执行maven命令

clean test sonar:sonar
#单侧覆盖率传入sonar
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent test -Dmaven.test.failure.ignore=true sonar:sonar

4. 集成jenkins

a、jenkins安装略,jenkins安装sonar插件并重启生效
b、设置jenkins的sonar配置–>系统设置SonarQube Server

Name=SonarQube(随意填写)
Server URL=http://ip:9000(sonar平台地址)
Server version=5.3 or higher

系统设置SonarQube Scanner

Name=sonar scanner(随意填写)
SONAR_RUNNER_HOME=/opt/sonar-scanner-2.8/(scanner安装目录)

c、设置job的sonar配置–>Add post-build step–>Execute SonarQube Scanner

#Analysis properties中输入如下内容
sonar.projectKey=testProject(根据实际项目填写)
sonar.projectName=testProject(根据实际项目填写)
sonar.projectVersion=1.0(根据实际项目填写)
sonar.sourceEncoding=UTF-8
sonar.language=java(扫描语言)
sonar.sources=.
sonar.projectBaseDir=.

5. 集成jacoco

a、jenkins安装jacoco插件并重启生效
b、设置job的jacoco配置–>构建后操作–>Record JaCoCo coverage report
c、maven配置jacoco插件

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<configuration>
<append>true</append>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

可通过maven命令测试覆盖率并上传至sonarmvn clean test sonar:sonar

6. 集成LDAP

a、首先admin登录,进入 Administration–>System–>Update Center,搜索LDAP,安装并重启生效
b、设置sonar配置文件,sonar.properties

# LDAP configuration
# General Configuration
sonar.security.realm=LDAP
sonar.security.savePassword=true
ldap.url=ldap://ldap.com
# User Configuration
ldap.user.baseDn=ou=Users,dc=mycompany,dc=com
ldap.user.request=(&(objectClass=inetOrgPerson)(uid={login}))
ldap.user.realNameAttribute=cn
ldap.user.emailAttribute=mail
# Group Configuration
ldap.group.baseDn=ou=Groups,dc=sonarsource,dc=com
ldap.group.request=(&(objectClass=posixGroup)(memberUid={uid}))