我想使用命令行格式化java代码并在预提交钩子中组织导入,当我搜索如何执行此操作时,我发现了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
任何想法?
答案 0 :(得分:2)
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