Sonar初探

一。什么是Sonar

Sonar 一个开源平台,用于管理Java源代码的质量。“一个质量数据报告工具+代码质量管理平台”http://docs.codehaus.org/display/SONAR/Documentation

主要特点

·                       代码覆盖:通过单元测试,将会显示哪行代码被选中

·                       改善编码规则

·                       搜寻编码规则:按照名字,插件,激活级别和类别进行查询

·                       项目搜寻:按照项目的名字进行查询

·                       对比数据:比较同一张表中的任何测量的趋势

 

整体架构如下: 


Sonar项目分析页面预览:


二.安装:

1.下载压缩包,在win/linux下面均可以运行。

自身集成jetty+java内存DB,一键启动。默认启动到http://localhost:9000

正式使用可以配置mysqlDB做持久存储。当然容器也可以切换成tomcat 具体看官站文档。

 

NoticeDeploy on Tomcat application serverA minimum heap size of 512Mo is required. 吃内存大户

 

 

2.环境依赖:

JDK5++

Maven2.09 ++

 

三。怎么分析项目

1maven项目:(简单的讲就是带了pom.xml)


 E:\>cd ws1\test 然后运行:

     1.mvn clean install -Dtest=false -DfailIfNoTests=false (第一步其实可以忽略)
        2. mvn sonar:sonar

之后再回到页面上,查看项目分析结果。 (第2步的mvn操作其实可以集成到pom.xml里面作为一个

 

2)非maven项目:

方法:给项目放一个pom.xml,不需要写复杂的依赖和编译操作,简单copy如下配置,改改项目名和java代码的目录路径即可!(标红)

 

<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>song</groupId>

  <artifactId>test</artifactId>

  <name>test</name>

  <version>1.1.0</version>

  <build>

        <sourceDirectory>src</sourceDirectory>

        <outputDirectory>bin</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>

 

详细文档在:http://docs.codehaus.org/display/SONAR/Collect+data#Collectdata-Mavenprojects

 

 

3)将sonar集成到maven

编辑mavenconfig目录下的setting文件activeByDefaultExample

 

<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.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
                <sonar.jdbc.username>sonar</sonar.jdbc.username>
                <sonar.jdbc.password>sonar</sonar.jdbc.password>
                <!-- SERVER ON A REMOTE HOST -->
                <sonar.host.url>http://myserver:1234 </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>

 

当然,这样做的代价就是:export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=256m" maven编译的内存也需要加到512M

四。总结

1.作为代码分析工具来使用,其实很简单。下载standalone包直接运行,不需要配置db和容器,直接找到项目分析就是。(用maven管理的项目直接分析,非maven项目按上面的方法加一个最精简的pom.xml配置即可。sonar分析代码的时候并不需要复杂的jar包依赖,指定了源代码目录即可

2.作为持续性的代码监控,比如关心代码量的变更,和质量走向,那么配置DB就是必须的了。而且要准备1.5G+空闲内存的机器跑sonar,在触发到代码分析的时候,对CPU的消耗也是比较大的。

3.将sonar集成到持续集成是非常不明智的。会导致每次编译都去分析一次。比较建议做成crontab的脚本,一个星期用定时脚本启动、分析那么一两回就可以了。

 

猜你喜欢

转载自sw1982.iteye.com/blog/705298
今日推荐