How to set checkstyle configuration from Gradle in IDEA

岁酱吖の 提交于 2019-12-21 05:31:27

问题


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.


回答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/.




回答2:


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.



来源:https://stackoverflow.com/questions/45051175/how-to-set-checkstyle-configuration-from-gradle-in-idea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!