Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 1261|回复: 0

sonar环境的搭建

  [复制链接]
  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2013-3-11 22:46:27 | 显示全部楼层 |阅读模式

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

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

        简单来说:

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

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


    工具说明:

    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: a program which uses static analysis to look for bugs in java code.

    Squid: Squid is a plugin that gathers all standard metrics number of classes, number of comments, cyclomatic complexity...). Originally, Sonar was using JavaNCSS, but they were too many limitations.Squid keeps by default methods and accessors (beans getters and setters) separated.

    PMD CPD : This is the historical engine to search for copy / paste. Mainly for performances reasons, it only enables to do copy / paste detection within a project and event within a maven module when maven is used.

    第一步.创建数据库

        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.


    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

     楼主| 发表于 2013-3-11 22:46:42 | 显示全部楼层
    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,来增加tomcat的memory 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 IPORT

      listen 80;

       

      # sets the domain 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.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

     楼主| 发表于 2013-3-11 22:53:05 | 显示全部楼层
    在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文件。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

     楼主| 发表于 2013-3-11 22:53:43 | 显示全部楼层
    分析一个非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

    如下图:


    注:在findbugs和sonar 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 for analysis were missing:

    org.springframework.jdbc.object.StoredProcedure

    org.displaytag.decorator.TableDecorator

    更多详细内容请参见:http://docs.codehaus.org/display/SONAR/Analyse+with+Maven
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

     楼主| 发表于 2013-3-11 23:05:21 | 显示全部楼层
    sonar的eclipse插件配置
    指定sonar server
    当安装好了sonar-eclipse插件后,可以在 Eclipse 全局 Preferences 窗口中看到,编辑后,如下图:


    连一个projects到sonar 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:

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


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


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


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

    使用道具 举报

    该用户从未签到

    发表于 2013-3-11 23:14:42 | 显示全部楼层
    新版块啊 来围观下!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

     楼主| 发表于 2013-3-12 08:40:32 | 显示全部楼层
    图片来自网易,可能无法显示,打开博文源出去后,刷新页面即可显示。。
    博文地址:http://blog.163.com/luowei505050 ... 206201221674156438/
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-23 15:07 , Processed in 0.423296 second(s), 49 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表