Organize imports with Maven2, Eclipse-style?

 ̄綄美尐妖づ 提交于 2019-12-03 09:02:59

The Hybridlabs beautifier which is used internally in the openArchitectureWare project (an open source generator framework) is doing what you're looking for. As explained in this blog entry, the beautifier is available as a Google Code project and its documentation describes a maven 2 plugin:

<plugin>
    <groupId>org.hybridlabs</groupId>
    <artifactId>maven-beautifier-plugin</artifactId>
    <executions>
         <execution>
             <goals>
                 <goal>beautify-imports</goal>
             </goals>
         </execution>
     </executions>
    <configuration>
         <!-- Recursively scan for *.java and beautifies imports -->
         <inputDirectory>${pom.basedir}/..</inputDirectory>
         <!--outputDirectory>${pom.basedir}/..</outputDirectory>
         <runBeautifier>true/runBeautifier>
         <runJalopy>false</runJalopy-->
    </configuration>
</plugin>

There is indeed a mojo in the source tree but it doesn't match the groupId mentioned above (this is a bit confusing) and I've not been able to find the plugin in maven's public repository.

Maybe you'll be more lucky with the version available in AndroMDA plugin repository as documented in this thread (the plugin is indeed present in http://team.andromda.org/maven2/).

The plugin is under org.apache.maven.plugins.maven-beautifier-plugin. It can be run with the short form: mvn beautifier:beautify-imports. It can also be run as part of a project pom by adding the plugin declaration under <build><plugins>:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-beautifier-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>beautify-imports</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <inputDirectory>${pom.basedir}/target/src</inputDirectory>
    </configuration>
</plugin>

Or contact the project's author (e.g. through twitter or by mail).

I think all of you (Eclipse, Emacs or whatever users) should use something like Jalopy which supports both Eclipse and Maven. This way it becomes irrelevant where the code was modified as long as it has been run through pretty-printer as part of checking code in. Said that - I'm not sure if Jalopy supports organizing imports beyond sorting these up

I have also found an ImportScrubber plugin. Can't as of yet attest to its quality.

Does your shop have code standards for how imports should be organized? If so then you are out of luck. Minimizing diffs is a small sacrifice to make towards incremental code improvement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!