Running SonarQube with Docker will result in an error if the specified projectName contains spaces

When the parameter projectName contains spaces, the sonar check will fail, probably treating the content after the spaces as the main class.

Here is my scripts:

randomProjectKey="$1"
codePath="$2"
logFile="$3"
randomProjectName="$4"
{
docker run 
--rm \
-e SONAR_HOST_URL="http://192.168.1.111:9000/" \
-e SONAR_SCANNER_OPTS="-Dsonar.projectKey=$randomProjectKey -Dsonar.projectName=$randomProjectName -Dsonar.java.binaries=/usr/src/sonar-check-code-path/target/classes -Dsonar.exclusions=**/*.js -Dsonar.sources=/usr/src/sonar-check-code-path" \
-e SONAR_TOKEN="13a87dd0b9d1d284a5e5b0437894da53d02d992a" \
-v "$codePath:/usr/src/sonar-check-code-path" \
--network mynetwork \
sonarsource/sonar-scanner-cli
} > "$logFile" 2>&1

Here is the error log:
Project Key: 14cca001-7cf4-4424-bab9-feeba2854d9e Code Path: /data/atm/source_code/f5b845a8727bb963819530979d209792/1688 LogFile:/data/atm/source_code/f5b845a8727bb963819530979d209792/1688/sonarCheckLog.log Project Name: GTS_V1.3.0 20241009_munet_3_20241104666 Error: Could not find or load main class 20241009_munet_3_20241104666' Caused by: java.lang.ClassNotFoundException: 20241009_munet_3_20241104666'

help me, please.
thx

Hey there.

Wrapping that in quotes should do the trick.

-Dsonar.projectName="$randomProjectName"

1. First time, according to this:

randomProjectKey="$1"
codePath="$2"
logFile="$3"
randomProjectName="$4"
{
docker run --rm \
-e SONAR_HOST_URL="http://192.168.1.111:9000/" \
-e SONAR_SCANNER_OPTS="-Dsonar.projectKey=$randomProjectKey -Dsonar.projectName="$randomProjectName" -Dsonar.java.binaries=/usr/src/sonar-check-code-path/target/classes -Dsonar.exclusions=**/*.js -Dsonar.sources=/usr/src/sonar-check-code-path" \
-e SONAR_TOKEN="1bd87dd0b9d1d284a5e5b0437385da53d02d97f5" \
-v "$codePath:/usr/src/sonar-check-code-path" \
--network mynetwork \
sonarsource/sonar-scanner-cli
} > "$logFile" 2>&1

when i called:

./sonarqube.sh '64b5ac6b-772c-4031-ba7a-de26c34aa814' '/data/atm/source_code/1/project' '/data/atm/source_code/1/project/sonarCheckLog.log' 'aabb 1122'

I got:
Project Key: 03b7522b-ec6b-4218-81e0-184a0eb6eef9 Code Path: /data/atm/source_code/1/project LogFile:/data/atm/source_code/1/project/sonarCheckLog.log Project Name: aabb 1122 docker: invalid reference format: repository name (usr/src/sonar-check-code-path/target/classes -Dsonar.exclusions=**/*.js -Dsonar.sources=/usr/src/sonar-check-code-path) must be lowercase. See 'docker run --help'.

2. Second time, I changed "$randomProjectName" to \"$randomProjectName\"

and i got:

Code Path: /data/atm/source_code/1/project
LogFile:/data/atm/source_code/1/project/sonarCheckLog.log
Project Name: aabb 1122
Error: Could not find or load main class 1122"
Caused by: java.lang.ClassNotFoundException: 1122"

I’ve tried it a few ways and I admit that I’m a bit stumped.

Indeed:

docker run \
    --rm \
    -e SONAR_SCANNER_OPTS="-Dsonar.projectName="Test Test" -Dsonar.projectKey=colin-sonarsource_dotnettest -Dsonar.organization=colin-sonarsource" \
    -e SONAR_TOKEN="X" \
    -v "./usr/src" \
    sonarsource/sonar-scanner-cli

results in

docker: invalid reference format: repository name (library/Test -Dsonar.projectKey=colin-sonarsource_dotnettest -Dsonar.organization=colin-sonarsource) must be lowercase.

While

 docker run \
    --rm \
    -e SONAR_SCANNER_OPTS="-Dsonar.projectName="Test\ Test" -Dsonar.projectKey=colin-sonarsource_dotnettest -Dsonar.organization=colin-sonarsource" \
    -e SONAR_TOKEN="X" \
    -v "./usr/src" \
    sonarsource/sonar-scanner-cli

results in

Caused by: java.lang.ClassNotFoundException: Test

I’m flagging this for some expert eyes.

Hello @HardToThinkAUsername ,

We reproduced the issue but we couldn’t find a workaround to make it work, so we opened a JIRA ticket.

Thank you for your report.
Cheers.