Not (actively) maintained anymore, you might want to check out the Spotless Gradle 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.
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"
}
The Format plugin defines the following tasks:
format
: Format Java source code
The Format task defines the following properties:
configurationFile
: The formatter configuration (File)importsOrder
: The import orders (List of strings)importsOrderConfigurationFile
: The import orders (a file using Eclipse format)files
: The files to format (FileCollection), defaults are Java sources from main and test SourceSets
format {
configurationFile = file('myformatsettings.properties')
importsOrder = ["java", "javax", "org", "\\#com.something.StaticImport"]
files = sourceSets.main.java
}
Using Hibernate Tools JavaFormatter
Import ordering from EclipseCodeFormatter
Readme format from https://github.com/bmuschko/gradle-gae-plugin