Detekt is a static code analysis tool for Kotlin, it comes with a cli and a Gradle plugin. However, if Maven is your goto build automation tool adding detekt to your build is a bit more cumbersome. The example in the detekt readme uses the maven-antrun-plugin to execute the detekt CLI.
A lot of lines to run detekt when building.
Enter me, attempting to introduce Kotlin at my workplace. One issue in the way is the matter of static code analysis and detekt seems, at the moment, to be the tool to use. We also use Maven for building. This prompted me to have a look at building Maven plugins, which by the way is not that hard.
Taking the simplest approach, the detekt maven plugin is based on the detekt CLI, basically creating the parameters from Maven properties instead of CLI parameters. This means that the detekt maven plugin does almost exactly the same thing as the above antrun plugin configuration does, but with less XML.
Apart from this, which runs detekt in the verify phase using the check goal, the detekt maven plugin has one goal for creating a baseline and one for generating a default configuration file.
- mvn detekt:create-baseline
- mvn detekt:generate-config
More detailed information on the usage of the plugin is available at the Github repository.
To conclude, writing the detekt maven plugin has given me two things, knowledge of how to write Maven plugins and a more concise pom. It should also make it easier to avoid configuration errors since the plugin dictates what configuration parameters are available unlike the more generic antrun plugin.