我正在寻找一种工具,可以自动化优化代码库中的导入过程。此工具在大多数IDE(例如IntelliJ)中都可用,它可以删除未使用的导入并将任何。*导入扩展到代码专门使用的那些。
我想将其添加为MVN目标或Ant任务,或者只是我在提交/推送之前可以运行的东西。
也许有一种方法可以在命令行运行IntelliJ来专门执行此功能,但我还没有找到这样的命令。
答案 0 :(得分:4)
如果您正在寻找优化Java导入的命令行工具,请检查Google Java Style Formatter。它提供了修复导入的选项:
$ 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
答案 1 :(得分:0)
ImportScrubber似乎满足您的要求。它可以ant task,maven plugin。