sonar服务器端和客户端的安装和使用

本篇文章将sonar的安装说明一下:
环境 1.JDK:1.8.0_60
   2.sonarqube:sonarqube-5.4版本服务器(mysql5.5包括及以上)
   3.sonar-runner:sonar-runner-2.3客户端
   4.操作系统 win8 64位
第一步:sonarqube-5.4 解压即可使用,先配置配置文件。
配置:
1.sonarqube-5.4\conf中sonar.properties
1).mysql:先创建一个数据库sonar,这里root是指可以访问这个库的用户。
sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
2.web启动访问端口:
sonar.web.port=9000
/sonarqube-5.4/bin/windows-x86-64/StartSonar.bat启动
3.sonarqube-5.4/logs中可以看到启动日志
当看到Web server is startedHTTP connector enabled on port 9000Process[web] is up就说明启动好了。
浏览器输入:http://127.0.0.1:9000/
4.插件安装
1)上述 进入http://127.0.0.1:9000/,默认用户名和密码:admin/admin
点击配置菜单栏-》系统-》更新中心 可以下载插件:中文,checkstyle等,当下载不下来。可以选择手动安装。
2).手动安装:git网址:https://github.com/SonarQubeCommunity
realease下载jar,放置到sonarqube-5.4/extensions/plugins下,重新启动sonarqube就会安装了。
2.第二步:解压sonar-runner-2.3
配置环境变量到path。
SONAR_RUNNER_HOME:D:\Program Files\sonar-runner-2.3
path增加%SONAR_RUNNER_HOME%\bin;
3.需要使用sonar分析代码的项目中增加配置文件sonar-project.properties
# Root project information
sonar.projectKey=allProjects
sonar.projectName=allProjects
sonar.projectVersion=1.0.0

# Some properties that will be inherited by the modules
sonar.sources=src

# List of the module identifiers
sonar.modules=a,b,c

# Properties can obviously be overriden for
# each module - just prefix them with the module ID
a.sonar.projectName=a1
b.sonar.projectName=b1
c.sonar.projectName=c1
ok!配置完成

下面使用sonar进行代码分析了:
第一种方法:
maven:在settings.xml配置sonar中配置
<profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <sonar.jdbc.url>
            jdbc:mysql://[sonar_db_host]:3306/sonar?useUnicode=trueamp;characterEncoding=utf8
        </sonar.jdbc.url>
        <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
        <sonar.jdbc.username>root</sonar.jdbc.username>
        <sonar.jdbc.password>root</sonar.jdbc.password>
        <sonar.host.url>[SonarQube_host]</sonar.host.url>
    </properties>
</profile>
然后使用maven方式,goals为sonar:sonar  第一个sonar表示maven的sonar插件,第二个sonar表示sonar插件中的sonar目标.
ok


第二种方法:
之前描述过安装了sonar-runner-2.3客户端而且也配置了环境变量。
所以可以直接使用命令行的方式进入需要分析代码的项目代码目录下(目录下配置好了sonar-project.properties文件,不然会报错unknown Unable to load component class org.sonar.batch.scan.ProjectLock  You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.projectName, sonar.projectVersion),执行sonar-runner ok.

第三种方法:使用持续化集成工具jenkins/hudson
详细的操作参见hudson的安装使用篇!


备注参考网址:
maven插件:
http://www.infoq.com/cn/news/2011/05/xxb-maven-8-plugin
http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin
sonar:
http://blog.csdn.net/yzhou86/article/details/41170669
http://www.ituring.com.cn/article/69556
maven与sonar结合:goals---->sonar:sonar
http://www.cnblogs.com/cuiliqiang/archive/2012/10/15/2725146.html

sonar 原理:
http://www.cnblogs.com/cuiliqiang/archive/2012/10/17/2727024.html
sonar 安装:https://my.oschina.net/jiangbianwanghai/blog/478338?p=1
checkStyle 插件源码https://github.com/checkstyle/sonar-checkstyle
sonar插件下载:https://github.com/SonarQubeCommunity

猜你喜欢

转载自javakill.iteye.com/blog/2405367
今日推荐