简体   繁体   中英

Organize imports of java files using eclipse from command line

I want to format java code and organize the imports in the pre-commit hook using command line, When i searched how to do this i found this link

which explains how to format the code using eclipse from command line as the following:

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

But couldn't know the parameter name that is responsible for formatting the code, as i expect the command will be like this:

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

any idea?

The Google Java Style Formatter 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM