针对非maven模式的JAVA项目进行sonar-runner配置

来源:互联网 发布:验证yum源修改 编辑:程序博客网 时间:2024/05/26 20:24

(原文地址 http://www.ituring.com.cn/article/69556)

过年后,北京分公司的同事移交了一个项目给我们上海分公司。目前此项目由我负责。因为原来上海分公司这边的java项目都是maven配置模式,因此用sonar基本上很轻松方便。但是这个项目没有用maven,也没有用ant,因此想在sonar中查看此项目就有点困难,我在sonar官网上找了sonar-runner,又结合网上一些资源对此项目进行了配置。现特地将整个配置过程分享给大家。谢谢。

注:没有使用过sonar的朋友,可参考以下两个网页对maven配置的java项目安装sonarhttp://my.oschina.net/codingforme/blog/185106

http://my.oschina.net/xiaokaceng/blog/177862

前置条件

1.已安装JAVA环境

2.已安装MySQL数据库

3.已安装DbVisualizer

4.已安装sonar

5.已下载sonar-runner

下载地址:

http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.3/sonar-runner-dist-2.3.zip

6.操作系统:win8.1

Step 1:

在DbVisualizer里执行下列语句,创建database和user

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;

连接创建的sonar数据库

url:jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8user:sonarpassword:sonar  

详细配置如下图: enter image description here[+]查看原图

Step 2:

安装sonar-runner

将下载包解压至win 8某目录,添加SONAR_RUNNER_HOME环境变量,并将SONAR_RUNNER_HOME加入PATH

修改sonar配置文件(在前置条件里已经默认安装了sonar,所以直接去sonar安装目录修改)

编辑/conf/sonar.properties文件,配置数据库设置,默认已经提供了各类数据库的支持,这里使用mysql,因此如下:

# Permissions to create tables, indices and triggers must be granted to JDBC user.# The schema must be created first.sonar.jdbc.username=sonarsonar.jdbc.password=sonar........#----- MySQL 5.x# Comment the embedded database and uncomment the following line to use MySQLsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true........#----- Connection pool settingssonar.jdbc.maxActive=20sonar.jdbc.maxIdle=5sonar.jdbc.minIdle=2sonar.jdbc.maxWait=5000sonar.jdbc.minEvictableIdleTimeMillis=600000sonar.jdbc.timeBetweenEvictionRunsMillis=30000

修改sonar-runner的配置文件

切换至sonar-runner的安装目录下,修改sonar-runner.properties如下:

#Configure here general information about the environment, such as SonarQube DB details for example#No information about specific project should appear here#----- Default SonarQube serversonar.host.url=http://localhost:9000#----- PostgreSQL#sonar.jdbc.url=jdbc:postgresql://localhost/sonar#----- MySQLsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8sonar.jdbc.driver=com.mysql.jdbc.Driver#----- Oracle#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE#----- Microsoft SQLServer#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor#----- Global database settingssonar.jdbc.username=sonarsonar.jdbc.password=sonar#----- Default source code encodingsonar.sourceEncoding=UTF-8#----- Security (when 'sonar.forceAuthentication' is set to 'true')#sonar.login=admin#sonar.password=admin

Step 3:

配置sonar要分析的目标项目

在要分析的目标项目源代码根目录下新建sonar-project.properties文件

文件内容如下:

# Required metadatasonar.projectKey=my:projectsonar.projectName=multiMediasonar.projectVersion=1.0# Paths to source directories.# Paths are relative to the sonar-project.properties file. Replace "\" by "/" on Windows.# Do not put the "sonar-project.properties" file in the same directory with the source code.# (i.e. never set the "sonar.sources" property to ".")sonar.sources=C:/wjs/workspace/multiMedia/src# The value of the property must be the key of the language.sonar.language=java# Encoding of the source codesonar.sourceEncoding=UTF-8# Additional parameterssonar.my.property=value

说明

sonar.projectKey和sonar.projectName内容无硬性规定,最好不要有中文

根据sonar.sources上方的注释可知在windows系统中,应该写"/"而不是"\",且不能和源代码文件放在同一目录下,而且经过测试我发现这里要写绝对路径。multiMedia就是我的目标项目名。

启动sonar

目录切换至sonar的/bin/windows-x86-64目录,双击StartSonar.bat文件,启动sonar(因为我的操作系统是win8,64位,可根据自身操作系统情况选择相应目录)

启动sonar-runner

在目标项目源代码根目录下打开cmd窗口,执行sonar-runner命令(如下)

C:\wjs\workspace\multiMedia\src>sonar-runner

也可以加参数查看分析过程(如下)

C:\wjs\workspace\multiMedia\src>sonar-runner -X

C:\wjs\workspace\multiMedia\src>sonar-runner -e

运行成功界面如下:
enter image description here

enter image description here

访问http:\localhost:9000即可查看分析结果,如下图:
enter image description here[+]查看原图

0 0
原创粉丝点击