How do you scan a project with sonar-scanner?

BeginnerHands-onSonarQube
Answer

Short answer

Create a project and a token in SonarQube, add a sonar-project.properties file (or pass -D options), then run sonar-scanner with the server URL and token. Results appear in the SonarQube dashboard.

sonar-project.properties

sonar.projectKey=shop-web
sonar.projectName=Shop Web
sonar.sources=src
sonar.tests=tests
sonar.exclusions=**/node_modules/**,**/dist/**
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.python.coverage.reportPaths=coverage.xml

Run it

export SONAR_TOKEN=squ_xxx                   # from My Account → Security
sonar-scanner -Dsonar.host.url=http://sonarqube:9000

# or with Docker
docker run --rm -e SONAR_HOST_URL=http://sonarqube:9000 -e SONAR_TOKEN \
  -v "$(pwd):/usr/src" sonarsource/sonar-scanner-cli

Language-specific scanners

  • Maven: mvn verify sonar:sonar
  • Gradle: ./gradlew sonar (plugin org.sonarqube)
  • .NET: dotnet sonarscanner begin /k:"shop-api" ... ; dotnet build ; dotnet sonarscanner end

What to say in the interview

"Project key and token from SonarQube, sonar-project.properties defining sources, exclusions and coverage report paths, then sonar-scanner pointed at the server. Maven, Gradle and .NET have their own scanner integrations."

All Security Tools questions Roadmap: SonarQube — code quality & SAST

More on SonarQube