How sonarsource/sonar-scanner-cli works
Project Type and Purpose
• This is an open‑source, Java‑based Command‐Line Interface (CLI) tool (“sonar‑scanner‑cli”) whose sole purpose is to run code analysis on a target project and submit results to a SonarQube server (or SonarCloud).
• It reads configuration from a sonar‑project.properties file or command‑line arguments, boots up an analysis engine, executes language plugins, and pushes analysis reports over HTTP to a SonarQube instance.
File Structure Analysis
• Top‐level orchestration:
• pom.xml – Maven build, dependencies, assembly plugin configuration
• src/main/java/org/sonarsource/scanner/cli – core Java classes (Main, Cli, Conf, PropertyResolver, ScannerEngineBootstrapperFactory, etc.)
• src/main/assembly – shell‑script wrappers, bat scripts, default sonar‑scanner.properties and packaging descriptors
• src/main/resources – logging config (logback.xml), version.txt
• src/test/java & src/test/resources – unit and component tests for each CLI module
• it/ and it/projects/ – integration tests with multiple sample projects to validate module discovery and configuration loading
• docs/, sonar-docs/ – user‑facing documentation & markdown
Main Components
a. CLI Layer
• Entry point: Main.java calls into Cli.java
• Argument parsing & help text, exit codes
b. Configuration Layer
• PropertyResolver (merges command‑line args, runner settings from home dir, project properties, module‑level overrides)
• Conf (typed configuration object passed downstream)
c. Bootstrapping Layer
• ScannerEngineBootstrapperFactory (Factory pattern to produce configured ScannerEngine)
d. Engine & Execution Layer
• ScannerEngine (coordinates scanning phases: project/module discovery, plugin loading, file enumeration)
• Analysis plugins & language adapters (external components, not in this repo)
e. Network Layer
• HTTP client inside engine submits analysis payload to SonarQube server endpoints
f. Packaging & Distribution
• Assembly scripts (sonar‑scanner, .bat, .properties) bundle JARs, config files, default templates
g. Testing & CI
• Unit tests vs. integration tests (it/), CI workflows in .github/workflows
Relationships & Data Flow
1. User invokes shell script (sonar‑scanner[-debug].sh or .bat)
2. Script launches JVM, calls org.sonarsource.scanner.cli.Main
3. Main → Cli: parse CLI args → PropertyResolver
4. PropertyResolver reads in cascade:
• Runner settings (~/.sonar/scanner/sonar‑scanner.properties or overridden path)
• Project root sonar‑project.properties
• Module‑specific sonar‑project.properties
• Environment variables
5. Conf object → ScannerEngineBootstrapperFactory → produces ScannerEngine
6. ScannerEngine executes:
• Discovers modules & source dirs
• Applies analysis plugins (via SPI) to collect measures
• Aggregates results
7. ScannerEngine → HTTP client → SonarQube server (REST API)
8. Engine returns exit code → Main → process exits
Architectural Patterns & Principles
• Layered architecture (CLI → Config → Bootstrap → Engine → Network)
• Factory pattern for engine bootstrapping
• Plugin‑SPI design for language analyzers (decoupled modules loaded at runtime)
• Immutable configuration object (Conf) passed downstream
• Clear separation of concerns: parsing, config resolution, engine orchestration, network transport