zoukankan      html  css  js  c++  java
  • sonar环境的搭建

    作者:维唯为为

    简单介绍

        Hudson 是一个可扩展的持续集成引擎(Continuous Integration Engine)。主要用于:持续、自动地构建/ 测试软件项目. 监控一些定时执行的任务;

        Sonar 是一个开源的质量管理平台,专注于从项目到类方法的持续的分析和测量技术质量,它把代码质量相关软件集成到一起统一管理。

        简单来说:

        hudson 是持续、自动地构建/ 测试软件项目;sonar 则是持续,自动地统计并分析软件项目的相关质量数据,例如单元测试的通过率,覆盖率,代码的复杂度,代码的行数等等,用于评估和度量软件项目质量。

       Sonar 是一个开源的代码质量管理平台,专注于对从项目组合到类方法的持续分析和度量其技术质量,当然你也可以使用开源的插件来扩展Sonar,查看扩展可以访问 open source plugins forge 

    以下是sonar环境及相关技术的一个关联图:

    工具说明:

    jacoco : is an Open Source and robust code coverage tool , Sonar is delivered with Cobertura plugin but if you prefer JaCoCo you just have to install this plugin and go back to your favorite practice : the test driven development.

    findbugs:

    第一步.创建数据库

        sornar默认就装载了Apache Derby ( an Apache DB subproject ) ,所以如果使用 Derby 不需要安装;它只是用于演示和测试,在实际应用中还是用外部强健的数据库,更多详细见  supported platforms 

        要使用外部数据库,它要求明确建立 database schema permission ,当第一次启动sonar 的时候,table index会自动创建,例如设置使用的外部数据库为  MySql 数据库,可以用以下脚本:

    #

    # Create Sonar database and user.

    #

    # Command: mysql -u root -p < create_database.sql

    #

    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;

    下载地址: extras/database/mysql

    注:当创建数据库时,Character Set 推荐使用utf-8 ,同时校验规则 Collation 是对大小写敏感。

    第二步.下载安装sonar

    1.下载并解压它的发行版本 the distribution.

    2.如果你使用sonar默认内嵌的Derby数据库,编辑 conf/sonar.properties文件配置对数据的访问,这个模板适用于所有支持的数据库,只需要注释掉derby专用的前四行,同取消对要引用外部数据库链接的注释。

    原文如下:

    If you do not use the default embedded database, edit conf/sonar.properties to configure the database access. Templates are available for every supported database. Just uncomment them and comment the first four lines dedicated to derby.

    • sonar.jdbc.url : the URL of the database
    • sonar.jdbc.driver : the class of the driver
    • sonar.jdbc.user : the username (default value is 'sonar')
    • sonar.jdbc.password : the password (default value is 'sonar')

      Example for MySQL :

      #sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true

      #sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver

      #sonar.jdbc.defaultTransactionIsolation: 1

      #sonar.jdbc.validationQuery: values(1)

      sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

      sonar.jdbc.driverClassName: com.mysql.jdbc.Driver

      sonar.jdbc.validationQuery: select 1 

      第三步.启动 sonar 应用

      启动 sonar 应用,这里有几种方式:

      Mode 1 - Start the standalone application

      编辑conf/sonar.properties文件,

      我这里设置的是:

      sonar.web.host : 192.168.5.28

      sonar.web.port: 9000

      sonar.web.context: /sonar

      执行以下脚本,启动sonar服务;

       On Linux/Mac OS : bin/<YOUR OS>/sonar.sh start

       On MS Windows : bin/windows-x86-32/StartSonar.bat

       Read "Install on AIX, HPUX or Solaris" for other platforms

      You can now browse to http://localhost:9000.

      在浏览器中访问,如图:

      2

      Mode 2 - Deploy on JEE Server

      sonar应用也可以打包成war布署到JEE server上,支持 Tomcat 5.x, 6.x, 7.x and Jetty 6.x

      安装步骤:

       1.Edit conf/sonar.properties as described in standalone mode. The file conf/wrapper.conf is never used when deploying to application server.

       2.Execute the script build-war.sh (or build-war.bat on MS Windows) from the directory war/

       3.Deploy war/sonar.war to the application server

       4.Browse to http://localhost/sonar and follow setup instructions

      注:sonar要求的最小堆内存大小是512Mb,所以在启动tomcat之前,通过设置 CATALINA_OPTS,来增加tomcatmemory heap size。如,修改catalina.bat :

      set CATALINA_OPTS=-Xmx1024m -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true -XX:MaxPermSize=256m

      set set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_24

      Set JAVA_OPTS=-Xms512m -Xmx256m

      Mode 3 - Run as a service on MS Windows

      Install/uninstall NT service (may have to run these batch files via "Run As Administrator"):

      bin/windows-x86-32/InstallNTService.bat

      bin/windows-x86-32/UninstallNTService.bat 

      Start/stop the service :

      bin/windows-x86-32/StartNTService.bat

      bin/windows-x86-32/StopNTService.bat 

         

      Mode 4 - Run as a service on Linux

      The following has been tested on Ubuntu 8.10.

      Create the file /etc/init.d/sonar with this content :

      #! /bin/sh

      /usr/bin/sonar $* 

      Register Sonar at boot time :

      sudo ln -s $SONAR_HOME/bin/linux-x86-32/sonar.sh /usr/bin/sonar

      sudo chmod 755 /etc/init.d/sonar

      sudo update-rc.d sonar defaults 

      Running Sonar behind Nginx

      我们假设你已经安装了Nginx, sonar的虚拟主机(visual host)是使用 www.somecompany.com 并且sonar服务已启动,并且对应的 http://sonahost:sonarport/可用.

      编辑Nginx配置文件. sonar的主机 http://www.somecompany.com/ 包含进来,如下:

      # the server directive is nginx's virtual host directive

      server {

        # port to listen on. Can also be set to an IP:PORT

        listen 80;

         

        # sets the domain[s] that this vhost server requests for

        server_name www.somecompany.com;

         

        location / {

          proxy_pass http://sonarhost:sonarport;

        }

      }

      Nginx configuration is going to vary based on your own application's requirements and the way you intend to expose Sonar to the outside world. If you need more details about Nginx, please see http://nginx.org.

      配置多个tomcat

    1. 下载压缩版的tomcat,
    2. 增加环境变量CATALINA_HOME2,值为新的tomcat的地址;增加环境变量CATALINA_BASE2,值为新的tomcat的地址。
    3. 修改新的tomcat中的startup.bat,把其中的CATALINA_HOME改为CATALINA_HOME2
    4. 修改新的tomcat中的catalina.bat,把其中的CATALINA_HOME改为CATALINA_HOME2CATALINA_BASE改为CATALINA_BASE2
    5. 修改conf/server.xml文件,将已被占用的端口都修改掉。

    注:<Connector port="8180" protocol="HTTP/1.1"

    connectionTimeout="20000"

    redirectPort="8543"

                 URIEncoding="UTF-8" />

    Maven环境的安装

    参考:http://maven.apache.org/download.html

    Windows 2000/XP

    1. 解压apache-maven-3.0.4-bin.zip 到你指定的安装目录. 这里默认假设安装到 C:\Program Files\Apache Software Foundation.
    2. 添加 M2_HOME = C:\Program Files\Apache Software Foundation\apache-maven-3.0.4. 不要有任何空格或引号: Maven2.0.9以下版本, M2_HOME 的路径的值最后面不要有 '\' .
    3. 在同一个对话框中添加环境变量 M2 = %M2_HOME%\bin.
    4. 可选: 添加环境变量 MAVEN_OPTS = -Xms256m -Xmx512m. =
    5. 确保存在环境变量 JAVA_HOME = <YOU JDK INSTALL DIRECTORY>\Java\jdk1.5.0_02 以及  %JAVA_HOME%\bin 也被添加到了 Path 环境变量中.
    6. 打开命令行,运行 mvn --version 确认maven是否正确安装.

      如下图:

       

    Unix-based Operating Systems (Linux, Solaris and Mac OS X)

    1. 解压 apache-maven-3.0.4-bin.tar.gz 到你指定的安装目录,这里假设你安装到 /usr/local/apache-maven.
    2. 在命令终端添加环境变量 M2_HOME ,例如 export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4.
    3. 添加环境变量 M2 ,例如 export M2=$M2_HOME/bin.
    4. 可选: 添加环境变量 MAVEN_OPTS用来指JVM的属性, 例如 export MAVEN_OPTS="-Xms256m -Xmx512m".
    5. 添加M2环境变量 M2 到你的PATH环境变量中, 例如 export PATH=$M2:$PATH.
    6. 确保JAVA_HOME 正确的配置到了你的 JDK, 例如 export JAVA_HOME=/usr/java/jdk1.5.0_02,并且 $JAVA_HOME/bin 也添加到了你的环境变量 PATH .
    7. 在终端运 mvn --version 确认Maven是否安装正确.

     

    在maven中配置sonar

    全局配置

    使用maven的插件让Sonar执行项目分析是最简单、使用最广泛的一种方式,To do so, you should edit settings.xml file, located into $MAVEN_HOME/conf or ~/.m2, and the database parameters to be used as well as the Sonar server URL

    例如:

    <settings>

    <profiles>

    <profile>

    <id>sonar</id>

    <activation>

    <activeByDefault>true</activeByDefault>

    </activation>

    <properties>

    <!-- EXAMPLE FOR MYSQL -->

    <sonar.jdbc.url>

    jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8

    </sonar.jdbc.url>

    <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>

    <sonar.jdbc.username>sonar</sonar.jdbc.username>

    <sonar.jdbc.password>sonar</sonar.jdbc.password>

     

    <!-- optional URL to server. Default value is http://localhost:9000 -->

    <sonar.host.url>

    http://localhost:8180/sonar <!—此处填访问sonar应用的url -->

    </sonar.host.url>

    </properties>

    </profile>

    </profiles>

    </settings>

    分析一个由Maven构建的项目

    分析一个Maven project,首先要在包含了pom.xml  文件的项目目录下,运行一个maven goal , maven goal的执行要在sonar分析之前执行,maven命令如下:

    mvn clean install -Dtest=false-DfailIfNoTests=false

    mvn sonar:sonar

    或者直接用这个命令:

    mvn clean install sonar:sonar -Dmaven.test.failure.ignore=true

    如图:

    注:ayyc 是一个maven project , ayyc目录下包含了pom.xml文件。

    分析一个非maven构建的项目

    其实,sonar可以分析任何一个Java项目,即使它不是由maven2构建的。当这个项目中配置了可用的xml文件(下面会它的内容格式)以及收集这个项目的信息的sonar应用服务也被被配置好了,那么就可以分析这个项目的代码了(包括单元测试的代码)。

        首先,安装 Maven2 (just unzip, define the $MAVEN_HOME property and add $MAVEN_HOME/bin to $PATH)

        然后,在你的项目的根目录下创建一个名为 pom.xml 的文件。pom.xml如下编辑:

    <project xmlns="http://maven.apache.org/POM/4.0.0"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>[YOUR.ORGANIZATION]</groupId>

    <artifactId>[YOUR.PROJECT]</artifactId>

    <name>[YOUR PROJECT NAME]</name>

    <version>[YOUR PROJECT VERSION]</version>

    <build>

    <sourceDirectory>[YOUR SOURCE DIRECTORY]</sourceDirectory>

    <outputDirectory>[YOUR CLASSES/BIN DIRECTORY</outputDirectory>

    <plugins>

    <plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-compiler-plugin</artifactId>

    <configuration>

    <source>1.5</source>

    <target>1.5</target>

    <excludes>

    <exclude>**/*.*</exclude>

    </excludes>

    </configuration>

    </plugin>

    </plugins>

    </build>

    <properties>

    <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>

    </properties>

    </project>

    这些参数代表的意义如下表:

      

    Description 

    Example  

    [YOUR.ORGANIZATION]  

    the id of your organization (no space)  

    com.myorganization 

    [YOUR.PROJECT] 

    the id of your project (no space)  

    my.project 

    [YOUR PROJECT NAME] 

    the name displayed into sonar (spaces allowed)  

    My Project 

    [YOUR PROJECT VERSION] 

    the version. Set 1.0 if no specific version.  

    1.0 

    [YOUR SOURCE DIRECTORY] 

    the relative path to the sources directory  

    src/java  

    [YOUR CLASSES/BIN DIRECTORY] 

    the relative path to the compiled java classes directory  

    bin  

    执行maven plugin,使用如下命令:

    mvn sonar:sonar

    如下图:

    注:在findbugssonar light模式下

        sonar light模式下,findbugs会被激活,但是findbugs需要已被编译好的class文件,才能去找不合符规范的类。在maven项目下,findbugs默认尝试在target/classes目录下找; 如果你是用Ant生成的类,那么你需要在pom.xml文件中的"<sourceDirectory> 结点下添加"<outputDirectory>[YOUR CLASSES/BIN DIRECTORY]</outputDirectory>",另外你在还要在"<dependencies>"结点下为findbugs定义aux classpath,配置如下:

    <dependency>

        <groupId>deps</groupId>

        <artifactId>dep1</artifactId>

        <version>0.1</version>

        <scope>system</scope>

        <systemPath>${basedir}/web_app/WEB-INF/lib/struts.jar</systemPath>

    </dependency>

    如果你少添加了依赖的jar包,就会看到有如下提示信息:

    The following classes needed foranalysis were missing:

     org.springframework.jdbc.object.StoredProcedure

     org.displaytag.decorator.TableDecorator

    更多详细内容请参见:http://docs.codehaus.org/display/SONAR/Analyse+with+Maven

    hudson的安装配置

    安装

    第一步:把的hudson.war 包放到tomcat webapps下,然后启动tomcat

    第二步:在浏览器中输入http://hudsonhost:8080/hudson 如果出现正常页面表示Hudson 已经安装成功。

    第三步:配置hudson,如下图:

    系统消息:可以理解为登录系统后的欢迎词,你可以写点随意的东西,然后弄个 Logo 图片什么的,它也支持 HTML 语言。

    生成前等待时间:这个值是表示你可以让你的构建有等待时间,以防止在提交代码的过程中去开始构建。这里是全局的变量,也可以在每个项目里面去设定。

    启用安全:这里是 hudson 权限的相应设置,如果想对每个用户进行相应的权限操作,选择授权策略项下的"项目矩阵授权策略"这个选项就可以进行相应的设置了。

    mavenjdk设置:就是取个名字,然后路径中指定 maven2 的解压路径。在这里设置时,去掉自动安装,直接指定 MAVEN_HOME(依个人设置)即可。

    注:选中hudson专有数据库,才能有用户注册的功能。

    集成sonar

    sonar搭建好后,进入sonar插件的安装:

    1进入Hudson的系统管理->的插件管理,进入"可用插件",选择sonar( Hudson Sonar Plugin) 插件,在页面右下角(最下面)点击安装,页面提示成功,重启hudson ,插件就生效了。如下图:

    2.配置sonar插件,在系统配置页面找到Sonar,配置sonar的系统信息,如下图:

    3.新建一个hudson Job,配置构建选项,如下图:

    配置项目关联到sonar,如下图:

    svn服务端的安装配置

        windows下安装,首先,下载svn服务端,直接安装,完成后,配置svn的数据仓库,如下:

    1. 创建SVN储存库(repository),可以选择命令行方式或者通过TortoiseSVN插件进行GUI操作,命令行运行:svnadmin create E:\svn\repository
    2. repository创建完毕后会在目录下生成若干个文件和文件夹,dav目录是提供给Apachemod_dav_svn使用的目录,让它们存储内部数据;db目录就是所有版本控制的数据文件;hooks目录放置hook脚本文件的目录;locks用来放置Subversion文件库锁定数据的目录,用来追踪存取文件库的客户端;format文件是一个文本文件,里面只放了一个整数,表示当前文件库配置的版本号。
    3. 打开svnserve.conf,找到:

      # [general]

      # anon-access = read

      # auth-access = write

      # password-db = passwd

      去之每行开头的#password-db = passwd第二行是指定身份验证的文件名,即passwd文件

    4. 同样打开passwd文件,找到:

      # [users]

      # harry = harryssecret

      # sally = sallyssecret

      把这几行的开头#字符去掉,这是设置用户,一行一个,存储格式为"用户名 = 密码"

    5. 设置服务

      安装服务

      sc create subversion_service binpath= "c:\subversion\bin\svnserve.exe --service -r c:\svn_test\repos" displayname= "Subversion Repository" depend= Tcpip

      删除服务

      sc delete subversion_service

    6.svn目录设置

    trunk——主干:这里存放的是最新稳写的源码,权限为项目组长可写,其它任何人有可读的权限。

    branches——开发分支:这里存放的是各开发中的版本,权限为任何开发人员可写,其它任何人不可读。

    tags——快照发布版本:这里存放的是临时发布的版本,权限为任何开发人员可写,其它任何人不可读。

    developer-resources——开发资源:这里存放的是开发者所需的开发资源,如:各类公用类库、.jar等。

    在eclipse安装各种插件

    将要安装的插件包中的featuresplugins,解压到<YOUR INSTALL PATH> \Genuitec\MyEclipse 8.5\dorpins\plugin_name目录下,并在此目录下建立一个plugin_name.link,文件内容是:

    path=<YOUR INSTALL PATH> \Genuitec\MyEclipse 8.5\dorpins\plugin_name

    例如安装svn插件,即可在此目录下建立一个svn的文件夹,将插件包中的featuresplugins,拷到此目录下,并在此目录,新建一个svn.link的文件,文件内容是:

    path=<YOUR INSTALL PATH> \Genuitec\MyEclipse 8.5\dorpins\svn.link

    下载地址:\\192.168.1.201\wap\技术资源共享\sonar\eclipse-plugins,包括:

    subclispeeclipseplugin-checkstylesonar eclipsepmdm2eclipse

    eclipse中的各项配置

    sonar的配置

    指定sonar server

    当安装好了sonar-eclipse插件后,可以在 Eclipse 全局 Preferences 窗口中看到,编辑后,如下图:

    连一个projectssonar server

    1. Package explore 对要连接到sonar server的项目右击,选择"Configure -> Associate with Sonar..." :

    注:Q: when i go to eclipse configure>associate with sonar says that my groupId is empty

        A: There few things to check:

    1. Check the groupId is actually there in your pom.xml.
    2. Check that the artifactId and the eclipse project name are the same.
    3. Check that you typed groupId and artifactId in the associate with sonar dialog window. Then click find on server.
    4. Also make sure that the sonar plugin is set up on your eclipse (by going to Windows->Preferences->Sonar).

    关联好后,在项目上点击右键,选择,如下图:

    sonar分析此项目的情况,如下图:

    Dashboard标签下,可以看到ttpod-ayyc的代码的行数、类的个数、注释所占百分比等信息。

    sonar应用的使用及对项目的分析详解

    更加详细可查看:http://docs.codehaus.org/display/SONAR/Documentation

    sonar server的整体介绍

    sonar workspace

    The Sonar web site is divided into 3 main areas:

    • "Home" area
    • Project area
    • Configuration area

    顶部工具条,如下图:

    左侧菜单,当当前的操作针对所有project时,如下图:

    当当前操作一个 project Area,如下图:

    sonar应用中项目

    包括的分析视图有:

    The Dashboard,The Rules Compliance Index (RCI),Design & Architecture,The project Treemap,The Events,Measures Drilldown,Violations Drilldown,Coverage Clouds,Hotspots,Components,Design / DSM,Managing libraries

    详细内容可参考:http://docs.codehaus.org/display/SONAR/Browsing+a+Project

    资源查看器

    在这个视图中包括:

    coverage:覆盖率,是通过标注额外的提示信息和不同颜色显示覆盖情况,比如,代码对应的单元测试的条件的覆盖率。

    Violations:违规的,同样也是通过标注额外的提示信息和不同颜色显示违规信息

    Duplications:重复的,显示了具体重复的代码

    Dependencies:依赖关系,例举出了被选中的文件的内向与外向的依赖关系

    LCOM4:紧密关系,

    source:项目源代码

    Test:单元测试的代码

    sonar的Advanced parameters

        这些参数可以在多个地方设置,比如可以在以下地方设置:

    • Global parameters, defined in the UI, will apply to all projects
    • Project parameters, defined in the UI, will override global parameters
    • Project parameters, defined in configuration file, will override the ones defined in the UI
    • Command line parameters, defined at analysis time, will override any parameters

    下表列出了sonar的配置参数:具体参看此链接 sonar的高级参数

    sonar的第一部份内容,就介绍到此。

  • 相关阅读:
    6)图[5]最短路径
    6)图[4]关键路径
    6)图[3]拓扑排序算法
    6)图[2]Prim算法[最小生成树]
    Aprori算法[关联规则算法]
    K-Modes算法[聚类算法]
    linux Centos6.7 python交互模式下回退异常问题解决
    Python-面向对象(二)-Day7
    Python-面向对象(一)-Day7
    (error) MISCONF Redis is configured to save RDB snapshots
  • 原文地址:https://www.cnblogs.com/luowei010101/p/2400813.html
Copyright © 2011-2022 走看看