安装好sonar后,跑sonar有三种方式
1. 通过maven插件跑,这个很简单,只需要简单的修改maven的settings文件就好了,在<profiles>下面增加
<profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:oracle:thin:@IP:1521:cpadb</sonar.jdbc.url> <sonar.jdbc.driver>oracle.jdbc.OracleDriver</sonar.jdbc.driver> <sonar.jdbc.username>sonar</sonar.jdbc.username> <sonar.jdbc.password>sonar</sonar.jdbc.password> <sonar.host.url>http://IP:48080/sonar/</sonar.host.url> </properties> </profile>
然后运行sonar:sonar命令就可以了,Maven 插件会自动把所需数据(如单元测试结果、静态检测结果等)上传到 Sonar 服务器上。
2. 通过eclipse的sonar插件
详细安装方法请参考这里 http://docs.codehaus.org/display/SONAR/Installing+Sonar+in+Eclipse
插件地址是http://dist.sonar-ide.codehaus.org/eclipse/
详细的配置请参考 http://docs.codehaus.org/display/SONAR/Configuring+Sonar+in+Eclipse
但是通过插件不能创建project,只能连接到之前在sonar上创建好的项目,所以需通过第一种方式创建出project,或是通过下面的第三种方式创建,有了project后就可以用这个插件连接到project来分析,插件的好处是在eclipse里面就可以直接看到分析结果,方面修改代码。
3. 通过sonar runner 客户端来运行project
下载地址是http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner 同时,这篇文章里面还有详细的安装说明,不过是英文的,我简单翻译一下。
1)下载后解压到$SONAR_RUNNER_HOME 目录。
2)修改 $SONAR_RUNNER_HOME/conf/sonar-runner.properties 里面的配置
#----- Default Sonar server#sonar.host.url=http://localhost:9000 #----- PostgreSQL#sonar.jdbc.url=jdbc:postgresql://localhost/sonar#sonar.jdbc.driver=org.postgresql.Driver #----- MySQL#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8#sonar.jdbc.driver=com.mysql.jdbc.Driver #----- Oracle#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE#sonar.jdbc.driver=oracle.jdbc.driver.OracleDriver #----- Global database settings#sonar.jdbc.username=sonar#sonar.jdbc.password=sonar3)创建环境变量SONAR_RUNNER_HOME, 指向安装目录
4)增加$SONAR_RUNNER_HOME/bin到path.
5) 运行可参考 http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Runner, 我简单翻译一下
a) 在项目目录下创建sonar-project.properties
# required metadatasonar.projectKey=my:projectsonar.projectName=My projectsonar.projectVersion=1.0 # optional descriptionsonar.projectDescription=Fake description # path to source directories (required)sonar.sources=srcDir1,srcDir2,./scr/main/java # path to test source directories (optional)#sonar.tests=testDir1,testDir2 # path to project binaries (optional), for example directory of Java bytecode#sonar.binaries=binDir # optional comma-separated list of paths to libraries. Only path to JAR file is supported.#sonar.libraries=path/to/library/*.jar,path/to/specific/library/myLibrary.jar,parent/*/*.jar # The value of the property must be the key of the language.#sonar.language=cobol # Additional parameterssonar.my.property=value
然后在项目根目录下运行sonar-runner