Fork me on GitHub

Usage

Some brief examples on how to use this plugin.

Using Java 17 and later

Java 17 is the first LTS release to enforce strong encapsulation. For ktlint to work we need to add --add-opens java.base/java.lang=ALL-UNNAMED to the JVM arguments.

We recommend that you add a .mvn/jvm.config file (relative to the top level project directory) to all of your projects using this plugin. The file should have the following contents:

--add-opens java.base/java.lang=ALL-UNNAMED

We also recommend adding this to all of your projects using this plugin and building with Java 11, as it'll suppress an illegal-access warning during the build.

For other options see: https://maven.apache.org/configure.html

Formatting and checking for violations as part of the build

Probably the most common use case is to want to both format and check your code; add the following plugin configuration to your POM.

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.github.gantsign.maven</groupId>
      <artifactId>ktlint-maven-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <id>format-and-check</id>
          <goals>
            <goal>format</goal>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

Formatting as part of the build

If you just want ktlint to automatically fix what it can, add the following plugin configuration to your POM.

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.github.gantsign.maven</groupId>
      <artifactId>ktlint-maven-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <id>format</id>
          <goals>
            <goal>format</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

To run this from the command line (without adding the plugin to your POM) you can run the following from your console:

mvn com.github.gantsign.maven:ktlint-maven-plugin:3.2.0:format

Checking for violations as part of the build

If you just want to drill good habits into your developers, add the following plugin configuration to your POM.

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.github.gantsign.maven</groupId>
      <artifactId>ktlint-maven-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <id>check</id>
          <goals>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

To run this from the command line (without adding the plugin to your POM) you can run the following from your console:

mvn com.github.gantsign.maven:ktlint-maven-plugin:3.2.0:check

Generate ktlint report as part of the project reports

To generate the ktlint report as part of the project reports, add the ktlint plugin in the <reporting> section of your POM.

<build>
  <pluginManagement>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <!-- ktlint-maven plugin requires maven-site-plugin >= 3.7.0 to work -->
        <version>3.7.1</version>
      </plugin>
      ...
    </plugins>
  </pluginManagement>
</build>

<reporting>
  <plugins>
    ...
    <plugin>
      <groupId>com.github.gantsign.maven</groupId>
      <artifactId>ktlint-maven-plugin</artifactId>
      <version>3.2.0</version>
    </plugin>
    ...
  </plugins>
</reporting>

Using ktlint reporters as part of the build

As well as Maven project reports that are part of the Maven site (see above) this plugin also supports ktlint reporters:

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.github.gantsign.maven</groupId>
      <artifactId>ktlint-maven-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <id>reporter</id>
          <goals>
            <goal>check</goal>
          </goals>
          <configuration>
            <failOnViolation>false</failOnViolation>
            <reporters>
              <reporter>
                <name>plain</name>
                <output>${project.build.directory}/ktlint.txt</output>
                <properties>
                  <property>
                    <name>group_by_file</name>
                    <value>true</value>
                  </property>
                </properties>
              </reporter>
            </reporters>
          </configuration>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

The built in reporters are plain, json and checkstyle. You can also provide a customer reporter (see https://github.com/pinterest/ktlint for details); to use a custom reporter it needs to be added as a Maven dependency to the Maven plugin e.g.:

<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.github.gantsign.maven</groupId>
      <artifactId>ktlint-maven-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <id>check</id>
          <goals>
            <goal>check</goal>
          </goals>
          <configuration>
            <failOnViolation>false</failOnViolation>
            <reporters>
              <reporter>
                <name>my-custom-reporter</name>
                <output>${project.build.directory}/ktlint.rpt</output>
              </reporter>
            </reporters>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>com.example</groupId>
          <artifactId>my-custom-reporter</artifactId>
          <version>1.0</version>
        </dependency>
      </dependencies>
    </plugin>
    ...
  </plugins>
</build>