Checkstyle not working

一世执手 提交于 2019-12-12 18:15:40

问题


I am new to maven and chekstyle, so need to ask some question... I want to use checkstyle in my maven based project, so in my pom.xml I have add the dependency

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

and also I have added the entry in plugin tag:

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

But when I run my maven build with command mvn clean install, checkstyle doesn't do anything. And as I dont have any checkstyle.xml in my system yet, shouldn't it complains me about the error?

What else configuration am I missing?


回答1:


I want to use checkstyle in my maven based project, so in my pom.xml I've add the dependency (...)

You don't need to add this dependency, you just need to declare the plugin (a plugin declares its own dependencies).

(...) But when I run my maven build with command mvn clean install, checkstyle doesn't do anything.

Yes because you only declared the plugin, you did not bind the check goal to a lifecycle phase, so a normal build doesn't trigger the checkstyle plugin. If you want checkstyle:check to be triggered as part of your build, you need to declare the check goal inside an execution (it binds itself by default to the verify phase). Something like this:

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

Now, calling any phase including verify will invoke checkstyle.

And as I don't have any checkstyle.xml in my system yet, shouldn't it complains me about the error?

It will... when called (either explicitly by mvn checkstyle:check or as part of the build if you modify your setup as suggested).



来源:https://stackoverflow.com/questions/2747395/checkstyle-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!