2

我是 maven 和 chekstyle 的新手,所以需要问一些问题......我想在基于 maven 的项目中使用 checkstyle,所以在我的项目中pom.xml添加了依赖项

<dependency>
   <groupId>checkstyle</groupId>
   <artifactId>checkstyle</artifactId>
   <version>2.4</version>
</dependency>

而且我还在插件标签中添加了条目:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>2.4</version>
  <configuration>
    <enableRulesSummary>true</enableRulesSummary>
    <configLocation>checkstyle.xml</configLocation>
  </configuration>
</plugin>

但是当我使用 command 运行我的 maven 构建时mvn clean install, checkstyle 什么也没做。而且由于我checkstyle.xml的系统中还没有任何东西,它不应该抱怨我的错误吗?

我还缺少什么配置?

4

1 回答 1

8

我想在基于 maven 的项目中使用 checkstyle,所以在我的 pom.xml 中我添加了依赖项(...)

你不需要添加这个依赖,你只需要声明插件(一个插件声明它自己的依赖)。

(...) 但是当我使用命令 mvn clean install 运行我的 maven 构建时,checkstyle 没有做任何事情。

是的,因为您只声明了插件,您没有将check目标绑定到生命周期阶段,因此正常构建不会触发 checkstyle 插件。如果您想checkstyle:check在构建过程中被触发,则需要check在执行中声明目标(默认情况下它将自身绑定到verify阶段)。像这样的东西:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <!-- Lock down plugin version for build reproducibility -->
  <version>2.5</version>
  <configuration>
    <consoleOutput>true</consoleOutput>
    <configLocation>checkstyle.xml</configLocation>
    ...
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

现在,调用任何阶段包括verify将调用 checkstyle。

而且由于我的系统中还没有任何 checkstyle.xml,它不应该抱怨我的错误吗?

它将...在调用时(mvn checkstyle:check如果您按照建议修改设置,则显式地或作为构建的一部分)。

于 2010-04-30T21:15:05.130 回答