sonar环境搭建

Posted jackyu126

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sonar环境搭建相关的知识,希望对你有一定的参考价值。

本文主要在Linux环境操作,Centos7.2

1.java 1.8.0安装及环境变量配置

安装及配置参考:

http://jingyan.baidu.com/article/d5c4b52bebcb64da570dc571.html

2.mysql:5.6.36安装及配置

数据库安装采用二进制安装mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz,参考如下

http://jingyan.baidu.com/article/a378c9609eb652b3282830fd.html

1、卸载mariadb

yum list installed | grep mariadb    #检查mariadb是否已安装

yum -y remove mariadb*    #全部卸载

修改配置文件/etc/my.cnf
default-storage-engine=INNODB
innodb_buffer_pool_size=256M
innodb_log_file_size=256M
max_allowed_packet=100M
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

本地 Mysql 创建数据库

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
本地 Mysql 创建用户并分配权限
CREATE USER \'sonar\' IDENTIFIED BY \'sonar\';
GRANT ALL PRIVILEGES ON *.* TO \'sonar\'@\'%\' IDENTIFIED BY \'sonar\' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO \'sonar\'@\'localhost\' IDENTIFIED BY \'sonar\' WITH GRANT OPTION;
FLUSH PRIVILEGES;

 

需要给用户添加权限:

登录mysql,在mysql下输入如下命令:

GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'%\' IDENTIFIED BY \'123456\' WITH GRANT OPTION;  //允许用户root使用密码123456从任何主机连接到MYSQL服务器

如果需要制定主机,则可以用下面的命令:

GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'10.1.1.123\' IDENTIFIED BY \'123456\' WITH GRANT OPTION;  //允许用户root从IP为10.1.1.123的主机连接到MYSQL服务器,并使用123456作为登录密码

3.sonar版本:6.5

下载地址:https://www.sonarqube.org/downloads/

unzip sonarqube-6.5.zip

修改配置文件/etc/profile

export SONAR_HOME=/usr/local/sonarqube-6.5

export PATH=/jboss/apache-jmeter-2.10/bin/:$JAVA_HOME/bin://usr/local/mysql5.6.36/bin:$SONAR_HOME/bin/linux-x86-64:$MAVEN_HOME/bin:$SONAR_SCANNER_HOME/bin:/u01/ant1.9.9/bin:$PATH

修改完成后,source /etc/profile
修改conf下配置文件

3.1、修改 sonar.properties

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
3.2 启动服务

/usr/local/sonarqube-6.5/bin/linux-x86-64/目录下

./sonar.sh start

查看是否启动:netstat -pantu |grep 9000

http://localhost:9000登陆页面,默认登陆admin,密码:admin

4. 下载sonar-scanner3.0

 添加环境变量:

 export SONAR_SCANNER_HOME=/usr/local/sonar-scanner-3.0

export PATH=/jboss/apache-jmeter-2.10/bin/:$JAVA_HOME/bin://usr/local/mysql5.6.36/bin:$SONAR_HOME/bin/linux-x86-64:$MAVEN_HOME/bin:$SONAR_SCANNER_HOME/bin:/u01/ant1.9.9/bin:$PATH

5.代码管理:svn,目前把需要扫描的下载到/u01/svn/目录下

http://192.168.120.10:4024/svn/ygyr

svn checkout http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 svn checkout svn://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 # 例子: svn co svn://localhost/测试工具 /home/testtools --username wzhnsc svn co http://localhost/test/testapp --username wzhnsc svn checkout svn://localhost/测试工具 /home/testtools --username wzhnsc svn checkouthttp://localhost/test/testapp --username wzhnsc
5.1.在src目录下增加sonar-project.properites

# must be unique in a given SonarQube instance
sonar.projectKey=pcWeb
# this is the name displayed in the SonarQube UI
sonar.projectName=pcWeb
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=.
sonar.java.binaries=.
sonar.language=java
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
# Additional Parameters
sonar.my.propety=value
sonar.core.codeCoveragePlugin=jacoco
sonar.jacoco.reportPath=/u01/report/workspace/sonar/target
sonar.jcoco.itReportPath=/u01/report/workspace/sonar/target
sonar.dynamicAnalysis=reuseReports


6. 执行sonar-scanner

在执行时报错:

13:50:42.385 ERROR: Error during SonarQube Scanner execution
java.lang.IllegalStateException: Unable to load component class org.sonar.scanner.scan.ProjectLock
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:71)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:678)
at org.sonar.core.platform.ComponentContainer.getComponentByType(ComponentContainer.java:283)
at org.sonar.scanner.scan.ProjectScanContainer.doBeforeStart(ProjectScanContainer.java:112)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:129)
at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:47)
at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:84)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:144)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:129)
at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:119)
at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:116)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:76)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy0.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:171)
at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:128)
at org.sonarsource.scanner.cli.Main.execute(Main.java:111)
at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.lang.IllegalStateException: Unable to load component class org.sonar.scanner.scan.DefaultInputModuleHierarchy
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:71)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:632)
at org.picocontainer.parameters.BasicComponentParameter$1.resolveInstance(BasicComponentParameter.java:118)
at org.picocontainer.parameters.ComponentParameter$1.resolveInstance(ComponentParameter.java:136)
at org.picocontainer.injectors.SingleMemberInjector.getParameter(SingleMemberInjector.java:78)
at org.picocontainer.injectors.ConstructorInjector$CtorAndAdapters.getParameterArguments(ConstructorInjector.java:309)
at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:335)
at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:69)
... 24 more
Caused by: java.lang.IllegalStateException: Unable to load component class org.sonar.api.batch.bootstrap.ProjectReactor
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:71)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:632)
at org.picocontainer.parameters.BasicComponentParameter$1.resolveInstance(BasicComponentParameter.java:118)
at org.picocontainer.parameters.ComponentParameter$1.resolveInstance(ComponentParameter.java:136)
at org.picocontainer.injectors.SingleMemberInjector.getParameter(SingleMemberInjector.java:78)
at org.picocontainer.injectors.SingleMemberInjector.getMemberArguments(SingleMemberInjector.java:61)
at org.picocontainer.injectors.MethodInjector.getMemberArguments(MethodInjector.java:100)
at org.picocontainer.injectors.MethodInjector$2.run(MethodInjector.java:112)
at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
at org.picocontainer.injectors.MethodInjector.decorateComponentInstance(MethodInjector.java:120)
at org.picocontainer.injectors.CompositeInjector.decorateComponentInstance(CompositeInjector.java:58)
at org.picocontainer.injectors.Reinjector.reinject(Reinjector.java:142)
at org.picocontainer.injectors.ProviderAdapter.getComponentInstance(ProviderAdapter.java:96)
at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:69)
... 38 more
Caused by: You must define the following mandatory properties for \'Unknown\': sonar.projectKey, sonar.sources

7. 进入http://localhost:9000页面

 

以上是关于sonar环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境

Mac-搭建Sonar系统

Mac-搭建Sonar系统

测试技术MAVEN集成测试环境搭建

白盒测试之静态代码扫描:SonarQube+Scanner环境搭建及使用

白盒测试之静态代码扫描:SonarQube+Scanner环境搭建及使用