Checkstyle LineLength configuration not working
Asked Answered
N

3

7

When trying to add maven-checkstyle-plugin to my Java project, I'm facing some weird issues. The checkstyle version is 3.1.0, that uses checkstyle version 8.19. Below is the checkstyle.xml the project is using:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="TreeWalker">
    <module name="LineLength">
        <property name="max" value="120"/>
    </module>
</module>

and my the configuration on the pom.xml

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

Now, when I run mvn checkstyle:check the following message is displayed:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.0:check (default-cli) on project myproject: Failed during checkstyle configuration: LineLength is not allowed as a child in Checker -> [Help 1]

If I update the checkstyle to make it look like this

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="LineLength">
        <property name="max" value="120"/>
</module>

I get the following message

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.0:check (default-cli) on project myproject: Failed during checkstyle configuration: Property 'max' does not exist, please check the documentation -> [Help 1]

I cannot reason what's happening since, to me, the error messages are misleading. What am I missing on this configuration?

Nostology answered 4/9, 2019 at 16:48 Comment(0)
M
8

The structure that matches the 1.3 DTD for your example is as follows (namely, you seem to be missing the Checker module).

<module name="Checker">
    <module name="TreeWalker">
        <module name="LineLength">
            <property name="max" value="120"/>
        </module>
    </module>
</module>
Mauchi answered 4/9, 2019 at 17:51 Comment(1)
I followed your suggestion but I had to remove the severity config, since it wasn't outputting any messages. ty!Nostology
S
8

Actually, LineLength should not be any more under TreeWalker but directly under Checker in the newer versions. See here: https://github.com/checkstyle/eclipse-cs/issues/190

Sym answered 16/3, 2020 at 4:29 Comment(0)
R
0

I was receiving this error in Visual Studio Code and I needed to make sure that my checkstyle.xml version matched the checkstyle version that Visual Studio Code was using.

I went to Extensions->CheckStyle->Settings->Version and set it to match the appropriate version. I just set the version to match the version defined in my build.gradle.kts file.

Ricoriki answered 23/11, 2021 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.