Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand

As per https://gerrit.onap.org/r/#/c/79312/1/onap-java-formatter.xml, an ONAP Eclipse Java Formatter XML file has been merged and can be imported into Eclipse. Updates to the settings should be committed and merged back into the repository (e.g.

Jira
serverONAP JIRA
serverId425b2b0a-557c-3c0c-b515-579789cceedb
keyAAI-2198
) . TBC: This should probably be in oparent for all to share.

Maven Plugins to Reformat Java code

Using the Eclipse Formatter file above and maven plugin configuration in pom.xml as per https://gerrit.onap.org/r/#/c/79312/1/pom.xml (https://code.revelc.net/formatter-maven-plugin/ for Eclipse formatter and https://github.com/diffplug/spotless/tree/master/plugin-maven for import order)

Code Block
languagexml
titleExample pom.xml configuration
collapsetrue
            <plugins>
                <!--
                Using https://code.revelc.net/formatter-maven-plugin/ for Eclipse formatter
                Using https://github.com/diffplug/spotless/tree/master/plugin-maven for import order
                Use in combination to rewrite code and imports, then checkstyle
                
                mvn formatter:format spotless:apply process-sources
                -->
                <plugin>
                     <groupId>net.revelc.code.formatter</groupId>
                     <artifactId>formatter-maven-plugin</artifactId>
                     <version>2.8.1</version>
                     <configuration>
                        <configFile>${project.parent.basedir}/onap-java-formatter.xml</configFile>
                     </configuration>
                     <!-- https://code.revelc.net/formatter-maven-plugin/
                          use mvn formatter:format to rewrite source files
                          use mvn formatter:validate to validate source files -->
                 </plugin>
                <plugin>
                  <groupId>com.diffplug.spotless</groupId>
                  <artifactId>spotless-maven-plugin</artifactId>
                  <version>1.18.0</version>
                  <configuration>
                    <java>
                     <importOrder>
                       <order>com,java,javax,org</order>
                     </importOrder>
                    </java>
                  </configuration>
                <!-- https://github.com/diffplug/spotless/tree/master/plugin-maven
                     use mvn spotless:apply to rewrite source files
                     use mvn spotless:check to validate source files -->
                </plugin>
            </plugins>


the combination can be used in a maven command to rewrite code and imports, then checkstyle audit like so

Code Block
titleExample maven command
mvn formatter:format spotless:apply process-sources

Modify settings to adapt to ONAP

  1. Select "Window -> Preferences".
  2. In the filter text box, type "tab".
  3. Under "Editors -> Text Editors", set "Displayed tab width:" to 4.
  4. Under "Editors -> Text Editors", activate "Insert spaces for tabs".
  5. If you want a visual aid for the print margin, under "Editors -> Text Editors", activate "Show print margin" and set "Print margin column:" to 120.
  6. Under "XML -> XML Files -> Editor", change to "Indent using spaces".
  7. Clear the filter text box and type"format".
  8. Under "Java -> Code Style Formatter", for "Active profile:" select GoogleStyle.
  9. Press the "Edit..." button.
  10. Expand the "Indentation" section.
  11. Select "Spaces only" for "Tab policy", and "4" for "Indentation size".
  12. Expand the "Line Wrapping" section.
  13. Set "Maximum line width:" to "120", and press "Apply".
  14. Expand the "Comments" section.
  15. Set "Maximum width for comments:" to "120"
  16. Unset "Count width from comment's starting position"
  17. Unset "Enable header comment formatting", and press "Apply" and then "OK"
  18. Clear the filter text box and type "save actions".
  19. Under "Java -> Editor -> Save Actions", check the "Perform the selected actions on save", and "Additional actions" check boxes.
  20. Press the "Configure..." button.
  21. On the "Code Organizing" tab in the "Formatter" box, check the "Remove trailing whitespace" check box.
  22. Press "OK".
  23. Select "Java -> Editor -> Content Assist -> Favorites".
  24. For each of the types below, press "New Type..." and type in the type and press "OK".
    org.junit.Assert
    org.junit.Assume
    org.junit.jupiter.api.Assert
    org.junit.jupiter.api.Assumptions
    org.junit.jupiter.api.DynamicContainer
    org.junit.jupiter.api.DynamicTest
    org.mockito.ArgumentMatchers
    org.mockito.Mock
    org.mockito.Mockito
  25. Select "Java -Code Style -> Organize Imports".
  26. Remove all items from the list.
  27. Press "Apply and Close".

To format a file open in an editor after these style preferences, press "Shift + Ctrl + F".



Install useful plugins

Install EclEmma for code coverage, and SonarLint for static code analysis.

...