您的位置:首页 > 编程语言

代码质量管理平台sonar

2011-08-02 10:39 363 查看
Sonar是一个开源平台,用于管理Java源代码的质量。从 Sonar 1.6 版本开始,Sonar从一个质量数据报告工具,转变成为现在的代码质量管理平台。

安装前提:

系统必须支持Java和Maven

数据库支持:

默认使用自带的Derby数据库

Mysql

Oracle

MS SqlServer

PostgreSQL

Sonar处理步骤:

1、mvn sonar:sonar 把代码注入到Sonar中进行分析处理,并将处理结果以XML的形式插入到DB中

2、通过浏览器访问,显示分析结果

3、持续运行Maven构建,会迭代显示分析结果

一分钟安装教程:

1、下载并解压

2、在Windows上安装

Cmd代码







bin/windows-x86-32StartSonar.bat

bin/windows-x86-32StartSonar.bat

3、 其它系统安装

Cmd代码







bin/[OS]/sonar.sh console

bin/[OS]/sonar.sh console


4、Maven工程中运行

Cmd代码







mvn clean install sonar:sonar

mvn clean install sonar:sonar

5、浏览器打开 http://localhost:9000
6、进入后台,admin/admin

5步骤安装:

第一步,创建数据库

Apache Derby 是Sonar默认安装的数据库,并且不需要你安装。它能很好的用于Sonar的演示,但是在实际运用中我推荐你使用性能更好更强大的数据库。Sonar对如下数据库提供支持:MySQL 5.x, Oracle 10g XE, Postgresql 和MS SqlServer 2005。

第一件事就是为Sonar创建一个数据库。表和索引会在Sonar激活后自动创建。
同时要给Sonar用户能够在数据库表中创建、禁止和更新对象的权限。

第二步,安装服务器

第三步,配置数据库

如果你不是使用默认的数据库,那么你可以编辑conf/sonar.properties配置数据库访问权限。注释derby的配置并复制一份自定义来修改,下面是Sonar的数据库配置模板:

Properties代码







sonar.jdbc.url : 数据库URL sonar.jdbc.driver : 驱动类 sonar.jdbc.user : 用户名 默认sonar sonar.jdbc.password : 密码 默认sonar

sonar.jdbc.url :  数据库URL
sonar.jdbc.driver :  驱动类
sonar.jdbc.user : 用户名 默认sonar
sonar.jdbc.password : 密码 默认sonar

Mysql示例:

Properties代码







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

#sonar.jdbc.driver: 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.driver: com.mysql.jdbc.Driver
sonar.jdbc.validationQuery: select 1

#sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
#sonar.jdbc.driver: 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.driver: com.mysql.jdbc.Driver
sonar.jdbc.validationQuery: select 1

如果是Oracle,你必须手动复制JDBC驱动类到/extensions/jdbc-driver/oracle/目录下。其它支持的数据库都已提供了驱动。

第四步,启动Sonar Server

方式一-单独启动

Sonar默认的端口是“9000”,默认的上下文路径是“/”,默认的网络接口是:“0.0.0.0”。一旦激活,Sonar服务器就可以使用http://localhost:9000 。这些参数都可以在conf/sonar.properties修改。下面提供一个http://localhost:80/sonar 的示例:

Properties代码







sonar.web.host : 192.0.0.1

sonar.web.port: 80
sonar.web.context: /sonar

sonar.web.host : 192.0.0.1
sonar.web.port: 80
sonar.web.context: /sonar

可以通过如下脚本启动Sonar服务器:

Cmd代码







linux/mac : bin/[YOUR PLATEFORM]/sonar.sh start
OR
windows : bin/windows-x86-32/StartSonar.bat

linux/mac : bin/[YOUR PLATEFORM]/sonar.sh start
OR
windows : bin/windows-x86-32/StartSonar.bat

同样你可以启动bin/windows-x86-32/InstallNTService.bat把它注册为一个Window服务
,然后再启动bin/windows-x86-32/StartSonar.bat

方式二-部署到Tomcat

打包步骤如下:

编辑conf/sonar.properties还原成标准格式(就是不修改端口之类)。确保部署到应用服务器时conf/wrapper.conf未被使用过。

在war/目录下执行build-war.sh脚本(Windows下执行build-war.bat)。

部署war/sonar.war到应用服务器。

通过http://loaclhost:8080/sonar 访问,继续安装步骤。

为了避免内存溢出,增加内存堆栈的大小。在Tomcat启动前设置CATALINA_OPTS环境变量:

Xml代码







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

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


第五步:配置到Maven中

因为Sonar是通过Maven2 插件来分析源代码并把结果注入到数据库的。这就是为什么我们必须在Maven的配置里设置数据库的属性。编辑位于$MAVEN_HOME/conf或者 ~/.m2下的settings.xml文件,然后在添加step 3中的参数。

如果数据库和服务器不在同一台主机,你还必须通过’sonar.host.url’指定服务器地址。

Xml代码







<settings>

<profiles>

<profile>

<id>sonar</id>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

<properties>

<!– mysql –>

<sonar.jdbc.url>

jdbc:mysql://localhost:3306/sonar?useUnicode=true&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>

<!– remote host –>

<sonar.host.url>http://myserver:1234</sonar.host.url>

</properties>

</profile>

</profiles>

</settings>

<settings>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!– mysql –>
<sonar.jdbc.url>
jdbc:mysql://localhost:3306/sonar?useUnicode=true&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>
<!– remote host –>
<sonar.host.url>http://myserver:1234</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>

注意: <sonar.host.url>属性不能以”/”结尾。否则,Maven Sonar插件将报找不到驱动类的错误。

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

Cmd代码







export MAVEN_OPTS=”-Xmx512m -XX:MaxPermSize=256m”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: