osc 有在线的 sonar 服务不错,不过毕竟在公网,自己想用还是内网搭一个更合适.
首先准备一台机器,安装好,jdk(JAVA_HONE),maven,数据库
下载解压sonar-runner和sonarqube
将maven和sonar-runner加入path
修改sonar-runner和sonarqube的conf/sonar-runner.properties和onf/sonar.properties
修改里面的数据库相关参数
在maven的.m2的settings.xml加入
sonar
true
jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
com.mysql.jdbc.Driver
root
root
http://localhost:9000
在java项目的pom中加入: 注意4.4是sonarqube的版本号,必须一致
org.codehaus.sonar
sonar-maven-plugin
4.4
sonar
site
sonar
在项目路径下执行 mvn install 和 mvn sonar:sonar 然后慢慢等,注意最后一步的入库操作需要很多时间.
安装sonar的eclipse插件,将项目和之前mvn sonar:sonar生成的项目关联上
错误处理:
异常:
mvn sonar:sonar时报错:
[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.4:sonar (d
efault-cli) on project mad.adserver: Unable to save some measures:
[ERROR] ### Error committing transaction. Cause: org.apache.ibatis.executor.Bat
chExecutorException: org.sonar.api.database.model.MeasureMapper.insert (batch in
dex #1) failed. Cause: java.sql.BatchUpdateException: Packet for query is too la
rge (21923518 > 4194304). You can change this value on the server by setting the
max_allowed_packet' variable.
[ERROR] ### Cause: org.apache.ibatis.executor.BatchExecutorException: org.sonar.
api.database.model.MeasureMapper.insert (batch index #1) failed. Cause: java.sql
.BatchUpdateException: Packet for query is too large (21923518 > 4194304). You c
an change this value on the server by setting the max_allowed_packet' variable.
解决办法:
修改 my.ini 加上 max_allowed_packet = 40M
登陆mysql,运行
show VARIABLES like '%max_allowed_packet%';
确认生效即可.
异常:
窗口方式启动sonarqube能正常打开9000端口,服务方式打开报错,日志显示:
WrapperSimpleApp: Encountered an error running main: java.lang.IllegalStateException: Unable to create file in temporary directory, please check existence and permissions of: C:\Windows\system32\config\systemprofile\AppData\Local\Temp
java.lang.IllegalStateException: Unable to create file in temporary directory, please check existence and permissions of: C:\Windows\system32\config\systemprofile\AppData\Local\Temp
at org.sonar.application.Env.verifyWritableTempDir(Env.java:69)
at org.sonar.application.StartServer.
(StartServer.java:32)
at org.sonar.application.StartServer.main(StartServer.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:240)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(Unknown Source)
at java.io.File.createTempFile(Unknown Source)
at org.sonar.application.Env.verifyWritableTempDir(Env.java:67)
... 8 more
解决办法:
服务和窗口启动程序时,临时目录和用户目录等环境,是不一致的.
手工创建C:\Windows\system32\config\systemprofile\AppData\Local\Temp这个目录即可