问题
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.
回答1:
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
回答2:
ImportScrubber seems to satisfy your requirement. It can do ant task and there is maven plugin.
来源:https://stackoverflow.com/questions/22973848/is-there-a-command-line-tool-to-optimize-imports-on-java-project