如何让SonarQube运行来分析PHP项目



我想评估SonarQube作为源代码检查工具。

项目托管在git存储库中,我希望SonarQube在每次提交时检查我的PHP项目

我通过docker得到了SonarQube的一个基本实例。(当前版本的声纳立方体是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组件通过:

设置>系统>更新中心

(or: http://localhost:9000/updatecenter)

和搜索PHP和安装。

在那里我可以添加PHP和重启SonarQube服务器后,(我通过docker stop container_id, container start container_id做的),扩展被加载。

服务器不会运行您的测试。它只会显示结果。

你需要一台专用的机器来作为你的sonar-runner,为了快速开始,你可以使用你的本地开发机器和从bitbucket的本地结帐。在那台机器上安装声纳运行器。

通过:

下载声纳运行器
 $ 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仪表板上看到一个新条目。

获取以下内容:

  • OpenJDK: 7u55+ of 8
  • MySQL: 5.1 t/m 5.7
  • SonarQube: http://dist.sonar.codehaus.org/sonarqube-5.1.zip
  • 声纳运行器:http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

执行以下操作:

  • 安装:http://docs.sonarqube.org/display/SONAR/Installing

设置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

最新更新