这两天项目组做web开发基础框架,需要集成sonar代码分析。
之前也用过sonar,只不过代码分析需要人工触发或者设置linux定时任务,本次web基础框架同时集成了bamboo日构建,故此想在每次代码提交之后即触发代码分析。
阅读这篇笔记假设你已经搭建好了sonar服务,同时安装好了maven。
1.修改maven的配置文件setting.xml,这个文件一般会放在~/.m2或者$MAVEN_HOME/conf目录下。
2.打开这个文件在对应的为知里面添加如下配置。
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.jdbc.url>jdbc:postgresql://10.20.0.171:5432/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>postgres</sonar.jdbc.username>
<sonar.jdbc.password></sonar.jdbc.password>
<sonar.host.url>http://10.20.0.171:9000</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
在此提醒一点,maven-sonar-plugin的会将分析完结果插入sonar指定的数据库中,所以在此需要保证数据库可以外网访问。postgresql为例,
a.更改postgressql.conf
listen_addresses = '*'
b.更改 pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 trust
3. 执行代码分析
mvn clean verify sonar:sonar
Ops!!!出错了!
Ops!!!出错了!
Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar
换个命令执行mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar
Ops!!!出错了!Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar (default-cli) on project ruma-web: 'sonar.projectDate' property cannot be older than the date of the last known quality snapshot on this project. Value: '2016-03-17T06:35:16+0000'. Latest quality snapshot: '2016-03-17T14:13:01+0000'. This property may only be used to rebuild the past in a chronological order. -> [Help 1]
搞咩啊,时间不对也不行啊,一看服务器的时间果然不对
尼玛,调整时间再试试吧。
参考我之前的笔记linux时区调整
vi /etc/profile
add line:
export TZ='Asia/Shanghai';
save and exit
source /etc/profile
继续执行
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar
等待了良久,喝了杯茶(twinings)…..
4.终于成功了
问题真不少啊。
qq:986737506
email:ding_cuiping@163.com