如何配置SonarCloud

时间:2017-09-28 06:40:44

标签: java encryption sonarqube travis-ci sonarcloud

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

  

使用SonarQube扫描仪#

检查代码      

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

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

添加我的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的密钥。(它未加密)。 enter image description here 从SonarCloud我添加权限 enter image description here

但是当我开始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。是令牌中还是某些Sonar配置中的问题。

2 个答案:

答案 0 :(得分:7)

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

  • 您会看到,对于Maven项目,您根本不需要来创建public string MeasureVoltage() { string Meas = "MEAS:VOLT:"; if (vt == VoltageType.DC) { Meas += "DC?"; } else { Meas += "AC?"; } io.PrintfAndFlush(Meas + "\n"); Console.WriteLine(Meas); string response; io.Scanf("%s", out response); return response; } public string MeasureCurrent() { string Meas = "MEAS:CURR:"; if (ct == CurrentType.DC) { Meas += "DC?"; } else { Meas += "AC?"; } io.PrintfAndFlush(Meas + "\n"); Console.WriteLine(Meas); string response; io.Scanf("%s", out response); return response; } public enum VoltageType { AC, DC } public enum CurrentType { AC, DC } 文件

  • 您甚至可以找到在SonarCloud上分析的sample Maven project的链接

答案 1 :(得分:0)

最后我找到了解决方案。在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目标才有意义。