开发者

Different Checkstyle Rules For Main And Test In Maven

开发者 https://www.devze.com 2023-03-03 01:55 出处:网络
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:

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&g开发者_如何学Ct;
  <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.

So I would either like to:

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

OR

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


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


What about the following:

   <reporting>
     <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.6</version>
        <reportSets>
          <reportSet>
            <id>main_checks</id>
            <reports>
              <report>checkstyle</report>
            </reports>
            <configuration>
              <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
              <includeTestSourceDirectory>false</includeTestSourceDirectory>
              <configLocation>config/maven_checks.xml</configLocation>
              <outputDirectory>${project.reporting.outputDirectory}/main-checks/</outputDirectory>
            </configuration>
          </reportSet>
          <reportSet>
            <id>test_checks</id>
            <reports>
              <report>checkstyle</report>
            </reports>
            <configuration>
              <sourceDirectory></sourceDirectory>
              <testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory>
              <includeTestSourceDirectory>true</includeTestSourceDirectory>
              <configLocation>config/sun_checks.xml</configLocation>
              <outputDirectory>${project.reporting.outputDirectory}/test-checks/</outputDirectory>
            </configuration>
          </reportSet>
        </reportSets>
      </plugin>
     </plugins>
   </reporting>


I found using this tip, applied to checktyle plugin, simpler. In my case, it looks like this :

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>checkstyle</id>
                            <goals>
                                <goal>check</goal>
                            </goals>
                            <phase>verify</phase>
                            <configuration>
                                <configLocation>some location</configLocation>
                            </configuration>
                        </execution>
                        <execution>
                            <id>checkstyle2</id>
                            <goals>
                                <goal>check</goal>
                            </goals>
                            <phase>verify</phase>
                            <configuration>
                                <configLocation>some other location</configLocation>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
0

精彩评论

暂无评论...
验证码 换一张
取 消