如何在IDEA中从Gradle中设置checkstyle配置 [英] How to set checkstyle configuration from Gradle in IDEA

查看:1139
本文介绍了如何在IDEA中从Gradle中设置checkstyle配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IDEA中的Checkstyle插件。我想为不同的模块设置不同的checkstyle配置。我使用gradle作为构建工具 - 版本4,我想编写一个修改模块的相应.iml文件的任务。 任何想法如何做到这一点?



我第一次尝试修改iml文件以查看 here $ b

  apply plugin:'idea'


任务setCheckStylePluginSettings {
group =Idea
description =将CheckStyle插件设置复制到Idea工作区。

println步骤1。
idea.module.iml {
withXml {xmlProvider - >
//获取根节点。
println第2步
def project = xmlProvider.asNode()
}
}
}

但是,我坚持只是在开始时我不能看到控制台上打印的 Step 2

解决方案

所以问题解决了。我已经尝试了Thomas Jansen在此问题中提出的解决方案。

但我会提供更多关于如何操作的信息。



为了给不同的checkstyle模块到不同的源代码组,您需要在模块中定义id标签。如下所示:

 < module name =ConstantName> 
< property name =idvalue =ConstantNameMain/>
< property name =severityvalue =error/>
< property name =applyToPrivatevalue =false/>

< / module>
< module name =ConstantName>
< property name =idvalue =ConstantNameTest/>
< property name =severityvalue =error/>
< property name =applyToPrivatevalue =false/>
< property name =formatvalue =^ [A-Z] [A-Za-z0-9] *(_ [A-Z0-9] +)* $/>
< / module>

然后我们为 suppression.xml定义 SuppressionFilter strong>,它可以位于与 checkstyle.xml 相同的文件夹中。一个重要的事情是将 SuppressionFilter 模块定位为 Checker 模块。

 < module name =Checker> 
< module name =SuppressionFilter>
< property name =filevalue =./ suppressions.xml/>
< / module>
< module name =TreeWalker>



< / module>
< / module>

然后,我们定义如下所示的suppression.xml文件:

 < suppressions> 
<! - >测试来源抑制< /! - >
< suppress files =[\\ /] src [\\ /] test [\\ /].*id =ConstantNameMain/>

<! - >主要来源抑制< /! - >
< suppress files =[\\ /] src [\\ /] main [\\ /].*id =ConstantNameTest/>
< / suppressions>

Aaaaa最后,配置您的Checkstyle-IDEA插件,从设置>编辑器激活实时扫描>检查> Checkstyle ,你就完成了。


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.

解决方案

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.

这篇关于如何在IDEA中从Gradle中设置checkstyle配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆