Skip to main content
Version: Next

Run detekt using Command Line Interface

Install the cli​

There are different ways to install the Command Line Interface (CLI):

MacOS, with Homebrew:​

brew install detekt
detekt [options]

Windows, with Scoop:​

scoop install detekt
detekt [options]

Unix-like OS, with SDKMAN!:​

sdk install detekt

Any OS:​

curl -sSLO https://github.com/detekt/detekt/releases/download/v2.0.0-alpha.6/detekt-cli-2.0.0-alpha.6.zip
unzip detekt-cli-2.0.0-alpha.6.zip
./detekt-cli-2.0.0-alpha.6/bin/detekt-cli --help

NixOS:​

As a prerequisite, you have to add the unstable channel via nix-channel and then execute the following command.

nix-shell -I nixpkgs=channel:nixpkgs-unstable -p detekt

Use the cli​

detekt will exit with one of the following exit codes:

Exit codeDescription
0detekt ran normally and no issues1 were found.
1An unexpected error occurred.
2At least one issue1 was found.
3Invalid detekt configuration file detected.

The following parameters are shown when --help is entered.

Usage: detekt [<options>] [<freecompilerargs>]...

Options:
-i, --input=<path> Input paths to analyze. Multiple paths are separated by ':' on *nix or ';' on Windows. If not specified the current working directory is used.
--analysis-mode=(full|light) Analysis mode used by detekt. 'full' analysis mode is comprehensive but requires the correct compiler options to be provided. 'light' analysis cannot utilise compiler information and some rules cannot be run in this mode.
-in, --includes=<text> Globbing patterns describing paths to include in the analysis. Useful in combination with 'excludes' patterns.
-ex, --excludes=<text> Globbing patterns describing paths to exclude from the analysis.
-c, --config=<path> Path to the config file (path/to/config.yml). Multiple configuration files can be specified with ':' on *nix or ';' on Windows as separator.
-cr, --config-resource=<value> Path to the config resource on detekt's classpath (path/to/config.yml).
-gc, --generate-config=<path> Export default config to the provided path.
-p, --plugins=<path> Extra paths to plugin jars separated by ':' on *nix or ';' on Windows.
--parallel Enables parallel compilation and analysis of source files. Do some benchmarks first before enabling this flag. Heuristics show performance benefits starting from 2000 lines of Kotlin code.
-b, --baseline=<path> If a baseline xml file is passed in, only new findings not in the baseline are printed in the console.
-cb, --create-baseline Treats current analysis findings as a smell baseline for future detekt runs.
-r, --report=<value> Generates a report for given 'report-id' and stores it on given 'path'. Entry should consist of: [report-id:path]. Available 'report-id' values: 'checkstyle', 'html', 'md', 'sarif'. These can also be used in combination with each other e.g. '-r html:reports/detekt.html -r checkstyle:reports/detekt.xml'
--fail-on-severity=<value> Specifies the minimum severity that causes the build to fail. When the value is set to 'Never' detekt will not fail regardless of the number of issues and their severities.
-bp, --base-path=<path> Specifies a directory as the base path. Currently it impacts all file paths in the formatted reports. File paths in console output are not affected and remain as absolute paths.
-dd, --disable-default-rulesets Disables default rule sets.
--build-upon-default-config Preconfigures detekt with a bunch of rules and some opinionated defaults for you. Allows additional provided configurations to override the defaults.
--all-rules Activates all available (even unstable) rules.
-ac, --auto-correct Allow rules to auto correct code if they support it. The default rule sets do NOT support auto correcting and won't change any line in the users code base. However custom rules can be written to support auto correcting. The additional 'ktlint' rule set, added with '--plugins', does support it and needs this flag.
--debug Prints extra information about configurations and extensions.
-cp, --classpath=<path> Paths where to find user class files and depending jar files. Used for type resolution.
--api-version=<value> Kotlin API version used by the code under analysis. Some rules use this information to provide more specific rule violation messages.
--language-version=<value> Compatibility mode for Kotlin language version X.Y, reports errors for all language features that came out later
--jvm-target=<value> Target version of the generated JVM bytecode that was generated during compilation and is now being used for type resolution
--jdk-home=<path> Use a custom JDK home directory to include into the classpath
--version Prints the detekt CLI version.
-h, --help Show this message and exit

Arguments:
<freecompilerargs> Options to pass to the Kotlin compiler.

Use the cli to generate configuration for custom rules​

Usage: java -jar detekt-generator-2.0.0-alpha.6-all.jar [options]
Options:
--generate-custom-rule-config, -gcrc
Generate custom rules configuration files. The files will be
placed under 'resources' folder of each rule respectively
(e.g. 'custom-rule/src/main/resources/config/config.yml').
--input, -i
Input paths to rules to analyze. Multiple paths are separated by comma.
java -jar detekt-generator-2.0.0-alpha.6-all.jar -gcrc -i /path/to/rule1, /path/to/rule2

Footnotes​

  1. All found issues that have a severity of or above the --fail-on-severity argument are considered. ↩ ↩2