Maven中主要和测试的不同Checkstyle规则 [英] Different Checkstyle Rules For Main And Test In Maven

查看:118
本文介绍了Maven中主要和测试的不同Checkstyle规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个Maven项目,以便能够使用两个不同的规则集运行checkstyle:一个用于主规则集,一个用于测试.我想做的事情大致如下:

I am trying to setup a maven project such that I will be able to run checkstyle using two distinct rule sets: one for main, and one for test. What I would like to do is something along the lines of:

<reporting>
  <plugins>
    <!-- One configuration for main -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <configuration>
        <configLocation>checkstyle</configLocation>
      </configuration>
    </plugin>
    <!-- But a different set of rules for test -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <configuration>
        <sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
        <configLocation>checkstyle-test.xml</configLocation>
      </configuration>
    </plugin>
  </plugins>
</reporting>

我已经能够成功运行一个版本或另一个版本,但不能同时运行两个版本.包括尝试将它们绑定到不同的执行阶段,但似乎经验法则是最后一个定义是唯一使用的定义.

I have been successfully able to run one version or the other, but not both at the sames time. Including trying to bind them to different executions phases, but it seems the rule of thumb is the last definition is the only one used.

所以我想:

-理想情况-具有两个可以独立运行的单独的checkstyle配置文件

-Ideally - have two seperate checkstyle configuration files that can be run independently

OR

-具有单个checkstyle配置,使用config属性includeTestSourceDirectory来同时检查main和test,但是有一些规则选择性地应用于main/test或另一个.

-Have a single checkstyle config, use the config property includeTestSourceDirectory to check main and test at the same time, but have some rules selectively applied to one of main/test or the other.

推荐答案

我无法找到任何机制,可以在编译时将不同的Checkstyle规则应用于不同的源根(其他答案可能适用于生成报告,但我的需求是专门进行编译时间检查).实施的解决方案是使规则适用于源,然后使用checkstyle抑制机制排除测试类上的某些规则,即

I have been unable to find any mechanism by which different checkstyle rules can be applied to different source roots at compile time (the other answer may work for report generation, but my needs are specifically compile time checking). The solution implemented is to have the rules that apply to source and then use the checkstyle supressions mechanism to exclude certain rules on the test classes i.e.

<suppress checks="CheckInappropiateForTests" files=".*Test\.java"/>

这篇关于Maven中主要和测试的不同Checkstyle规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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