4

I am using the Checkstyle plugin in IDEA. I want to set up different checkstyle configurations to my different modules. I am using gradle as build tool-version 4- and I want to write a task that modifies the corresponding .iml files of the modules. Any idea how to do that?

My very first attempt in modifying the iml file looking over here

apply plugin: 'idea'


task setCheckStylePluginSettings {
    group = "Idea"
    description = "Copies CheckStyle plugin settings to Idea workspace."

    println "Step 1."
    idea.module.iml {
    withXml {xmlProvider ->
        // Get root node.
        println "Step 2."
        def project = xmlProvider.asNode()
        }
    }
 }

However, I am stuck just at the beginning that I cant event see the Step 2 printed on the Console.

Guillotine1789
  • 342
  • 4
  • 12

2 Answers2

1

A "module" in IntelliJ is a one-to-one mapping to a SourceSet in Gradle, assuming you imported the project with the "Create separate modules per source set" option checked.

By default, the Checkstyle plugin adds tasks for each source set that is added to the build. So, you should already have the tasks checkstyleMain and checkstyleTest when you apply the java plugin. These tasks are effectively what you're looking for.

Now, to customize them, in your build.gradle, configure them like so:

checkstyleMain {
    configFile = file("${rootDir}/checkstyle/main.xml")
}

checkstyleTest {
    configFile = file("${rootDir}/checkstyle/test.xml")
}

This assumes that you have different Checkstyle configuration files in your project at ${rootDir}/checkstyle/.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • Yes I have created dirfferent modules for sourcesets. But I dont think the solution will configure **IDEA Checkstyle** plugin, this will just configure the gradle task, am I wrong? I guess IDEA plugin will be checking against **.iml** file and the solution you suggest will not do any good to it. I want is to make IDEA checkstyle plugin to actively check against violations with correct configuration for each module. Thanks. – Guillotine1789 Jul 12 '17 at 21:16
  • No, it won't configure the plugin used in the editor. What you're suggesting is error prone and not repeatable in multiple environments (devs who don't use IntelliJ, CI or any build environments, etc). There's no public documentation for the IntelliJ checkstyle plugin's configuration format or how it works, so they're free to update / change it for different versions of IntelliJ (which I'm sure they've done). – nickb Jul 12 '17 at 23:23
  • You' re right that it may change. I have checked how IDEA modifies its .iml file when you added a checkstyle configuration to it and my aim was to automate it just for developer side of view to see the checkstyle effects actively while you develop. In CI or anything we will be still triggering from the gradle task. To be honest, this was way easier to do in Eclipse. Thanks for the help anyways. – Guillotine1789 Jul 13 '17 at 06:11
0

So the problem is solved. I have tried the solution proposed by Thomas Jansen in this question.

But I will give more information on how to do it.

In order to give different checkstyle modules to different sourcesets you need to define id tag in the module. Shown below:

  <module name="ConstantName">
     <property name="id" value="ConstantNameMain"/>
     <property name="severity" value="error"/>
     <property name="applyToPrivate" value="false"/>
     <property name="format" value="^[A-Z][A-Za-z0-9]*(_[A-Za-z0-9]+)*$"/>
  </module>
  <module name="ConstantName">
     <property name="id" value="ConstantNameTest"/>
     <property name="severity" value="error"/>
     <property name="applyToPrivate" value="false"/>
     <property name="format" value="^[A-Z][A-Za-z0-9]*(_[A-Z0-9]+)*$"/>
  </module>

Then we define SuppressionFilter module for suppression.xml which can be located at the same folder with your checkstyle.xml. One important thing is to locate the SuppressionFilter module as Checker module.

<module name="Checker">
  <property name="severity" value="warning"/>
   <module name="SuppressionFilter">
       <property name="file" value="./suppressions.xml"/>
   </module>
  <module name="TreeWalker">
  .
  .
  .
  </module>
</module>

Then, we define the suppression.xml file as below:

<suppressions>
    <!-- >Test sources suppressions</!-->
    <suppress files="[\\/]src[\\/]test[\\/].*" id="ConstantNameMain" />

    <!-- >Main sources suppressions</!-->
    <suppress files="[\\/]src[\\/]main[\\/].*" id="ConstantNameTest" />
</suppressions> 

Aaaaaand lastly, configure your Checkstyle-IDEA plugin, activate real time scan from Settings>Editor>Inspections>Checkstyle and you are done.

Guillotine1789
  • 342
  • 4
  • 12