# sonarsource/sonar-scanner-cli architecture

> Architecture diagram of the GitHub repository sonarsource/sonar-scanner-cli, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sonarsource/sonar-scanner-cli
- Repository: https://github.com/sonarsource/sonar-scanner-cli

## Overview

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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    subgraph "CLI Layer"
        Main["Main"]:::internal
        Cli["Cli"]:::internal
    end

    %% Config Layer
    subgraph "Configuration Layer"
        PropertyResolver["PropertyResolver"]:::internal
        Conf["Conf"]:::internal
    end

    %% Bootstrapping Layer
    subgraph "Bootstrap Layer"
        ScannerEngineBootstrapperFactory["ScannerEngineBootstrapperFactory"]:::factory
    end

    %% Engine Layer
    subgraph "Engine Layer"
        ScannerEngine["ScannerEngine"]:::internal
        PluginSPI["Plugin SPI (Language Analyzers)"]:::factory
    end

    %% Network Layer
    subgraph "Network Layer"
        HTTPClient["HTTP Client"]:::internal
        SonarQubeServer["SonarQube Server"]:::external
    end

    %% Packaging & Distribution
    subgraph "Packaging & Distribution"
        ShellScript["Shell Script (sonar-scanner)"]:::dist
        BatScript["Batch Script (sonar-scanner.bat)"]:::dist
        DefaultProperties["Default Properties (sonar-scanner.properties)"]:::files
        AssemblyDescriptors["Assembly Descriptors"]:::dist
    end

    %% Resources
    subgraph "Resources"
        LogConfig["logback.xml"]:::files
        VersionInfo["version.txt"]:::files
    end

    %% Testing & CI
    subgraph "Testing & CI"
        UnitTests["Unit Tests"]:::external
        IntegrationTests["Integration Tests"]:::external
        CIWorkflows["CI Workflows"]:::external
    end

    %% External File System for properties
    FSProperties[(Properties Files)]:::external

    %% Flow
    User["User"]:::external -->|Invoke| ShellScript
    User -->|Invoke| BatScript
    ShellScript -->|Launch JVM| Main
    BatScript -->|Launch JVM| Main
    Main -->|calls| Cli
    Cli -->|Parsed Args| PropertyResolver
    PropertyResolver -->|reads| FSProperties
    PropertyResolver -->|builds| Conf
    Conf -->|config object| ScannerEngineBootstrapperFactory
    ScannerEngineBootstrapperFactory -->|creates| ScannerEngine
    ScannerEngine -->|loads plugins| PluginSPI
    PluginSPI -->|extensions| ScannerEngine
    ScannerEngine -->|sends report| HTTPClient
    HTTPClient -->|REST API| SonarQubeServer
    SonarQubeServer -->|response code| Main

    %% Packaging/Resources dependencies
    ShellScript --> DefaultProperties
    ShellScript --> AssemblyDescriptors
    ShellScript --> LogConfig
    ShellScript --> VersionInfo

    BatScript --> DefaultProperties
    BatScript --> AssemblyDescriptors
    BatScript --> LogConfig
    BatScript --> VersionInfo

    %% Testing & CI mappings
    UnitTests -.-> Cli
    UnitTests -.-> PropertyResolver
    IntegrationTests -.-> ScannerEngine
    CIWorkflows -.-> Main

    %% Click Events
    click Main "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/java/org/sonarsource/scanner/cli/Main.java"
    click Cli "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/java/org/sonarsource/scanner/cli/Cli.java"
    click PropertyResolver "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/java/org/sonarsource/scanner/cli/PropertyResolver.java"
    click Conf "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/java/org/sonarsource/scanner/cli/Conf.java"
    click ScannerEngineBootstrapperFactory "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java"
    click ShellScript "https://github.com/sonarsource/sonar-scanner-cli/tree/master/src/main/assembly/bin/sonar-scanner"
    click BatScript "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/assembly/bin/sonar-scanner.bat"
    click DefaultProperties "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/assembly/conf/sonar-scanner.properties"
    click AssemblyDescriptors "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/assembly/*.xml"
    click LogConfig "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/resources/logback.xml"
    click VersionInfo "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/main/resources/version.txt"
    click UnitTests "https://github.com/sonarsource/sonar-scanner-cli/blob/master/src/test/java/org/sonarsource/scanner/cli/*.java"
    click IntegrationTests "https://github.com/sonarsource/sonar-scanner-cli/tree/master/it/projects"
    click CIWorkflows "https://github.com/sonarsource/sonar-scanner-cli/blob/master/.github/workflows/*.yml"

    %% Styles
    classDef internal fill:#c2f0c2,stroke:#333,stroke-width:1px
    classDef factory fill:#c2d1f0,stroke:#333,stroke-width:1px
    classDef external fill:#f0c2c2,stroke:#333,stroke-width:1px
    classDef dist fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef files fill:#f7f0c2,stroke:#333,stroke-width:1px
```
