Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle LineLength configuration not working

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?

like image 228
luizfzs Avatar asked Dec 18 '22 15:12

luizfzs


2 Answers

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

like image 79
Radoslav Ivanov Avatar answered Jan 04 '23 21:01

Radoslav Ivanov


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>
like image 44
SDJ Avatar answered Jan 04 '23 19:01

SDJ