Skip to main content

Java Code Formatting Specification

The Java portion of Apache Doris code (mainly the FE module) is typically formatted automatically by the IDE. This document lists the general formatting rules and the corresponding configuration methods for different IDEs. The CI pipeline validates code formatting through formatter-check.

Import Order Rules

Import statements must be arranged in the following group order, with one blank line separating each group:

org.apache.doris
<blank line>
third party package
<blank line>
standard java package
<blank line>

Additional rules:

RuleDescription
No import *Every class must be imported explicitly
No import staticStatic imports are not allowed

Checkstyle Check at Compile Time

When compiling with maven, the Checkstyle check runs by default, which slightly slows down compilation. To skip the check, use the following command:

mvn clean install -DskipTests -Dcheckstyle.skip

The formatter-check in CI enforces code format validation. Skipping the check locally is only for development and debugging. Make sure the format complies before submitting a PR.

Checkstyle Plugin Configuration

Configure Checkstyle in IDEA

  1. Install the Checkstyle-IDEA plugin in the settings.
  2. Go to Tools -> Checkstyle, find Configuration File, and click Use a local Checkstyle file.
  3. Select the fe/check/checkstyle/checkstyle.xml file in the project root directory.
  4. Confirm that the Checkstyle version is 9.3 or above (the latest version is recommended).

Once configured, you can use the Checkstyle-IDEA plugin to run Checkstyle checks on the code:

Configure Checkstyle in VS Code

  1. Install the Checkstyle for Java plugin.
  2. Complete the configuration by following the instructions and animations in the Java Linting official documentation.

IDEA Code Formatting

Auto-Formatting Configuration

The auto-formatting feature of IDEA is recommended:

  1. Go to Preferences -> Editor -> Code Style -> Java.
  2. Click the configuration icon's Import Scheme and select IntelliJ IDEA code style XML.
  3. Select the build-support/IntelliJ-code-format.xml file in the project root directory.

Rearrange Code

Checkstyle checks the order of code declarations according to Class and Interface Declarations.

After importing the build-support/IntelliJ-code-format.xml above, use Code/Rearrange Code to perform the sorting automatically:

Automatically Remove Unused Imports

PurposeAction
Remove unused imports onlyDefault shortcut CTRL + ALT + O
Automatically remove and reorder on saveCheck Preferences -> Editor -> General -> Auto Import -> Optimize Imports on the Fly

FAQ

Q: How do I troubleshoot a formatter-check failure in CI?

Run the check locally with IDEA or the Checkstyle plugin. Make sure the import order, naming conventions, and declaration order all comply with the requirements in fe/check/checkstyle/checkstyle.xml, then resubmit.

Q: Does skipping Checkstyle locally affect PR merging?

Yes. CI still runs formatter-check. The local -Dcheckstyle.skip option is only for speeding up development builds. The final code must pass Checkstyle.