javamavenintellij-ideacheckstyle

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


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.


Solution

  • 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.