I tried to run sonarqube running on docker for my Spring Boot Project. I have a problem to get sonarqube result.
Here is my sonarqube plugin in my pom.xml
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar-maven-plugin.version}</version>
</plugin>
Here is the docker-compose.yml
services:
postgres:
image: postgres:latest
container_name: postgres-container
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=weatherapianalysisdatabase
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- weatherapianalysis_network
pgadmin:
image: dpage/pgadmin4:latest
container_name: pgadmin-container
restart: always
ports:
- "5050:80"
environment:
- PGADMIN_DEFAULT_EMAIL=admin@pgadmin.com
- PGADMIN_DEFAULT_PASSWORD=admin
depends_on:
- postgres
networks:
- weatherapianalysis_network
weatherapianalysis:
image: 'weatherapianalysis:latest'
build:
context: .
dockerfile: Dockerfile
container_name: weatherapianalysis
restart: on-failure
env_file:
- .env # Use the .env file for environment variables
ports:
- "1100:1100"
environment:
- server.port=1100
- WEATHER_DB_IP=postgres
- WEATHER_DB_PORT=5432
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- OPEN_WEATHER_API_KEY=${OPEN_WEATHER_API_KEY:-open_weather_api_default_key}
depends_on:
- postgres
networks:
- weatherapianalysis_network
sonarqube:
image: sonarqube:latest
container_name: sonarqube
restart: always
ports:
- "9000:9000"
environment:
- SONAR_JDBC_URL=jdbc:postgresql://postgres:5432/weatherapianalysisdatabase
- SONAR_JDBC_USERNAME=${POSTGRES_USER}
- SONAR_JDBC_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
depends_on:
- postgres
networks:
- weatherapianalysis_network
volumes:
postgres_data:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
networks:
weatherapianalysis_network:
Here is thr sonar-project.properties
# Project identification
sonar.projectKey=weatherapianalysis
sonar.projectName=Weather API Analysis
sonar.projectVersion=1.0
# SonarQube Server
sonar.host.url=http://localhost:9000
# Authentication Token (replace with your token from SonarQube)
sonar.token=token
# Language & Source Code
sonar.language=java
sonar.sources=src
sonar.tests=src/test
sonar.sourceEncoding=UTF-8
sonar.java.binaries=target/classes
After I created token through My Account > Security > Generate a Token , I copied the new token and updated sonar-project.properties
When I run this command mvn clean verify sonar:sonar
, I got this issue shown below
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:5.0.0.4389:sonar (default-cli) on project weatherapianalysis: Error status returned by url [https://
api.sonarcloud.io/analysis/jres?os=windows&arch=amd64]: 401 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
How can I fix the issue?
After I defined sonar properties in pom.xml shown below instead of using sonar-project.properties, the issue disappeared.
<sonar.host.url>http://localhost:9000</sonar.host.url> <!-- Changed to localhost if running locally -->
<sonar.login>{sonar.login}</sonar.login> <!-- Authentication Token -->
<sonar.projectKey>{sonar.projectKey}</sonar.projectKey>
<sonar.projectName>{sonar.projectName}</sonar.projectName>
<sonar.projectVersion>1.0</sonar.projectVersion>
<sonar.sources>src/main</sonar.sources>
<sonar.tests>src/test</sonar.tests>
<sonar.sourceEncoding>UTF-8</sonar.sourceEncoding>
<sonar.java.binaries>target/classes</sonar.java.binaries>