Home

Awesome

Not (actively) maintained anymore, you might want to check out the Spotless Gradle plugin

Gradle Format plugin

The Gradle Format plugin enables you to format your Java sources. It operates in place and can be used for instance before pushing changes to enforce a coding style. The formatting can be customized using a properties or XML file with the keys defined in DefaultCodeFormatterConstants from Eclipse. See src/test/resources/ for examples. Eventually you should be able to export Eclipse settings to this file.

The plugin also enables you to sort imports.

Usage

To use the Format plugin, include in your build script:

apply plugin: 'com.github.youribonnaffe.gradle.format'

Note that the Java plugin will be applied too.

To run it:

gradle format

The plugin JAR needs to be accessible in the classpath of your build script. It is directly available on Gradle plugins. Alternatively, you can download it from GitHub and deploy it to your local repository. The following code snippet shows an example on how to get it and use it:

plugins {
  id "com.github.youribonnaffe.gradle.format" version "1.5"
}

Tasks

The Format plugin defines the following tasks:

Properties

The Format task defines the following properties:

Example

format {
    configurationFile = file('myformatsettings.properties')
    importsOrder = ["java", "javax", "org", "\\#com.something.StaticImport"]
    files = sourceSets.main.java
}

Acknowledgements

Using Hibernate Tools JavaFormatter

Import ordering from EclipseCodeFormatter

Readme format from https://github.com/bmuschko/gradle-gae-plugin