【问题标题】:Maven checkstyle not fired in goalsMaven checkstyle 未在目标中触发
【发布时间】:2021-03-07 17:47:30
【问题描述】:

不知何故,checkstyle 目标并没有被我设置为运行的任务触发。

这是我的POM

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  ...
  <version>1.0-SNAPSHOT</version>

  <name>...</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.idk.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>2.17</version>
          <configuration>
            <failsOnError>true</failsOnError>
            <configLocation>checkstyle.xml</configLocation>
            <consoleOutput>true</consoleOutput>
          </configuration>
          <executions>
            <execution>
              <id>build</id>
              <phase>build</phase>
              <goals>
                <goal>checkstyle</goal>
              </goals>
            </execution>
            <execution>
              <id>verify</id>
              <phase>verify</phase>
              <goals>
                <goal>checkstyle</goal>
              </goals>
            </execution>
            <execution>
              <id>package</id>
              <phase>package</phase>
              <goals>
                <goal>checkstyle</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

但如果我运行 mvn verify,我看不到 checkstyle 已运行:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< ... >-----------------
[INFO] Building Icecream Shop Manager 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ ... ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory ...
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ ... ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to ...
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ ... ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory ...
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ ... ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to ...
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ ... ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running it.unipd.tos.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 s - in it.unipd.tos.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ ... ---
[INFO] Building jar: ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.423 s
[INFO] Finished at: 2020-11-24T22:19:44+01:00
[INFO] ------------------------------------------------------------------------

我错过了什么?我不应该看到类似的东西吗:

[INFO] --- maven-checkstyle-plugin:2.17:checkstyle (default) @ ... ---

【问题讨论】:

  • 一些尝试:使用check 目标而不是checkstyle,并手动运行目标,即man checkstyle:checkstyle
  • @tgdavies 你好,谢谢你的评论,但是mvn verify | grep check 仍然没有返回任何内容,mvn checkstyle:checkstyle 会触发 An error has occurred in Checkstyle report generation.: Failed during checkstyle execution: There is 1 error reported by Checkstyle 6.11.2 with checkstyle.xml ruleset.... 任何想法?
  • 对于&lt;goal&gt;checkstyle&lt;/goal&gt;,验证阶段不应该是check吗?

标签: java maven checkstyle maven-checkstyle-plugin


【解决方案1】:

&lt;pluginManagement&gt; 元素用于配置要继承的插件。您需要将 Checkstyle 插件定义放在 &lt;plugins&gt; 元素下,而不是 &lt;pluginManagement&gt; 元素下。

【讨论】:

  • 我现在感觉很笨
  • 有没有办法查看checkstyle失败报告的错误?...
  • @Berto99 checkstyle 目标会生成一份报告,您可以在target 目录下找到该报告。您可以改用check,如果有任何违规行为,则构建失败(并将它们报告为构建错误)
  • 我认为检查目标会在控制台上显示错误。
  • @tgdavies 不幸的是,我在控制台上只看到 There is 1 error reported by Checkstyle 6.11.2 with checkstyle.xml ruleset. -&gt; [Help 1]
猜你喜欢
  • 2019-12-02
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 2011-05-04
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
相关资源
最近更新 更多