10

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

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:

add in my travis.yml file

 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 here From SonarCloud I add permissions enter image description here

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

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.

slartidan
  • 20,403
  • 15
  • 83
  • 131
Xelian
  • 16,680
  • 25
  • 99
  • 152
  • What is the "official documentation" you're talking about? If you look at http://about.sonarqube.com/get-started/, you'll find the right path for Maven projects, and it does not mention using "sonar-project.properties" at all. – Fabrice - SonarSource Team Sep 29 '17 at 06:45

2 Answers2

10

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

  • You will see that for Maven projects, you don't need to create a sonar-project.properties file at all

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

slartidan
  • 20,403
  • 15
  • 83
  • 131
  • Hello Fabrice, there's a related issue some users are getting with the Maven execution. More about this issue: https://stackoverflow.com/q/50012220/2073804 https://github.com/travis-ci/travis-ci/issues/7814 https://github.com/travis-ci/travis-ci/issues/7182 – ron190 Apr 25 '18 at 00:31
  • See my answer on your post: you are doing something wrong, everything is working correctly as expected. – Fabrice - SonarSource Team Apr 25 '18 at 06:04
0

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

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:

  # 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.

Xelian
  • 16,680
  • 25
  • 99
  • 152