Coder Social home page Coder Social logo

davidburstromspotify / gradle-imports Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ajoberstar/gradle-imports

0.0 1.0 0.0 163 KB

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

License: Apache License 2.0

Groovy 100.00%

gradle-imports's Introduction

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.

gradle-imports's People

Contributors

ajoberstar avatar simschla avatar brunoessmann avatar waffle-iron avatar

Watchers

David Burström avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.