Fork me on GitHub

Usage

Some brief examples on how to use this plugin.

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>1.2.2</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>1.2.2</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:1.2.2: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>1.2.2</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:1.2.2: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>1.2.2</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>1.2.2</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/shyiko/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>1.2.2</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>