Versions Compared

Key

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

...

Follow instructions on: Installing the coding style settings in eclipse.

See also Java code style

ONAP Eclipse Java Formatter (Optional)

...

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

Install useful plugins

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

Set up Sonar towards ONAP

To bind your projects to the ONAP Sonar server, follow the instructions below. Your projects should be imported in to Eclipse before this.

  1. Right click on the project and select "SonarLint -> Bind to SonarQube or SonarCloud...".
  2. Select "sonarcloud" and press "Next".
  3. Click "Generate Token"
  4. A browser opens and you are taken to a Sonarcloud login page
  5. Login with an appropriate account from the list presented, most likely your GitHub account
  6. You are now directed to a sonarcloud token generation page
  7. Enter a name for your token and click "Generate"
  8. Copy the token hex string that is generated from the browser and paste it into the "Token" field in Eclipse and click "Next"
  9. In the "Organization" field, enter the string "onap" and press "Next"
  10. The Connection name "SonarCloud/onap" should be found by the system, click "Next"
  11. The connection should be successfully created, click "Finish"
  12. Press "Add...".
  13. Select the projects you want to add and press "OK".
  14. Press "Next".
  15. Start typing the name of your project, and it should appear in a list box where it should be selected.
  16. Press "Finish".

To see messages from Sonar introduced by edits made in the projects, select "Window -> Show Wiew -> Other...". Expand "SonarLint" and select "SonarLint -> On-The-Fly".

Setting up the ONAP Checkstyle in Eclipse

Set "ONAP" configuration in Eclipse

To set the newly built checkstyle files in Eclipse:

  1. Preferences->Checkstyle
  2. Click "New"
  3. Select "External Configuration File"
  4. Give it a name eg ONAP
  5. Point at the file <your_git_folder>/oparent/checkstyle/src/main/resources/onap-checkstyle/onap-java-style.xml  
    (assuming you have downloaded the oparent repo)
  6. Click OK
  7. Select "ONAP" configuration and click "Set as Default"
  8. Select "Apply and Close"

Apply "ONAP" configuration to a project in Eclipse

Now we need to activate the checkstyle on one project and set it as the blueprint for all of them:
Select a project in eclipse and right click->Properties->Checkstyle
check "Checkstyle active for this project"
Select the "ONAP" checkstyle profile
Click "Apply and Close"

Spread blueprint to other projects in Eclipse

Now spread the profile to all other projects:

  1. Select all the projects you want to apply the profile to in the Eclipse project explorer (not the one that you set up above)
  2. Right click->Checkstyle->Configure projects from blueprint
  3. Select the project you set up above
  4. Now all the projects have the correct checkstyle setup.


Hack oparent to fix "curly bracket" issue

Expand

The first issue is that the current ONAP master tagged version of the checkstyle does not work with Eclipse Oxygen/Photon (and maybe other versions) because of the "curly bracket" issue. There is a fix on the way but it's not here yet. The current tagged version of oparent we are using in Policy is 1.2.1.

Hack oparent to fix "curly bracket" issue

To get around this issue, check out oparent, checked out the 1.2.1 tag, fixed the "curly bracket" bug and built it on my local machine.

  1. Check out oparent
  2. git tag -l
  3. git co tags/1.2.3 -b 1.2.3
  4. If you want CheckStyle to ignore generated files, do the following:
    1. Add the following tag to the above file:
      <module name="SuppressionFilter">
        <property name="file" value="<absolute path to the directory of the file>/suppressions.xml"/>
      </module>
    2. Create a file called "suppressions.xml" in the folder given above, and put the following content in it:
      <?xml version="1.0"?>
      <!DOCTYPE suppressions PUBLIC
        "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
        "https://checkstyle.org/dtds/suppressions_1_2.dtd">
      <suppressions>
        <suppress files="[\\/]generated-sources[\\/]" checks="[a-zA-Z0-9]*"/>
      </suppressions>
  5. The versions in the POMs in oparent are snapshot, so we need to change those to 1.2.1
     mvn versions:set -DnewVersion=1.2.1
  6. Now build locally:
    mvn clean install

ONAP Eclipse Java Formatter (Optional)

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

Install useful plugins

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

Set up Sonar towards ONAP

To bind your projects to the ONAP Sonar server, follow the instructions below. Your projects should be imported in to Eclipse before this.

  1. Right click on the project and select "SonarLint -> Bind to SonarQube or SonarCloud...".
  2. Select "sonarcloud" and press "Next".
  3. Click "Generate Token"
  4. A browser opens and you are taken to a Sonarcloud login page
  5. Login with an appropriate account from the list presented, most likely your GitHub account
  6. You are now directed to a sonarcloud token generation page
  7. Enter a name for your token and click "Generate"
  8. Copy the token hex string that is generated from the browser and paste it into the "Token" field in Eclipse and click "Next"
  9. In the "Organization" field, enter the string "onap" and press "Next"
  10. The Connection name "SonarCloud/onap" should be found by the system, click "Next"
  11. The connection should be successfully created, click "Finish"
  12. Press "Add...".
  13. Select the projects you want to add and press "OK".
  14. Press "Next".
  15. Start typing the name of your project, and it should appear in a list box where it should be selected.
  16. Press "Finish".

To see messages from Sonar introduced by edits made in the projects, select "Window -> Show Wiew -> Other...". Expand "SonarLint" and select "SonarLint -> On-The-Fly".

Setting up the ONAP Checkstyle in Eclipse

Set "ONAP" configuration in Eclipse

To set the newly built checkstyle files in Eclipse:

  1. Preferences->Checkstyle
  2. Click "New"
  3. Select "External Configuration File"
  4. Give it a name eg ONAP
  5. Point at the file <your_git_folder>/oparent/checkstyle/src/main/resources/onap-checkstyle/onap-java-style.xml  
    (assuming you have downloaded the oparent repo)
  6. Click OK
  7. Select "ONAP" configuration and click "Set as Default"
  8. Select "Apply and Close"

Apply "ONAP" configuration to a project in Eclipse

Now we need to activate the checkstyle on one project and set it as the blueprint for all of them:
Select a project in eclipse and right click->Properties->Checkstyle
check "Checkstyle active for this project"
Select the "ONAP" checkstyle profile
Click "Apply and Close"

Spread blueprint to other projects in Eclipse

Now spread the profile to all other projects:

  1. Select all the projects you want to apply the profile to in the Eclipse project explorer (not the one that you set up above)
  2. Right click->Checkstyle->Configure projects from blueprint
  3. Select the project you set up above
  4. Now all the projects have the correct checkstyle setup.

Hack oparent to fix "curly bracket" issue

Expand

The first issue is that the current ONAP master tagged version of the checkstyle does not work with Eclipse Oxygen/Photon (and maybe other versions) because of the "curly bracket" issue. There is a fix on the way but it's not here yet. The current tagged version of oparent we are using in Policy is 1.2.1.

Hack oparent to fix "curly bracket" issue

To get around this issue, check out oparent, checked out the 1.2.1 tag, fixed the "curly bracket" bug and built it on my local machine.

  • Check out oparent
  • git tag -l
  • git co tags/1.2.3 -b 1.2.3
  • If you want CheckStyle to ignore generated files, do the following:
    1. Add the following tag to the above file:
      <module name="SuppressionFilter">
        <property name="file" value="<absolute path to the directory of the file>/suppressions.xml"/>
      </module>
    2. Create a file called "suppressions.xml" in the folder given above, and put the following content in it:
      <?xml version="1.0"?>
      <!DOCTYPE suppressions PUBLIC
        "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
        "https://checkstyle.org/dtds/suppressions_1_2.dtd">
      <suppressions>
        <suppress files="[\\/]generated-sources[\\/]" checks="[a-zA-Z0-9]*"/>
      </suppressions>
  • The versions in the POMs in oparent are snapshot, so we need to change those to 1.2.1
     mvn versions:set -DnewVersion=1.2.1
  • Now build locally:
    mvn clean install


    Install useful plugins

    IntelliJ

    ...