How do I get Checkstyle CustomImportOrder to work with IntelliJ properly?

15,299

Solution 1

Default formatting in IntelliJ looks like as follows:

all other imports
<blank line>
javax.* in alphabetical order
java.* in alphabetical order
<blank line>
static imports in alphabetical order

Right now, it is not possible to sort java and javax separately without blank line in-between and that's why you have the violations.

I've raised issue on GitHub to solve that and it will require changes in Checkstyle code.

As a workaround you may add blank line between javax and java in IntelliJ IDEA configuration and then it should be easy to tune Checkstyle to work with that.

Solution 2

Don't split java and javax in separate groups - just let javax stand before java packages.

checkstyle.xml:

...
    <module name="CustomImportOrder">
        <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
        <property name="sortImportsInGroupAlphabetically" value="true"/>
        <property name="standardPackageRegExp" value="^(java|javax)\."/>
        <property name="separateLineBetweenGroups" value="true"/>
    </module>
...

checkstyleSuppressions.xml

...
<!--  Let javax.* stand before java.* - that's default Idea settings  -->
<suppress checks="CustomImportOrder"
          message="Wrong lexicographical order for 'java\.[^']+' import\. Should be before 'javax\.[^']+'\."/>
...

You can also omit properties that match default values (standardPackageRegExp and separateLineBetweenGroups for today) - please check actual documentation.

Share:
15,299
Roger
Author by

Roger

I'm a Java developer, mostly working on enterprise applications/systems. I have a background in PL1, FormScape and .NET (C#) although that is history now. =)

Updated on June 06, 2022

Comments

  • Roger
    Roger almost 2 years

    I'm trying to get Checkstyle (via maven-checkstyle-plugin) to have my IntelliJ imports checked by using the Checkstyle CustomImportOrder module. Despite having ordered my imports according to IntelliJ's default rules, Checkstyle still says the import order is wrong.

    Here's my imports (ordered according to IntelliJ rules (ctrl+o):

    import org.codehaus.jackson.JsonNode;
    
    import javax.sql.rowset.serial.SQLOutputImpl;
    import java.util.ArrayList;
    import java.util.List;
    

    Here's the warning message from Checkstyle:

    [WARNING] src\main\java\com\example\hej\EnKlass.java[5] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'SPECIAL_IMPORTS' group.
    [WARNING] src\main\java\com\example\hej\EnKlass.java[6] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'STANDARD_JAVA_PACKAGE' group.
    [WARNING] src\main\java\com\example\hej\EnKlass.java[7] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'STANDARD_JAVA_PACKAGE' group.
    

    Here's my checkstyle.xml CustomImportOrder rules (as recommended for IntelliJ by the Checkstyle site):

    <module name="CustomImportOrder">
        <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
        <property name="specialImportsRegExp" value="^javax\."/>
        <property name="standardPackageRegExp" value="^java\."/>
        <property name="sortImportsInGroupAlphabetically" value="true"/>
        <property name="separateLineBetweenGroups" value="false"/>
    </module>
    

    What might be the problem here? I have been trying to switch the rules around, with no luck. I have also tried to remove/manipulate the regexes with no luck.

  • Roger
    Roger over 8 years
    Thanks. I figured this was the case. I will let this check be until it's fixed, as we don't want to change any IDEA configuration at the moment.