Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a command line tool to Optimize Imports on Java project?

I'm looking for a tool that will automate the process of optimizing the imports on a code base. This tool is available in most IDEs (IntelliJ for instance) which removes unused imports and expands any .* imports into the those specifically used by the code.

I'd like to add this as a MVN goal, or Ant Task, or just something I can run before my commit/push.

Perhaps there is a way run IntelliJ at the command line to specifically execute this feature, but I haven't found such a command.

like image 421
lucidquiet Avatar asked Apr 09 '14 21:04

lucidquiet


1 Answers

If you're looking for a command line tool to optimize java imports, checkout The Google Java Style Formatter. It provides an option to fix imports:

$ java -jar google-java-format-1.4-all-deps.jar
no files were provided

Usage: google-java-format [options] file(s)

Options:
  -i, -r, -replace, --replace
    Send formatted output back to files, not stdout.
  -
    Format stdin -> stdout
  --aosp, -aosp, -a
    Use AOSP style instead of Google Style (4-space indentation)
  --fix-imports-only
    Fix import order and remove any unused imports, but do no other formatting.
  --skip-sorting-imports
    Do not fix the import order. Unused imports will still be removed.
  --skip-removing-unused-imports
    Do not remove unused imports. Imports will still be sorted.
  --length, -length
    Character length to format.
  --lines, -lines, --line, -line
    Line range(s) to format, like 5:10 (1-based; default is all).
  --offset, -offset
    Character offset to format (0-based; default is all).
  --help, -help, -h
    Print this usage statement
  --version, -version, -v
    Print the version.

If -i is given with -, the result is sent to stdout.
The --lines, --offset, and --length flags may be given more than once.
The --offset and --length flags must be given an equal number of times.
If --lines, --offset, or --length are given, only one file (or -) may be given.

google-java-format: Version 1.0
https://github.com/google/google-java-format
like image 109
poindexter Avatar answered Oct 20 '22 15:10

poindexter