Can I use or import an Eclipse formatter in NetBeans?

后端 未结 8 1310
猫巷女王i
猫巷女王i 2021-02-06 23:40

We have been using Eclipse for a long time. We have formatter for each of our projects. Now, some of us are moving to NetBeans. Is it possible to migrate / sync / import our Ecl

8条回答
  •  情书的邮戳
    2021-02-06 23:46

    AFAIK There is no way to import the Eclipse formatting preferences into NetBeans. But if you use Maven you can use the Maven2 Java Formatter Plugin which uses the Eclipse JDT features to format source files and integrates nicely with Maven projects. For a usage of current version 0.3.1 see here.

    This is a simple pom.xml using the formatter. Just put in any Java source file and run mvn java-formatter:format, your source should be formatted using the given Eclipse configuration file (where you have to provide the path to your own one).

    
    
        4.0.0
    
        de.mackaz
        maven.java.formatter.plugin.example
        0.1
        jar
    
        
            
                
                    com.googlecode.maven-java-formatter-plugin
                    maven-java-formatter-plugin
                    0.3.1
                    
                        
                            
                                format
                            
                        
                    
                    
                    ./example-eclipse-code-format-config.xml
                    
                
            
        
    
    

    EDIT:

    A different approach is to invoke the Eclipse Code Formatter from commandline, (but takes a bit longer and is not nicely integrated into the Maven lifecycle), as described here. You can format a sourcefile with it like this:

    /opt/eclipse/eclipse -application org.eclipse.jdt.core.JavaCodeFormatter -verbose -nosplash -config example-eclipse-code-format-config.xml src/main/java/de/mackaz/FormatMe.java
    

    (Also take a look at this page where the author put that into a little script)

    It should be easy to integrate one of those approaches into NetBeans. Either wrap those inside an Ant file and add that to your NetBeans commands, or write a little NetBeans plugin to add the action e.g. to the contextmenu (which is pretty easy, especially compared to writing Eclipse Plugins).

提交回复
热议问题