How to set checkstyle configuration from Gradle in

2019-03-31 18:44发布

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.

2条回答
何必那么认真
2楼-- · 2019-03-31 19:06

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.

查看更多
Ridiculous、
3楼-- · 2019-03-31 19:21

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

查看更多
登录 后发表回答