maven+jenkins+sonar集成搭建

本文详细介绍如何搭建集成质量管理平台,包括Sonar、Maven、Jenkins等工具的安装配置流程,以及它们之间的集成方法,并提供了常见问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载地址:http://blog.youkuaiyun.com/wzd_0116/article/details/24651333

集成质量管理平台搭建

 

第一章 Sonar安装... 2

1.1下载sonar2

1.2安装sonar2

1.2.1 JDK安装配置... 2

1.2.2数据库安装... 2

1.3成功界面... 3

1.4配置插件... 3

1.4.1插件介绍... 3

1.4.2常用的插件:... 4

第二章 Maven安装... 5

2.1下载 Maven5

2.2配置Maven5

第三章 Jenkins安装... 5

3.1安装Jenkins5

第四章 Sonar-Runner安装... 6

4.1下载Sonar-Runner6

4.2配置Sonar-Runner环境变量... 6

4.3测试文件的配置... 6

4.4应用sonar监测代码质量... 7

第五章 环境集成... 7

5.1 SonarJenkins集成... 7

5.2 Jenkins系统配置... 9

5.3 Jenkins项目配置... 10

第六章 常见错误... 14

6.1 sonar没有启动... 14

6.2 Maven setting.xml配置... 15

6.3  文件POM.xml配置... 16

6.4  0 files indexd16

  

第一章 Sonar安装

1.1下载sonar

     最新版版本:4.2,稳定版:3.7(本人建议下载3.7稳定版)

     下载地址:http://www.sonarsource.org/downloads/

下载完成之后,要注意一个问题,官方的说明是不要安装在以数字开头的目录中。所以我们就不要解压在数字开头的目录中。

1.2安装sonar

1.2.1 JDK安装配置

这个不予多说,最基本的JAVA开发环境配置,下载JDK,配置其JAVA_HOMEPATH等。

1.2.2数据库安装

1)本人是选择的MySQL,这里要注意的是如果你下载的是免安装版,直接解压使用的话,需要配置my.ini

2)在数据库中创建新用户sonar 密码为sonar并给用户赋权。(用户和密码可以自定义,如果自定义需要更改sonar-3.7\conf\sonar.properties配置文件中得sonar.jdbc.usernamesonar.jdbc.password,其默认的就是sonar)。

3)配置sonar.properties文件。

a.配置启动的http端口:

sonar.web.host:                           localhost(或者你的IP

sonar.web.port:                           9000(默认是9000,你可以修改,不然会和Jenkins的端口号冲突)

sonar.web.context:                        /

    (三句前本来被注释,取消注释)

  b.取消MySQL连接的注释,并输入自己数据库的url地址,同时将默认的sonar.jdbc.url屏蔽

#----- MySQL 5.x

# Comment the embeddeddatabase and uncomment the following line to use MySQL

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

# :mysql是指你要连接的事Mysql数据库,这里我给Sonar的权限是管理员所以可以用localhost,否则你要使用的你的IP地址,3306是你使用的端口号,test是我测试时创建的数据库,你需要改成你自己的数据库名。

# Optionalproperties 

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

sonar.jdbc.validationQuery:  select 1

c.进入bin中,选择适合自己系统,运行sonar.sh,如果是windows运行bat。运行sonar-3.7\bin\windows-x86-64\StartSonar.bat,打开相应的网页:http://localhost:9000测试是否配置成功,这里的页面链接跟前头的http配置有关。(第一次运行StartSonar.bat,时需要的时间较长,请耐心等待,切不可关闭窗口重新加载,sonar其需要创建一个和Mysql数据库相关的migrating数据库和表,如果中途退出则创建不成功!

1.3成功界面

登陆用户名密码都是admin

                          

1.4配置插件

1.4.1插件介绍

Sonar支持多种插件,插件的下载地址为:http://docs.codehaus.org/display/SONAR/Plugin+Library

将下载后的插件上传到${SONAR_HOME}extensions\plugins目录下,重新启动sonar

sonar默认集成了Java Ecosystem插件,该插件是一组插件的合集

1Java [sonar-java-plugin]java源代码解析,计算指标等

2Squid [sonar-squid-java-plugin]:检查违反Sonar定义规则的代码

3Checkstyle [sonar-checkstyle-plugin]:使用CheckStyle检查违反统一代码编写风格的代码

4FindBugs [sonar-findbugs-plugin]:使用FindBugs检查违反规则的缺陷代码

5PMD [sonar-pmd-plugin]:使用pmd检查违反规则的代码

6Surefire [sonar-surefire-plugin]:使用Surefire执行单元测试

7Cobertura [sonar-cobertura-plugin]:使用Cobertura获取代码覆盖率

8JaCoCo [sonar-jacoco-plugin]:使用JaCOCO获取代码覆盖率

 

1.4.2常用的插件:

1JavaScript代码检查:http://docs.codehaus.org/display/SONAR/JavaScript+Plugin

2python代码检查:http://docs.codehaus.org/display/SONAR/Python+Plugin

3Web页面检查(HTMLJSPJSFRubyPHP等):http://docs.codehaus.org/display/SONAR/Web+Plugin

4xml文件检查:http://docs.codehaus.org/display/SONAR/XML+Plugin

5scm源码库统计分析:http://docs.codehaus.org/display/SONAR/SCM+Stats+Plugin

6)文件度量:http://docs.codehaus.org/display/SONAR/Tab+Metrics+Plugin

7)中文语言包:http://docs.codehaus.org/display/SONAR/Chinese+Pack

8)时间表显示度量结果:http://docs.codehaus.org/display/SONAR/Timeline+Plugin

9)度量结果演进图:http://docs.codehaus.org/display/SONAR/Motion+Chart+Plugin

b)插件配置示例(本段内容来自http://www.ibm.com/developerworks/cn/java/j-lo-sonar/

 

 

 

 

 

 

 

 

 

 

 


第二章Maven安装

2.1下载 Maven

最新版:Apache Maven 3.2.1

下载地址   http://maven.apache.org/download.cgi

2.2配置Maven

Maven的配置也比较简单:MAVEN_HOME= ${ Maven.home }  PATH=% MAVEN_HOME %\bin
测试maven是否成功 mvn –v

第三章 Jenkins安装

3.1安装Jenkins

下载地址:http://jenkins-ci.org/content/thank-you-downloading-windows-installer

Jenkins安装比较简单在这里就不多说了,其最主要的就是Jenkins集成MavenSonar

3.2安装后打开http://localhost:9000/进行测试

3.3成功界面:

第四章 Sonar-Runner安装

4.1下载Sonar-Runner

下载地址:http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-runner

4.2配置Sonar-Runner环境变量

1SONAR_RUNNER_HOME            你的安装位置

2Path            ${SONAR_RUNNER_HOME}/bin (Unix) or%SONAR_RUNNER_HOME%/bin

3)修改${SONAR_RUNNER_HOME}/conf/sonar-runner.properties文件,打开database connection, server URL的注释

4.3测试文件的配置

在每个项目的项目源文件目录下新建一个文件名为sonar-project.properties的文件,在文件中输入以下内容:

# required metadata

# My project------修改成你的项目名称

sonar.projectKey=my:project

sonar.projectName=My project

sonar.projectVersion=1.0

# path to source directories (required)

# srcDir1,srcDir2---------修改成你的源文件夹路径

sonar.sources=srcDir1,srcDir2  #通常就填src

# path to test source directories (optional)

# testDir1,testDir2--------修改成你的测试文件夹路径

tests=testDir1,testDir2

# path to project binaries (optional), for example directory of Java#bytecode

# binDir--------修改成你的二进制文件夹路径

# binaries=binDir    #此为可选项,可以不填

# path to project libraries (optional)

#libraries=junit.jar      #此为可选项,可以不填

# advanced parameters

sonar.my.property=value

4.4应用sonar监测代码质量

以上配置完成之后,就可以应用sonar来监测代码质量了。

1)先启动sonarsonar-3.7\bin\windows-x86-64\StartSonar.bat文件,这时可以查看sonar-3.7\logs\ sonar.log文件,看是否已经启动sonar

2)启动好sonar之后,接着在命令行中切换到项目文件的目录下,然后输入sonar-runner,等到运行结束后,进入到http://localhost:9000页面,查看代码的质量统计结果。

需进入项目的目录中:

成功时如图所示:

第五章 环境集成

5.1SonarJenkins集成

5.1.1通过Maven进行集成

修改maven的主配置文件(${MAVEN_HOME}/conf/settings.xml文件或者~/.m2/settings.xml文件),在其中增加访问Sonar数据库及Sonar服务地址,添加如下配置:

 

 

<profile>

<id>sonar</id>

<properties>

<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>

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

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

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

<sonar.host.url>http://localhost:9000</sonar.host.url><!-- Sonar服务器访问地址 -->

</properties>

</profile>

<activeProfiles>

<activeProfile>sonar</activeProfile>

</activeProfiles>

 

此处注意sonar.host.url地址应根据sonar部署情况修改

同样,为了避免内存溢出,推荐增加内存堆栈的大小。设置MAVEN_OPTS环境变量:

set MAVEN_OPTS=”-Xmx512m -XX:MaxPermSize=256m”

使用Sonar

1 运行Sonar服务器;

2 通过 mvn sonar:sonar 将代码注入到Sonar中进行分析处理,并将处理结果以XML的形式保存在数据库中;

3)通过浏览器访问,显示分析结果;

4)持续运行Maven构建,会迭代显示分析结果;

5)可以显式指定sonar插件的版本,如下:

<project>

   <build>

       <plugins>

           <plugin>

               <groupId>org.codehaus.sonar</groupId>

               <artifactId>sonar-maven-plugin</artifactId>

               <version>3.5.1</version>

           </plugin>

      </plugins>

     </build>

</project>

 

6)可以显式的将sonar绑定到Maven生命周期中,如下:

 

<plugin> 

  <groupId>org.codehaus.sonar</groupId>

      <artifactId>sonar-maven-plugin</artifactId>

       <version>3.5.1</version>

       <executions>

          <execution>

            <id>sonar</id>

          <phase>site</phase>

          <goals>

            <goal>sonar</goal>

          </goals>

         </execution>

  </executions>

</plugin>

 

5.1.2下载sonar jenkins plugin

jenkins的插件管理中选择安装sonar jenkins plugin,该插件可以使项目每次构建都调用sonar进行代码度量。

 

打开Jenkins,选择系统管理,然后进入管理插件,找到sonar plugin,下载安装。

 

5.2Jenkins系统配置

5.2.1 JDK配置


5.2.2 sonar runner 
配置

 

5.2.3 Maven配置

5.2.4 Sonar 配置

5.3Jenkins项目配置

进入你创建的项目,我这里创建的项目名是scmp,进入后,找到左边的配置,进入。

5.3.1源码管理

选择你的源码来源,我用的是SVN,选择 Subversion选择框,里面要配置你的源码URL,登录名和密码,其中Repository depth选择infinityLocal moduledirectory是你导出文件的位置,此位置是相对于Jenkins的,在Jenkinsworkspace。如图所示:

5.3.2 Build

其中的root Pom选择是相对于workspace的,即是在workspace里里面。 如图:

5.3.3 Post Steps

如图操作:

选择Maven targets

然后里面的设置如图:

5.3.4 构建后操作

添加构建后操作步骤:

此处只需要填写下JDK就可以用了,应用程序构建时就会自动触发Sonar对代码的检查

 

成功效果图:

第六章 常见错误

6.1sonar没有启动

错误如下:

[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.2:sonar (default-cli) on project scmp: SonarQube server can not be reached at http://localhost:9000. Please check the parameter 'sonar.host.url'. Connection refused: connect -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[JENKINS] Archiving D:\Program Files\Jenkins\workspace\scmp\scmp\pom.xml to com.fnii.scmp/scmp/0.0.1-SNAPSHOT/scmp-0.0.1-SNAPSHOT.pom
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
An attempt to send an e-mail to empty list of recipients, ignored.
channel stopped
Skipping sonar analysis due to bad build status FAILURE
An attempt to send an e-mail to empty list of recipients, ignored.
Finished: FAILURE

 

解决方法:应先运行sonar,在运行Jenkins进行构建项目

6.2 Maven setting.xml配置

错误如下:

 

 [ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.3.1:sonar (default-cli) on project scmp: SonarQube server can not be reached at http://localhost:9000. Please check the parameter 'sonar.host.url'. Connection refused: connect -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
 
 

解决方法:因为Maven中找不到sonar-maven-plugin配置,将下面文件直接替换即可:

 

<?xml version="1.0"encoding="UTF-8"?>

<settingsxmlns="http://maven.apache.org/SETTINGS/1.0.0"           

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

xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd">

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

                <!-- SERVER ON A REMOTE HOST-->                

        <sonar.host.url>http://192.168.5.67:9000</sonar.host.url>

     </properties>

   </profile>

  </profiles>

</settings>

 

6.3 文件POM.xml配置

[ERROR] There are testfailures.

 

Please refer to D:\ProgramFiles\Jenkins\jobs\scmp\workspace\scmp\target\surefire-reports for the individualtest results.

[JENKINS] Recording test results

log4j:WARN No appenders could befound for logger (org.apache.commons.beanutils.converters.BooleanConverter).

log4j:WARN Please initialize thelog4j system properly.

 

pom.xmlbuild标签中添加下面内容:

         <plugins>  

       <plugin>  

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

        <artifactId>maven-surefire-plugin</artifactId>  

        <version>2.4.2</version>  

         <configuration>  

          <skipTests>true</skipTests>  

        </configuration>  

       </plugin>  

     </plugins>

 

6.4 0 files indexd

运行Jenkins构建时出现检测不到文件:0 files indexd时,该错误的出现的可能原因有两个:

1.数据库出现问题,把相关数据库中表删除,重新构建。

2.导出时Repository depth选择错误,应当选择infinity,将工程中.repository文件删除,重新构建


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值