sonar环境的搭建

简单介绍

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

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

    简单来说:

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

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

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

 

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

工具说明:

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 -< 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.

    在浏览器中访问,如图:

    sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

     

    2

    sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

     

    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_HOME2 CATALINA_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是否正确安装.

    如下图:

     

    sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

     

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

如图:

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

注: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 for analysis were missing:

 org.springframework.jdbc.object.StoredProcedure

 org.displaytag.decorator.TableDecorator

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

 sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

hudson的安装配置

安装

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

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

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

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

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

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

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

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

 

集成sonar

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

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

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

 sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

   

svn服务端的安装配置

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

  1. 创建 SVN 储存库 (repository) ,可以选择命令行方式或者通过 TortoiseSVN 插件进行 GUI 操作,命令行运行: svnadmin create E:\svn\repository 
  2. repository 创建完毕后会在目录下生成若干个文件和文件夹, dav 目录是提供给 Apache mod_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,包括:

subclispe eclipseplugin-checkstylesonar eclipsepmdm2eclipse

 

eclipse中的各项配置

 

sonar的配置

指定sonar server

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

 连一个projectssonar server

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

注: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).

 

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

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

sornar环境的搭建 - luowei505050@126 - 维唯为为_126博客

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Docker搭建SonarQube,首先需要拉取SonarQube的镜像并安装。可以通过以下命令拉取SonarQube镜像: ``` docker pull sonarqube ``` 然后,可以重新构建SonarQube镜像来添加汉化包和其他插件。可以使用以下Dockerfile来重新构建镜像: ``` FROM sonarqube ADD sonar-l10n-zh-plugin-8.6******``` 这是因为vm.max_map_count的值太低导致的。你可以通过以下命令来增加vm.max_map_count的值: ``` sysctl -w vm.max_map_count=262144 ``` 最后,还需要搭建一个PostgreSQL数据库容器来与SonarQube容器连接。可以使用以下命令来搭建PostgreSQL容器: ``` docker run -d -p 5432:5432 --name postgresql -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar postgres ``` 这样,你就成功地使用Docker搭建SonarQube。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Docker搭建代码检测平台SonarQube并检测maven项目的流程](https://download.csdn.net/download/weixin_38651273/14048771)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [docker搭建sonar](https://blog.csdn.net/qq_39122146/article/details/112328478)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值