【问题标题】:Is there a command line tool to Optimize Imports on Java project?是否有命令行工具来优化 Java 项目的导入?
【发布时间】:2014-05-23 07:43:53
【问题描述】:

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

我想将其添加为 MVN 目标或 Ant 任务,或者只是我可以在提交/推送之前运行的东西。

也许有办法在命令行运行IntelliJ来专门执行这个功能,但是我没有找到这样的命令。

【问题讨论】:

    标签: java refactoring automated-refactoring


    【解决方案1】:

    如果您正在寻找优化 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
    

    【讨论】:

      【解决方案2】:

      ImportScrubber 似乎满足您的要求。可以ant task,还有maven plugin

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-13
        • 1970-01-01
        相关资源
        最近更新 更多