简体   繁体   中英

Organize imports with Maven2, Eclipse-style?

I'm a lone Emacs user in a group that's hung up on Eclipse. Time and again, code diffs are made larger by imports that have been "organized" or "cleaned up" by Eclipse.

I don't have a strong opinion on the subject of import style in Java, but I do want to minimize our diffs. What can I do?

We use Maven2 for builds; is there a plugin that can help me? Can a piece of Eclipse be abstracted out & turned into a Maven plugin? I currently use ImportScrubber and have encountered a few other stand-alone pieces that do the same thing. What I'm really looking for is a Maven plugin that can integrate with the build.

Barring the above, I'll put an ImportScrubber invocation into my .git/hooks/post-commit .

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 (eg 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.

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