【发布时间】:2015-09-04 16:28:23
【问题描述】:
我想将 SonarQube 评估为源代码检查工具。
该项目托管在一个 git 存储库中,我希望 SonarQube 在每次提交时检查我的 PHP 项目。
【问题讨论】:
标签: php sonarqube sonar-runner
我想将 SonarQube 评估为源代码检查工具。
该项目托管在一个 git 存储库中,我希望 SonarQube 在每次提交时检查我的 PHP 项目。
【问题讨论】:
标签: php sonarqube sonar-runner
我得到了SonarQube via docker 的基本实例。 (声纳立方体的当前版本是6.7——但我不知道步骤是否保持不变。这个答案考虑到5.1。)
运行容器:
sudo docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:5.1
这提供了 SonarQube 的标准安装,可在以下位置访问
http://localhost:9000/
我可以通过用户名和密码admin登录,并通过以下方式安装PHP组件:
设置 > 系统 > 更新中心
(或:http://localhost:9000/updatecenter)
然后搜索 PHP 并安装。
在那里我可以添加 PHP,并且在 SonarQube 服务器重新启动后(我是通过 docker stop container_id、container start container_id 完成的),加载了扩展。
服务器不会运行您的测试。它只会显示结果。
您将需要一台专门用作sonar-runner 的机器,为了快速入门,您可以使用本地开发机器和从 bitbucket 进行本地结帐。在该机器上安装 sonar-runner。
通过以下方式下载声纳跑步者:
$ wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
并将其提取到:
~/programs/sonar-runner-2.4
在此目录中,您可以找到一个文件conf/sonar-runner.properties,其中应包含:
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://localhost:9000
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
sonar.login=admin
sonar.password=admin
进入项目的根目录并创建一个名为sonar-project.properties的文件:
# must be unique in a given SonarQube instance
sonar.projectKey=yourProjectKey
# this is the name displayed in the SonarQube UI
sonar.projectName=yourProject
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=./classes/,./tests/
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
然后我跑了:
your/projects/dir$: ~/programs/sonar-runner-2.4/bin/sonar-runner
然后,您将在 SonarCube 仪表板上看到一个新条目。
【讨论】:
获取以下内容:
执行以下操作:
设置 SonarRunner:
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://14.3.1.4:9000
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://14.3.1.2:3306/sonarqube? useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----- Global database settings
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
#sonar.login=admin
#sonar.password=admin
在 sonar 所在服务器的 var/www 文件夹中克隆一个 git repo。
然后在您要检查的项目中添加一个名为sonar-project.properties 的配置文件。这是一个 Symfony 示例:
# Required metadata
sonar.projectKey=yoursite.dev.nl.project
sonar.projectName=Project
sonar.projectVersion=1.0
# Comma-separated paths to directories with sources (required)
sonar.projectBaseDir=/var/www/your_project
# Folder being analysed.
sonar.sources=symfony/src
# Language (Only when it is a single language)
sonar.language=php
# Encoding of the source files
sonar.sourceEncoding=UTF-8
【讨论】: