当前位置:文档之家› sonar+maven2使用说明(最终版)

sonar+maven2使用说明(最终版)

一、sonar环境搭建1、安装JDK-1.5以上版本。

在环境变量中配置JAVA_HOME,并在path里添加%JAVA_HOME%\bin;2、安装mysql-5.x以上版本。

3、mysql新建数据库并增加权限CREATE DATABASE sonar CHARACTER SET utf8 COLLATEutf8_general_ci;GRANT all ON sonar.* TO sonar@localhost IDENTIFIED BY ‘sonar’; FLUSH PRIVILEGES ;4、在sonar官网上下载并解压sonar-2.8.zip,不要放在中文目录下。

5、配置sonar-2.8\conf\sonar.properties文件:1)配置启动的http端口sonar.web.host: localhostsonar.web.port: 9000sonar.web.context: /三句前本来被注释,取消注释2)注释掉Derby数据库绑定(49、50行)#sonar.jdbc.url:jdbc:derby://localhost:1527/sonar;create=true#sonar.jdbc.driverClassName:org.apache.derby.jdbc.ClientDriver3)取消mysql连接的注释#----- MySQL 5.x/6.x# Comment the embedded database and uncomment the following properties to use MySQL. The validation query is optional. sonar.jdbc.url:jdbc:mysql://localhost:3306/sonar?useUnicode=true&character Encoding=utf8sonar.jdbc.driverClassName:com.mysql.jdbc.Driversonar.jdbc.validationQuery: select 1运行sonar-2.8\bin\windows-x86-32\StartSonar.bat,打开相应的网页:http://localhost:9000测试是否配置成功,这里的页面链接跟前头的http配置有关第三条,也就是说,把sonar.properties 文件修改成以下内容:# Listen host/port and context path (for example / or /sonar). Default values are localhost:9000/.sonar.web.host: localhostsonar.web.port: 9000sonar.web.context: /ername: sonarsonar.jdbc.password: sonar#----- MySQL 5.x/6.x# Comment the embedded database and uncomment the following properties to use MySQL. The validation query is optional. sonar.jdbc.url:jdbc:mysql://localhost:3306/sonar?useUnicode=true&character Encoding=utf8sonar.jdbc.driverClassName:com.mysql.jdbc.Driversonar.jdbc.validationQuery: select 1#----- Connection pool settingssonar.jdbc.maxActive: 10sonar.jdbc.maxIdle: 5sonar.jdbc.minIdle: 2sonar.jdbc.maxWait: 5000sonar.jdbc.minEvictableIdleTimeMillis: 600000sonar.jdbc.timeBetweenEvictionRunsMillis: 30000二、maven环境搭建1、到maven官网/download.html去下载maven2.x版本。

解压文件到非中文目录下。

2、配置MAVEN_HOME环境变量,在path里添加%MAVEN_HOME%\bin;在命令行输入mvn –h测试MAVEN环境是否配置正确。

配置正确后,开始使用。

三、maven+sonar测试1、在%MAVEN_HOME%\conf\setting.xml中输入下面内容(直接用下面的内容覆盖原文件):<?xml version="1.0" encoding="UTF-8"?><settings xmlns="/SETTINGS/1.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/SETTINGS/1.0.0 /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;chara cterEncoding=utf8</sonar.jdbc.url><sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jd bc.driverClassName><ername>sonar</ername><sonar.jdbc.password>sonar</sonar.jdbc.password><!-- SERVER ON A REMOTE HOST --><sonar.host.url>http://localhost:9000</sonar.host.url></properties></profile></profiles></settings>2、在项目源文件夹下,创建pom.xml文件,输入以下内容:<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.maven</groupId><!--项目包路径--><artifactId>maventest</artifactId><!--项目名称--><name>maven@sonar--test</name><!--maven项目显示名称--> <url></url><version>maven@sonar-test1.1</version><!--版本信息--> <build><sourceDirectory>src</sourceDirectory><!--项目文件目录--><outputDirectory>bin</outputDirectory><!--项目文件相关class文件目录--><testSourceDirectory>test</testSourceDirectory><!--测试类文件目录--><testOutputDirectory>bin</testOutputDirectory><!--测试类相关class文件目录--><resources><!--项目资源文件目录--><resource><mergeId>resource-0</mergeId><directory>src\resources</directory></resource></resources><testResources><!--测试资源文件目录--><testResource><mergeId>resource-1</mergeId><directory>test\resources</directory></testResource></testResources><plugins><plugin><!—解决覆盖率为0的问题--><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.3</version><configuration><includes><include>**/*Test*.java</include></includes></configuration></plugin><plugin><!—解决不支持泛型问题--><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target></configuration></plugin></plugins></build><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEn coding><!--字符集设置--></properties><dependencies><dependency><groupId>junit</groupId><!--单元测试依赖文件--><artifactId>junit</artifactId><version>3.8.1</version><!--单元测试版本--><scope>test</scope></dependency></dependencies></project>四、测试1、先运行%SONAR_HOME%\bin\windows-x86-32\StopNTService.bat (如果是第一次使用)2、启动sonar在%SONAR_HOME%\bin\windows-x86-32\StartSonar.bat,等到启动完成,进入下一步3、在命令行进入到项目文件的目录中去,然后再输入mvn sonar:soanr进行测试4、在浏览器中输入http://localhost:9000查看结果。

相关主题