提问者:小点点

Maven checkstyle未作为构建的一部分运行


我完全不知道为什么不。结构是这样的:

使用mvn checkstyle:check创建报告(我在报告部分有相同的设置),但build将插件视为不存在。如果我更改checkstyle版本,它不会下载它,等等。本质上它将此配置视为不可见。

<build>
   <pluginManagement>
         <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-checkstyle-plugin</artifactId>
              <version>2.13</version>

              <configuration>
                <configLocation>checkstyle.xml</configLocation>
                <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
                <maxAllowedViolations>2500</maxAllowedViolations>
                <consoleOutput>true</consoleOutput>
                <failsOnError>true</failsOnError>
                <failOnViolation>true</failOnViolation>
              </configuration>

              <executions>
                <execution>
                  <id>checkstyle</id>
                  <goals>
                    <goal>check</goal>
                  </goals>
                  <configuration>

                    <encoding>UTF-8</encoding>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>true</failsOnError>
                    <failOnViolation>true</failOnViolation>
                  </configuration>
                </execution>
              </executions>
            </plugin>
.....
</plugins>
</pluginManagement>
</build>

共1个答案

匿名用户

您可以按如下方式将插件添加到pom,它将作为构建的一部分执行(mvn清洁安装):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <configLocation>checkstyle.xml</configLocation>
    <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
    <consoleOutput>true</consoleOutput>
    <maxAllowedViolations>2500</maxAllowedViolations>
    <failsOnError>true</failsOnError>
    <failOnViolation>true</failOnViolation>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

另请参阅:Checkstyle不工作

实际上,对于大多数这些配置参数(除了没有“用户属性”的ailsOnError),您可以将它们作为属性放在您的pom. xml中:

  <properties>
    ...
    <checkstyle.config.location>checkstyle.xml</checkstyle.config.location>
    <checkstyle.suppressions.location>checkstyle-suppressions.xml</checkstyle.suppressions.location>
    <checkstyle.consoleOutput>true</checkstyle.consoleOutput>
    <checkstyle.maxAllowedViolations>2500</checkstyle.maxAllowedViolations>
    <checkstyle.failOnViolation>true</checkstyle.failOnViolation>
  </properties>

这样,为了方便起见,您可以从插件配置部分中删除它们,尽管这两种方法都有效。有关“用户属性”值等,请参阅https://maven.apache.org/plugins/maven-checkstyle-plugin/check-mojo.html。