How to auto format Kotlin source code

Posted by Java developer blog on July 5, 2019

Modern programming languages like Go and Rust use an additional tool that automatically formats source code according to the official code style.

It helps to reduce bikeshedding significantly.

Kotlin does not have built in solution.

Nevertheless, there are two solutions to the problem.

First solution

Kotlin has a great IDE which helps you a lot.

The IDE has ability to reformat code explicitly according to the official code style.

There is a Gradle plugin that is able to launch IDE inspections, for example during a build. It has ability to format code as well.

However, there is no plugin for Maven right now. It is sad(

Second solution

There is a ktlint tool that addresses the same problem. It has plugins for both Gradle and Maven build tools. You can set up it to auto format source code before the compilation stage.

The following example shows you how to implement it with help of a Maven ktlint plugin.

You need to add the following snippet to pom.xml (inside <build><plugins>...</plugins></build>)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>ktlint-format</id>
            <phase>validate</phase>
            <configuration>
            <target name="ktlint">
                <java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true"
                    classname="com.pinterest.ktlint.Main" classpathref="maven.plugin.classpath">
                    <arg value="-F"/>
                    <arg value="src/**/*.kt"/>
                </java>
            </target>
            </configuration>
            <goals><goal>run</goal></goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>com.pinterest</groupId>
            <artifactId>ktlint</artifactId>
            <version>0.33.0</version>
        </dependency>
    </dependencies>
</plugin>

I recommend to put it into parent pom.xml file in case of a multi module project.

I recommend to change max number of Blank Lines to 1 for Kotlin Code Style in IntelliJ IDEA settings.

That is it!

It works perfectly!

Thank you for reading!