java 如何配置 SonarCloud

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46462540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:14:02  来源:igfitidea点击:

How to configure SonarCloud

javaencryptionsonarqubetravis-cisonarcloud

提问by Xelian

I have java project and I want to integrate it with SonarCloud I Follow the official steps:

我有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:

使用 SonarQube 扫描仪检查代码 #

在检查代码之前,您需要:

  1. 在 SonarCloud 上为您的帐户创建用户身份验证令牌。
  2. 加密此令牌 travis encrypt abcdef0123456789 或在您的存储库设置中定义 SONAR_TOKEN
  3. 找到您想要推动项目的 SonarCloud.io 组织并获取其密钥
  4. 为您的项目创建一个 sonar-project.properties 文件(请参阅文档)。然后将以下行添加到您的 .travis.yml 文件以触发分析:

add in my travis.yml file

添加到我的 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

Where SONAR_TOKEN is a variable on Travis CI pointing to the key from SonarCloud.(It is not encrypted). enter image description hereFrom SonarCloud I add permissions enter image description here

其中 SONAR_TOKEN 是 Travis CI 上的一个变量,指向来自 SonarCloud 的密钥。(未加密)。 在此处输入图片说明从 SonarCloud 我添加权限 在此处输入图片说明

But when I start the travis build I have the following error:

但是当我开始 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.

It seems to me that I the travis do not have permissions to upload results to SonarCloud. Is the problem in the token or in some Sonar configurations.

在我看来,我的 travis 无权将结果上传到 SonarCloud。是令牌的问题还是某些声纳配置的问题。

回答by Fabrice - SonarSource Team

The official entry point to configure a project on SonarCloud is the "Get Started" page:

在 SonarCloud 上配置项目的官方入口点是“入门”页面

  • You will see that for Maven projects, you don't needto create a sonar-project.propertiesfile at all

  • You will even find a link to a sample Maven projectthat is analyzed on SonarCloud

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

  • 您甚至会找到在 SonarCloud 上分析的示例 Maven 项目的链接

回答by Xelian

Finally I find a solution. In the root path whete the yml file is you have to add:

最后我找到了解决方案。在 yml 文件的根路径中,您必须添加:

sonar-project.properties

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

And in the travis.yml I add: script:

在 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

Edit:

编辑

sonar-project.properties not necessary. Only maven goals make sense.

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