是否存在用于在Java项目上优化导入的命令行工具? [英] Is there a command line tool to Optimize Imports on Java project?

查看:117
本文介绍了是否存在用于在Java项目上优化导入的命令行工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种工具,该工具可以根据代码自动优化导入过程。该工具在大多数IDE(例如IntelliJ)中都可用,它可以删除未使用的导入并将任何。*导入扩展为代码专门使用的那些导入。

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.

我想要将其添加为MVN目标或Ant Task,或者将其添加为我的提交/推送操作之前的内容。

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

也许有一种方法可以在命令行上运行IntelliJ专门执行此功能,但我没有找到这样的命令。

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

推荐答案

如果您正在寻找命令行用于优化Java导入的工具,请检出Google Java样式格式化器。它提供了一个修复导入的选项:

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

这篇关于是否存在用于在Java项目上优化导入的命令行工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆