如何配置 SonarCloud

How to configure SonarCloud

我有java项目,我想将它与SonarCloud集成我按照官方步骤:

Inspecting code with the SonarQube Scanner #

Before inspecting your code, you need to:

  1. Create a user authentication token for your account on SonarCloud.
  2. Encrypt this token travis encrypt abcdef0123456789 or define SONAR_TOKEN in your Repository Settings
  3. Find which SonarCloud.io organization you want to push your project on and get its key
  4. Create a sonar-project.properties file for your project (see the documentation). Then add the following lines to your .travis.yml file to trigger the analysis:

添加到我的 travis.yml 文件中

 addons:
  sonarcloud:
    organization: "xelian-github"
    token:
      secure: ${SONAR_TOKEN}
    branches:
      - master
script:
  # other script steps might be done before running the actual analysis
  - sonar-scanner

其中 SONAR_TOKEN 是 Travis CI 上的一个变量,指向来自 SonarCloud 的密钥。(它未加密)。 从 SonarCloud 我添加权限

但是当我开始构建 travis 时出现以下错误:

Setting environment variables from repository settings
$ export SONAR_TOKEN=[secure]

 ....
ERROR: Error during SonarQube Scanner execution
ERROR: You're only authorized to execute a local (preview) SonarQube analysis without pushing the results to the SonarQube server. Please contact your SonarQube administrator.
ERROR: 
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

在我看来,我的 travis 没有权限将结果上传到 SonarCloud。是token的问题还是某些Sonar配置的问题。

终于找到解决办法了。在 yml 文件的根路径中,您必须添加:

sonar-project.properties

# Required metadata
sonar.projectKey=java-sonar-runner-simple:master
sonar.projectName=Rss-service
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=/microservice-application/rss-reader-service/src/main/java
sonar.java.binaries=/microservice-application/rss-reader-service/target/classes

# Language
sonar.language=java

# Encoding of the source files
sonar.sourceEncoding=UTF-8

并在 travis.yml 中添加: 脚本:

  # other script steps might be done before running the actual analysis
  - mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar

编辑:

sonar-project.properties 不需要。只有 Maven 目标才有意义。

在 SonarCloud 上配置项目的官方入口点是 "Get Started" page:

  • 您将看到对于 Maven 项目,您根本不需要创建sonar-project.properties文件

  • 你甚至会发现在 SonarCloud

    上分析的 link 到 sample Maven project