简体   繁体   English

如何配置Maven Checkstyle插件以允许一行上进行多个分配?

[英]How to configure maven checkstyle plugin to allow for multiple assignments on a line?

I am using the maven checkstyle plugin and running into this error: 我正在使用Maven Checkstyle插件并遇到此错误:

InnerAssignment: Inner assignments should be avoided.

I read about this error here . 在这里了解到此错误。 Although this check makes sense to me, I would like to configure the plugin so that it does not complain about inner assignments of the form: 尽管此检查对我来说很有意义,但我还是想配置插件,以便它不会抱怨表单的内部分配:

i = j = k = l = m = 0;

which is what I really have in my code. 这就是我在代码中真正拥有的。 How can this be done? 如何才能做到这一点?

You need to define your own rules by providing your own checkstyle.xml file as next: 您需要通过如下提供自己的checkstyle.xml文件来定义自己的规则:

In your pom file, configure the plugin as next 在您的pom文件中,将插件配置为下一个

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <configLocation>${pom.basedir}/checkstyle.xml</configLocation>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

Then take the default checkstyle.xml file from the checkstyle project and simply customize it by removing the module InnerAssignment . 然后从checkstyle项目中获取默认的checkstyle.xml文件,只需删除模块InnerAssignment即可对其进行自定义。 To do so simply remove the following line from your own checkstyle.xml file: 为此,只需从您自己的checkstyle.xml文件中删除以下行:

<module name="InnerAssignment"/>

Response Update: 回应更新:

Actually the way we can customize this rule cannot fulfill your requirements anyway because you can decide to enable by assign type not by the way you do it. 实际上,我们自定义此规则的方式始终无法满足您的要求,因为您可以决定通过分配类型来启用而不是通过您的方式来启用。 I mean if you decide to disable it for = you would actually disable it for i = j = k = l = m = 0; 我的意思是,如果您决定为=禁用它,则实际上将在i = j = k = l = m = 0;禁用它i = j = k = l = m = 0; but also String s = Integer.toString(i = 2); 而且还有String s = Integer.toString(i = 2); which is not what you want. 这不是你想要的。 Moreover I did the test and even what we have in the doc is not working, it seems that this rule is not even configurable, so you keep it as it is or you remove it as proposed above. 此外,我进行了测试,甚至文档中的内容都无法正常工作,看来该规则甚至是不可配置的,因此请按原样保留它,或者按照上面的建议删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM