Skip to content

UNMAINTAINED: Gradle plugin for organizing imports in Java and Groovy source files.

License

Notifications You must be signed in to change notification settings

davidburstromspotify/gradle-imports

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gradle-imports

A Gradle plugin to organize imports in Java and Groovy source files.

NOTE This plugin is no longer maintained. I recommend using diffplug/spotless instead.

Bintray

Where do I get it?

See the Gradle plugin portal for instructions on applying the latest version of the plugin.

What does it do?

The plugin adds a single task called organizeImports. By default, the task is configured to process all .java and .groovy files in all source sets.

It has two main behaviors:

  • Sort imports into sections.
  • Remove unused imports.

How do I use it?

NOTE: You should only run this on versioned code. This will ensure that you can revert if you either don't like the behavior or find a bug.

Just run the organizeImports task.

./gradlew organizeImports

How can I configure it?

Just directly configure the task:

organizeImports {
	sourceSets = project.sourceSets
	sortOrder = [/^(javax?)\./, /^(groovyx?)\./, /^([^\.]+\.[^\.]+)\./]
	staticImportsFirst = true
	removeUnused = true
	includes = ['**/*.java', '**/*..groovy']
}

The main one that needs explanation is the sort order. This is based on a list of regular expressions that match against the fully qualified class names. It should have one capture group that returns the section of the class name that will be sorted on.

How does it work?

The sorting is done in 4 steps:

  1. Static import or not.
  2. Index of the sortOrder pattern it matched against.
  3. Result of capture group of matched pattern.
  4. Full class name

As an example (using the default sortOrder). The following imports:

import groovy.transform.Immutable
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.Commit
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.Repository
import java.util.regex.Matcher
import java.io.File
import static org.junit.Assert.*

Result in:

import static org.junit.Assert.*

import java.io.File
import java.util.regex.Matcher

import groovy.transform.Immutable

import org.ajoberstar.grgit.Commit
import org.ajoberstar.grgit.Grgit

import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.Repository

The approach to determining unused imports is to search for uses of the class's simple name after the import declarations in the file. As mentioned above, this isn't meant to be a robust parser, so ensure you have your files versioned before running it.

See the Groovydoc for more information.

Release Notes

v1.0.1

  • Support ! in front of class name.

v1.0.0

  • Changing plugin ID from organize-imports to org.ajoberstar.organize-imports to be compatible with plugin portal.
  • Waiting to configure sourceSets on task until the java plugin applied to work around ordering issues.

v0.1.0

  • Initial release.
  • See this commit for an example of the changes it makes.

About

UNMAINTAINED: Gradle plugin for organizing imports in Java and Groovy source files.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Groovy 100.0%