Maven checkstyle插件:向控制台打印警告

时间:2012-05-07 19:46:05

标签: maven-plugin maven-3 checkstyle

我似乎无法弄清楚当我运行检查目标时如何让maven-checkstyle-plugin向控制台打印警告。

如果我配置为:

<module name="Checker">
    <module name="TreeWalker">
        <module name="NPathComplexity">
            <property name="max" value="5" />
            <property name="severity" value="warning" />
        </module>
    </module>
</module>

...我在控制台中看不到任何内容,检查不会失败。如果我将严重性更改为错误,则检查失败。

有什么想法吗?我错过了什么吗?

插件配置如下

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <includeTestSourceDirectory>true</includeTestSourceDirectory>
                    <logViolationsToConsole>true</logViolationsToConsole>
                    <configLocation>
                        rules.xml</configLocation>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

3 个答案:

答案 0 :(得分:5)

我有类似的问题。这个配置对我有用。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.9.1</version>
    <configuration>
        <consoleOutput>true</consoleOutput>
    </configuration>
    <executions>
        <execution>
           <id>checkstyle</id>
           <goals>
               <goal>checkstyle</goal>
           </goals>
           <phase>prepare-package</phase>
        </execution>
    </executions>
</plugin>

答案 1 :(得分:1)

<violationSeverity>warning</violationSeverity>添加到pom.xml中的<configuration>为我工作:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <configuration>
                <logViolationsToConsole>true</logViolationsToConsole>
                <configLocation>checkstyle.xml</configLocation>
                <violationSeverity>warning</violationSeverity>
            </configuration>
            <executions>
            ...
            </executions>
            ...
        </plugin>

另外,我在checkstyle.xml中有这个:

       <module name="Checker">

          <module name="SeverityMatchFilter">
              <!-- report all violations except ignore -->
              <property name="severity" value="ignore"/>
              <property name="acceptOnMatch" value="false"/>
          </module>

          ...

      </module>

答案 2 :(得分:0)

我假设您必须明确添加检查目标,以便将信息打印到控制台mentioned in the documentation

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.9.1</version>
    <executions>
      <execute>
        <id>check</id>
        <goals><goal>check</goal></goals>
        <phase>prepare-package</phase>
      <execute>
    </executions>
  </plugin>

根据您调用mvn的方式,您需要更改执行检查的阶段。 maven-checkstyle-plugin的默认绑定是verfiy阶段。您可以在执行块中给出明确的阶段,或者只是执行mvn clean verify,并且可以省略阶段输入。