---
title: "Run detekt using the Compiler Plugin"
keywords: [detekt, static, analysis, code, kotlin]
sidebar:
permalink: compilerplugin.html
folder: gettingstarted
summary:
sidebar_position: 7
---

You can integrate detekt in your project using the Detekt Compiler Plugin instead of the classic [Detekt Gradle Plugin](/docs/gettingstarted/gradle). Detekt offers a compiler plugin for K1 which allows you to run detekt as part of the Kotlin compilation process using [type resolution](/docs/gettingstarted/type-resolution). This allows you to run detekt on your code without having separate tasks to invoke and results in much faster execution of detekt, especially if you use type resolution with the Gradle plugin.

:::caution

Please note that Detekt Compiler Plugin is an **experimental extension** of detekt. We expect it to be stable with the upcoming release of detekt (2.x)

:::

## Using Gradle

### Adding the Compiler Plugin

To use the detekt Compiler Plugin, you will have to add the following Gradle Plugin:

```kotlin
plugins {
  id("io.github.detekt.gradle.compiler-plugin") version "[detekt_version]"
}
```

### Configuring the Compiler Plugin

The compiler plugin can be configured using the `detekt {}` block in your gradle file.

The following options are allowed:

```kotlin
detekt {
    // Define the detekt configuration(s) you want to use.
    // Defaults to the default detekt configuration.
    config.setFrom("path/to/config.yml")

    // Applies the config files on top of detekt's default config file. `false` by default.
    buildUponDefaultConfig = false

    // Turns on all the rules. `false` by default.
    allRules = false

    // Specifying a baseline file. All findings stored in this file in subsequent runs of detekt.
    baseline = file("path/to/baseline.xml")

    // Disables all default detekt rulesets and will only run detekt with custom rules
    // defined in plugins passed in with `detektPlugins` configuration. `false` by default.
    disableDefaultRuleSets = false

    // Adds debug output during task execution. `false` by default.
    debug = false

    // Kill switch to turn off the Compiler Plugin execution entirely.
    enableCompilerPlugin.set(true)
}
```

Moreover, detekt reports can be configured at the Kotlin Compilation tasks level as follows

```kotlin
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    detekt {
        reports {
            xml.enabled.set(true)
            md.enabled.set(false)
            create("custom") {
                enabled.set(false)
            }
        }
    }
}
```

### Adding third party plugins

As for the Detekt Gradle Plugin, you can add third party plugins to the Compiler Plugin using the `detektPlugins` configuration.

```kotlin
dependencies {
    detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:[detekt_version]")
}
```

### Running the Compiler Plugin

The compiler plugin will run during your `compileKotlin` tasks execution:

```shell
$ ./gradlew compileKotlin

> Task :example:compileKotlin
w: Analysis failed with 1 issues.
w: file:///.../example/src/main/java/Sample.kt:4:17 MagicNumber: This expression contains a magic number. Consider defining it to a well named constant.

BUILD SUCCESSFUL in 1s
5 actionable tasks: 1 executed, 4 up-to-date
```

## Using CLI compiler

You can also use the Compiler Plugin with the Kotlin command-line compiler.

You'll need to grab `detekt-compiler-plugin-<version>-all.jar` from our [GitHub releases page](https://github.com/detekt/detekt/releases) or Maven Central `io.github.detekt:detekt-compiler-plugin` artifact (note that you'll need the uber-jar file, the one with the `all` classifier).

You can attach the plugin by providing the path to its JAR file using the `-Xplugin` kotlinc option:

```shell
kotlinc Main.kt -Xplugin=./detekt-compiler-plugin-1.23.7-all.jar
```

It's possible to pass options to the plugin using the `-P` kotlinc option:

```
# The plugin option format is: "-P plugin:detekt-compiler-plugin:<key>=<value>".
# Options can be repeated.

-P plugin:detekt-compiler-plugin:config=path/to/config.yml
-P plugin:detekt-compiler-plugin:debug=true
```

The available options are:

```
config=<path|paths> # Comma separated paths to detekt config files.
baseline=<path> # Path to a detekt baseline file.
debug=<true|false> # Print debug messages.
isEnabled=<true|false> # Should detekt run?
useDefaultConfig=<true|false> # Use the default detekt config as baseline.
allRules=<true|false> # Turns on all the rules.
disableDefaultRuleSets=<true|false> # Disables all default detekt rulesets.
parallel=<true|false> # Enables parallel compilation and analysis of source files.
rootPath=<path> # Root path used to relativize paths when using exclude patterns.
excludes=<base64-encoded globs> # A base64-encoded list of the globs used to exclude paths from scanning.
report=<report-id:path> # Generates a report for given 'report-id' and stores it on given 'path'. Available 'report-id' values: 'xml', 'html'.
```

## Known Issues

1. The rule `InvalidPackageDeclaration` is known to not be working well with the Compiler Plugin [#5747](https://github.com/detekt/detekt/issues/5747).
