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

3

17

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?

Katykatya answered 29/9, 2017 at 19:15 Comment(0)
C
19

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.

Countershading answered 29/9, 2017 at 19:22 Comment(3)
Well ideally the checkstyle plugin should be expected to not throw an exception for module-info.java, isn't it?Govern
@nullpointer Yes. But I don't know if they support it already. I found this page from their documentation which suggests it cannot read the file.Countershading
Ya seems like some work is in progress in that direction. I'd tried detailing it as well.Govern
T
9

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.

Trisect answered 1/4, 2018 at 19:3 Comment(0)
G
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.
Govern answered 29/9, 2017 at 20:4 Comment(2)
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).Countershading
@manouti the point I tried bringing up was that checkstyle support is probably being built in with the plugin update.Govern

© 2022 - 2024 — McMap. All rights reserved.