15

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?

Naman
  • 27,789
  • 26
  • 218
  • 353
Dmitriy Dumanskiy
  • 11,657
  • 9
  • 37
  • 57

3 Answers3

17

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.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Well ideally the checkstyle plugin should be expected to not throw an exception for `module-info.java`, isn't it? – Naman Sep 29 '17 at 19:34
  • @nullpointer Yes. But I don't know if they support it already. I found [this page](http://checkstyle.sourceforge.net/config_filefilters.html) from their documentation which suggests it cannot read the file. – M A Sep 29 '17 at 19:36
  • Ya seems like some work is in progress in that direction. I'd tried [detailing it as well](https://stackoverflow.com/a/46496192/1746118). – Naman Sep 29 '17 at 20:06
8

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.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
4

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.
Naman
  • 27,789
  • 26
  • 218
  • 353
  • I don't think it has to do with the Maven plugin. The plugin internally delegates to Checkstyle. Supporting `module-info.java` should likely be done on Checkstyle's side and not Maven (this explains your third point about the missing reference to the plugin in the upgrade wiki). – M A Sep 29 '17 at 20:17
  • @manouti the point I tried bringing up was that checkstyle support is probably being built in with the plugin update. – Naman Sep 29 '17 at 20:20