Dmitriy Dumanskiy
Dmitriy Dumanskiy

Reputation: 12777

How to exclude module-info.java from checkstyle plugin checks?

After adding module-info.java files to my project my checkstyle plugin start failing with:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (default-cli) on project email: Failed during checkstyle configuration: NoViableAltException occurred during the analysis of file /home/xxx/IdeaProjects/blynk-server/server/notifications/email/src/main/java/module-info.java. unexpected token: module -> [Help 1]

I tried

<module name="BeforeExecutionExclusionFileFilter">
    <property name="fileNamePattern" value="module\-info\.java$"/>
</module>

However, it failed with:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (default-cli) on project blynk: Failed during checkstyle configuration: cannot initialize module BeforeExecutionExclusionFileFilter - Unable to instantiate 'BeforeExecutionExclusionFileFilter' class, it is also not possible to instantiate it as com.puppycrawl.tools.checkstyle.checks.annotation.BeforeExecutionExclusionFileFilter

What is the correct way for skipping module-info.java files during checkstyle for maven-checkstyle-plugin?

Upvotes: 17

Views: 4840

Answers (3)

Alex Shesterov
Alex Shesterov

Reputation: 27525

BeforeExecutionExclusionFileFilter was added in Checkstyle 7.2.

But the maven-checkstyle-plugin version 3.0.0 (which is the latest version as of 2018-04-01) uses Checkstyle 6.18 by default.

"Checkstyle" and "Checkstyle Maven Plugin" are different things and have different release cycles.

You may want to upgrade the Checkstyle version as follows:

<plugin>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.0.0</version> <!-- Checkstyle Plugin version -->

  <!-- ... Configuration, Executions ... -->

  <dependencies>
    <dependency>
      <groupId>com.puppycrawl.tools</groupId>
      <artifactId>checkstyle</artifactId>
      <version>8.8</version> <!-- Checkstyle version -->
    </dependency>
  </dependencies>
</plugin>

After that, BeforeExecutionExclusionFileFilter as well as other newer Checkstyle features (e.g. new checks) will be recognized.

Upvotes: 9

Naman
Naman

Reputation: 31858

Though this doesn't possibly qualify as an answer. Yet being too long to fit in comment, just to keep a note of the track that the maven-checkstyle-plugin is in:-

  • The last release of the was version 2.17 on 15-Oct-2015 which was almost 2 years back.
  • The current trunk of maven-plugins points to an ongoing work within the plugin in its 3.0.0-SNAPSHOT version which might mean we can soon expect a org.apache.maven.plugins:maven-checkstyle-plugin:3.0.0 sometime in near future and which would understand the module-info.java as a class.
  • This doesn't align with the Java+9+-+Jigsaw doc that specifies the list of modules and plugins that are being upgraded to support JDK-9.

Upvotes: 4

M A
M A

Reputation: 72844

Not sure why the Checkstyle filter is not working (this reported bug seems very similar to yours and it was fixed in version 7.3.0, so maybe you need to update Checkstyle).

Anyway the Maven excludes element is also supposed to do this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <excludes>**/module-info.java</excludes>
    </configuration>
</plugin>

More in the plugin goal documentation.

Upvotes: 19

Related Questions