【问题标题】:Organize imports of java files using eclipse from command line使用 eclipse 从命令行组织 java 文件的导入
【发布时间】:2014-07-14 05:05:15
【问题描述】:

我想格式化 java 代码并使用命令行在 pre-commit 钩子中组织导入,当我搜索如何执行此操作时,我发现了这个link

它解释了如何从命令行使用 eclipse 格式化代码,如下所示:

eclipse -application org.eclipse.jdt.core.JavaCodeFormatter -config {setting}/org.eclipse.jdt.core.prefs {project.basedir}/src

但不知道负责格式化代码的参数名称,因为我希望命令是这样的:

eclipse -application {Java_Import_Organizer_Parameter} -config {setting}/org.eclipse.jdt.ui.prefs {project.basedir}/src

有什么想法吗?

【问题讨论】:

  • Java 开发工具提供的命令行应用程序只有 JavaCodeFormatter 和 JavaIndexer。
  • 太糟糕了,看来你和我有同样的问题;但看起来没有解决方案可以从命令行组织导入。

标签: java eclipse command-line import pre-commit-hook


【解决方案1】:

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

【讨论】:

  • 这不会重现 Eclipse 添加缺失导入的功能(怎么可能,它必须询问您想要哪个类)。
  • 他们很好地隐藏了这个选项。感谢您发布它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 2013-06-12
相关资源
最近更新 更多