一、所需安裝及下載軟件
1、JDK1.7 [jdk-7u79-windows-x64.exe],其版本不能小於1.7,因SonarQube需要JDK支持。安裝之後配置環境變量。
2、tomcat7 [apache-tomcat-7.0.26.exe]
3、MySQL5.1 [mysql-essential-5.1.41-win32.msi],數據庫最好在5.0以上。
4、Jenkins [jenkins-1.642.4],官網下載。
5、Maven [apache-maven-3.1.0]。
6、sonarqube-5.4.zip主要軟件
Plugins:
sonar-scanner-2.5.1.zip主要分析工具
sonar-examples-master.zip 測試工程,自行選擇。
sonar-scm-svn-plugin-1.3.jar svn,所下載SonarQube自帶1.2,使用過程中並未涉及,自行 選擇。
sonar-l10n-zh-plugin-1.9.jar SonarQube漢化插件,自行選擇。
二、安裝過程及系統級配置
1、JDK安装好之后配置环境变量
JAVA_HOME:C:\Program Files\Java\jdk1.7.0_79
Path后追加%JAVA_HOME%\bin;
2、tomcat7安装之后配置相关参数,环境变量根据需要进行配置,在此未曾配置。
打开Tomcat7_LABSysw.exe选中’Java’选项卡,在java options最下方粘贴以下配置:
-Xms1048m
-Xmx1048m
-XX:PermSize=512M
-XX:MaxPermSize=512M
-XX:NewSize=512m
删除webapp下的ROOT项目。
conf/tomcat-users.xml:
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui" password="tomcat" name="tomcat"/>
conf/server.xml:
在Host节点下增加
<Context path="" docBase="ProjectName" debug="0" reloadable="true" antiResourceLocking="true"/>
antiResourceLocking=”true”参数必需配置,此参数也可配置在conf/context.xml的context节点处。若不配置此参数,maven远程发布时,二次发布将会发生异常:
Caused by: org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: FAIL - Unable to delete [D:\Tomcat7_LAB\webapps\LABSys3.0]. The continued presence of this file may cause problems.
也就是webapps\LABSys3.0\WEB-INF\lib下的jar文件不能被删除,导致项目不能被完全删除,以致发布不成功。
3、此间所用MySQL为安装版,安装完之后配置环境变量
MYSQL_HOME:C:\Program Files (x86)\MySQL\MySQL Server 5.1
Path后追加%MYSQL_HOME%\bin;
配置环境变量主要是为了方便在cmd下使用其相关命令。
注册其服务,在cmd下运行:mysqld -install
删除服务命令[mysql -remove]
登录admin账户
–sonar创建数据库与用户
show databases;
create database sonar character set utf8 collate utf8_general_ci;
grant all privileges on sonar.* to ‘sonar’@’%’ identified by ‘sonar123’ grant option;
flush privileges;
可参考:http://blog.csdn.net/frank0417/article/details/8496545 (sonarqube的安装与配置)
4、Jenkins安装结束,修改端口(此处修改为8087),自行选择是否修改。然后访问 http://adress:8087
5、所用为免安装版,配置环境变量
M2_HOME:C:\Continuous-Integration\apache-maven-3.1.0
Path后追加%M2_HOME%\bin;
配置conf/setting.xml,具体配置参考当前所所使用maven。
6、将sonarqube-5.4.zip和sonar-scanner-2.5.1.zip分别解压。
配置scanner的环境变量以方便使用
SONAR_RUNNER_HOME:C:\Continuous-Integration\sonar-scanner-2.5.1
Path后追加%SONAR_RUNNER_HOME%\bin;
配置sonar.properties:
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar123
sonar.jdbc.url=jdbc:mysql://address:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformanc
配置sonar-runner.properties:
sonar.host.url=http://address:9000
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar123
sonar.jdbc.url=jdbc:mysql://address:3306/sonar?useUnicode=true&characterEncoding=utf8
以上两个properties原本有相关配置,只是被注释,只需修改
进入C:\Continuous-Integration\sonarqube-5.4\bin\windows-x86-64,根据服务器版本而选择windows-x86-64。运行StartSonar.bat,然后访问http://address:9000
运行InstallNTService.bat将sonarqube安装至服务中
若需汉化将sonar-l10n-zh-plugin-1.9.jar放在sonarqube-5.4\extensions\plugins下然后重启sonarqube
可参考:http://blog.csdn.net/frank0417/article/details/8496545 (sonarqube的安装与配置)
测试sonarqube是否能分析项目,cmd进入
..\sonar-examples-master\projects\languages\java\sonar-runner,
然后运行sonar-runner.bat,然后在http://address:9000查看结果
三、使用方法及工作級参数配置
1.sonarqube
项目设置
在项目根目录下增加sonar-project.properties文件,内容如下:
#----- Default SonarQube server
#sonar.host.url=http://localhost:9000
#sonar.host.url=http://10.122.127.16:9000
# must be unique in a given SonarQube instance
sonar.projectKey=projectKey
# this is the name displayed in the SonarQube UI
sonar.projectName=ProjectName
sonar.projectVersion=1.0
# Comma-separated paths to directories with sources (required)
sonar.sources=src/main/java,src/main/resources/query,src/main/webapp/common,src/main/webapp/WEB-INF/view
#sonar.sources=src/main/java
# 未设置,则按多语言分析
#sonar.language=java
# Encoding of the source files
sonar.sourceEncoding=UTF-8
服务设置
SCM设置
防止发生以下错误
Caused by: org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for '<svn://10.27.213.98:3690> c4dcedc1-3be6-4ef8-8fed-ba624a96dd11'
2、登录jenkins点击’系统管理’-》’系统设置’,配置以下信息
安装插件
在jenkins中新建一个项目
点击ok,然后配置相关参数
SVN
如果Credentials的下拉框中没有可新增一个认证。
触发器
SonarQube
Deploy
Tomcat
以上配置好之后回到项目列表点击构建
如果以上所配置的步骤在执行过程中发生以下错误
构建之后进行分析导致以下错误,故在jenkins中将
系统资源不足,无法完成请求的服务
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 968176 bytes for Chunk::new
# Possible reasons:
# The system is out of physical RAM or swap space
# In 32 bit mode, the process size limit was hit
# Possible solutions:
# Reduce memory load on the system
# Increase physical memory or swap space
# Check if swap backing store is full
# Use 64 bit Java on a 64 bit OS
# Decrease Java heap size (-Xmx/-Xms)
# Decrease number of Java threads
# Decrease Java thread stack sizes (-Xss)
# Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.
#
# Out of Memory Error (allocation.cpp:323), pid=4224, tid=5556
那么可以尝试如下配置