Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Scanning and Integrations

Scanners for Build Tools

Run SonarQube analysis through Maven, Gradle, .NET and Node.js: the commands, the key properties, why compiled languages need build output, and a command generator.

For languages that compile, the scanner needs more than the source text: it needs the compiled output and the classpath to understand types and resolve calls. That is why SonarSource provides scanners integrated with each build tool. They read the project structure straight from the build file, so you rarely write a properties file by hand. This lesson shows the four common routes.

Plugin versions move quickly. The commands below show the shape; take the current plugin version from the official documentation. They are read-only examples, because the build tools and a SonarQube server are not available on this site.

Maven

The Maven integration is a goal of the SonarQube Maven plugin, run after the code is compiled and tested:

Terminal
mvn clean verify sonar:sonar \
  -Dsonar.projectKey=acme_orders \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.token=$SONAR_TOKEN

verify runs compilation and tests first, so class files and test reports exist. The plugin derives sources, tests, binaries (target/classes) and the module structure from pom.xml. For a multi-module build, run it once on the parent and every module is analyzed. Code coverage comes from JaCoCo, so add the JaCoCo plugin and point sonar.coverage.jacoco.xmlReportPaths at its XML report (the default location is target/site/jacoco/jacoco.xml).

Gradle

Apply the Sonar plugin in build.gradle.kts:

Kotlin
plugins {
    id("org.sonarqube") version "<current version>"
    jacoco
}

sonar {
    properties {
        property("sonar.projectKey", "acme_orders")
        property("sonar.host.url", "http://localhost:9000")
    }
}

Then run ./gradlew build sonar. The plugin reads source sets, class directories and the classpath from Gradle; the token comes from the SONAR_TOKEN environment variable. Make the sonar task depend on test and report tasks, so everything exists first.

.NET

For C# and other .NET languages the analysis hooks into the compiler, so the scanner wraps the build in three steps:

Terminal
dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k:"acme_orders" /d:sonar.host.url="http://localhost:9000" /d:sonar.token="$SONAR_TOKEN"
dotnet build
dotnet sonarscanner end /d:sonar.token="$SONAR_TOKEN"

begin prepares the analysis, build runs your normal build while the Roslyn analyzers collect results, and end uploads the report. Keep the same token in begin and end. Coverage reports (for example from Coverlet in OpenCover or Cobertura format) are given with /d:sonar.cs.opencover.reportsPaths.

Node.js and the generic scanner

JavaScript and TypeScript do not need compiled output. Either use the CLI as in the previous lesson, or the Node package that wraps it, usually run from an npm script:

JSON
{
  "scripts": {
    "test": "jest --coverage",
    "sonar": "sonar-scanner"
  }
}

Tell the analyzer where Jest writes LCOV with sonar.javascript.lcov.reportPaths=coverage/lcov.info. TypeScript projects benefit from pointing the analyzer at tsconfig.json through sonar.typescript.tsconfigPaths for accurate type information.

Choosing the route

flowchart TD
  A["Which language?"] --> B{"Java or Kotlin?"}
  B -- "Maven" --> C["mvn verify sonar:sonar"]
  B -- "Gradle" --> D["./gradlew build sonar"]
  A --> E{".NET?"}
  E --> F["dotnet sonarscanner begin / build / end"]
  A --> G{"Python, JS, PHP, Go, other?"}
  G --> H["sonar-scanner (CLI)"]

Compiled JVM and .NET builds use their build integration; everything else uses the CLI.

A command generator

The stand-in builds the right command for a project description and shows how a script can choose the route. It prints the form of the commands; real version numbers come from the official docs.

Python Pick the scanner command for a project (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Detecting the build tool

Many teams ship a single pipeline template for many repositories. This stand-in guesses the right tool from the files in the repository root, which is the logic such a template uses.

Python Detect the build tool from file names (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Running the scan before compiling (Java): missing binaries mean weak or no bug detection; the analyzer reports an error or analyzes without types.
  • Missing coverage reports because tests ran in a different step or workspace; paths must exist where the scanner runs.
  • Different tokens in begin and end (.NET) cause an authorization failure.
  • Analyzing generated code adds noise; exclude it (Exclusions and Test Sources).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do Java projects need compiled classes for analysis?

    Type information from compiled classes enables deeper rules such as null dereference detection.
  2. 2. Which Maven command runs analysis after building and testing?

    verify builds and tests so class files and reports exist before the sonar:sonar goal.
  3. 3. What are the three .NET scanner steps?

    dotnet sonarscanner begin, then dotnet build, then dotnet sonarscanner end.
  4. 4. Which tool analyzes Python projects?

    Languages without a build-tool integration use the generic scanner.

Summary

  • Compiled languages use build-tool scanners so analyzers see classes and classpaths.
  • Maven: mvn verify sonar:sonar; Gradle: org.sonarqube plugin and ./gradlew sonar; .NET: begin, build, end; JS/Python/others: the CLI.
  • Generate coverage reports before the scan and point the right property at them.
  • Keep tokens in environment variables and exclude generated code.

Next: Exclusions and Test Sources