gradle-imports

Gradle plugin for organizing imports in Java and Groovy sources.

License

License

Categories

Categories

Gradle Build Tools
GroupId

GroupId

org.ajoberstar
ArtifactId

ArtifactId

gradle-imports
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

gradle-imports
Gradle plugin for organizing imports in Java and Groovy sources.
Project URL

Project URL

https://github.com/ajoberstar/gradle-imports
Source Code Management

Source Code Management

https://github.com/ajoberstar/gradle-imports

Download gradle-imports

How to add to project

<!-- https://jarcasting.com/artifacts/org.ajoberstar/gradle-imports/ -->
<dependency>
    <groupId>org.ajoberstar</groupId>
    <artifactId>gradle-imports</artifactId>
    <version>1.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/org.ajoberstar/gradle-imports/
implementation 'org.ajoberstar:gradle-imports:1.0.3'
// https://jarcasting.com/artifacts/org.ajoberstar/gradle-imports/
implementation ("org.ajoberstar:gradle-imports:1.0.3")
'org.ajoberstar:gradle-imports:jar:1.0.3'
<dependency org="org.ajoberstar" name="gradle-imports" rev="1.0.3">
  <artifact name="gradle-imports" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.ajoberstar', module='gradle-imports', version='1.0.3')
)
libraryDependencies += "org.ajoberstar" % "gradle-imports" % "1.0.3"
[org.ajoberstar/gradle-imports "1.0.3"]

Dependencies

runtime (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.6

Project Modules

There are no modules declared in this project.

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.

Versions

Version
1.0.3
1.0.2
1.0.1
1.0.1-rc.1
0.1.0