Skip to content

youribonnaffe/gradle-format-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

  • format: Format Java source code

Properties

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

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

About

A Gradle plugin to format Java source code

Resources

License

Stars

Watchers

Forks

Packages

No packages published