Checkstyle LineLength configuration not working - java

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?

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

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>

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.

Related

How do increase the number of nested if else statements allowed by maven

I have written my code to search if a node can be reached in a graph using java. I don't to change my code. I am using maven. When I try to compile it's giving me the error below.
[INFO] There is 1 error reported by Checkstyle 8.29 with com/github/ngeor/checkstyle.xml ruleset.
[ERROR] src/main/java/dsa/graphs/Graph.java:[160,25] (coding) NestedIfDepth: Nested if-else depth is 2 (max allowed is 1).
How do I get around this. I don't want to disable checkstyle at the same time I don't want to change my code. How can I increase that number to be more than 1.
I am presuming that you have the checkstyle under com.github.ngeor package so just go to the file and change/add
<module name="NestedIfDepth">
<property name="max" value="{AS_MUCH_AS_YOU_WANT}"/>
</module>
Or if you are referencing a checkstyle that is not under your control you can also override it.
Add the checkstyle plugin to your pom:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
<suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression>
</configuration>
</plugin>
</plugins>
And of course you need to create checkstyle-surpressions.xml file:
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.0//EN"
"https://checkstyle.org/dtds/suppressions_1_0.dtd">
<suppressions>
<suppress checks="NestedIfDepth"
files="Graph.java"
lines="{what_Lines_you_need}"/>
</suppressions>
This will

How to define a checkstyle SuppressionFilter in both eclipse and sbt

I would like to use the same checkstyle config in sbt (https://github.com/etsy/sbt-checkstyle-plugin) and in eclipse (http://eclipse-cs.sf.net/)
This works apart from the SuppressionFilter
I can define the file either so that it works in eclipse:
<module name="SuppressionFilter">
<property name="file" value="${samedir}/checkstyleExcludeFiles.xml"/>
</module>
Then sbt complains
org.xml.sax.SAXException: Property ${samedir} has not been set
Or so that it works in sbt:
<module name="SuppressionFilter">
<property name="file" value="checkstyleExcludeFiles.xml"/>
</module>
Then eclipse complains with
cannot initialize module SuppressionFilter - Cannot set property 'file' to 'checkstyleExcludeFiles.xml' in module SuppressionFilter
I found this solution for ant:
How to define suppressions-definitions for checkstyle, that work with both ant and eclipse
But I am new to sbt, and have no idea how I define a variable in build.sbt in such a way, that it will be picked up by the checkstyle plugin.
What could I do? I would like to configure as little as possible in eclipse, as that would have to be done on each developer machine.
thanks
Looking into sbt-checkstyle-plugin code, I don't see support for setting properties. As a workaround I would suggest the following:
<module name="SuppressionFilter">
<property name="file" value="${samedir}/checkstyleExcludeFiles.xml"
default="checkstyleExcludeFiles.xml"/>
</module>

Checkstyle – ignore Javadoc while checking duplication

I am using checkstyle to check violation in my code. One of the module in my configuration is about duplicate code. Since I am using StrictDuplicateCode I get violation on duplication for javaDoc as well.
Can anyone guide me to achieve my goal?
In the meantime, I tried following BUT it doesn’t work:
To suppress duplication with java doc I created a separate xml file (JavaDocSup.xml) with following content
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress checks="JavadocStyleCheck"
files="SomeClass.java"
/>
</suppressions>
Then I added following code in my main configuration file. Following is the code for my config file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="severity" value="warning"/>
<module name="TreeWalker">
<module name="MethodLength">
<property name="max" value="50"/>
</module>
<module name="ParameterNumber">
<property name="max" value="4"/>
</module>
<module name="CyclomaticComplexity"/>
</module>
<module name="StrictDuplicateCode">
<property name="fileExtensions" value="java"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${samedir}/JavaDocSup.xml"/>
</module>
</module>
When I run the checkstyle on my code, it still detects the duplicate lines in the Java doc.
Is the suppress checks="JavadocStyleCheck” is correct?
Any help will be highly appreciated. Thanks.
Checkstyle's StrictDuplicateCode is found to be inefficient, hence it's thrown away (since Checkstyle 6.2). Try using other tools like PMD's CPD, etc.
If you want to stick to current Checkstyle version, you can try adding <property name="min" value="20"/> to throw the violation only if at least 20 lines are matching.
(Some clone detection tools are mentioned under Tools section in Wikipedia)

Automatic verification of JavaDoc with Maven

During refactoring it happens frequently that JavaDoc gets out-of-date. It describes method arguments which are not present any more or some new ones are missing, to give examples.
It would be fine, if there is a Maven-plugin which automatically checks the existing JavaDoc and stops the build if there are some kind of "JavaDoc-violations".
I've seen the Maven-JavaDoc-Plugin and maven-doccheck, but both seem only to be able fix existing JavaDoc automatically in case of violations instead of bailing some error or warning.
Does anyone know how if there is some Maven-plugin like this and how to archive this?
As far as I know this is currently not possible with the maven-javadoc-plugin. There is the javadoc:fix mojo for the JavaDoc plugin, but this automatically fixes problems.
I recently created a JIRA entry for this problem: MJAVADOC-374 (which is acutally a duplicate of MJAVADOC-314).
Update:
You can use Checkstyle to verify correct JavaDoc. The configuration options are described here. Use the maven-checkstyle-plugin and the check-Mojo to integrate this into your maven build.
An example maven configuration could look like this:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<configuration>
<logViolationsToConsole>true</logViolationsToConsole>
<checkstyleRules>
<module name="JavadocMethod">
<property name="scope" value="public"/>
<property name="allowUndeclaredRTE" value="true"/>
<property name="allowMissingParamTags" value="false"/>
</module>
</checkstyleRules>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

eclipse checkstyle error cannot initialize module TreeWalker - TreeWalker is not allowed as a parent of FileLength

I get the following error when I try to load a user defined check style template.
cannot initialize module TreeWalker - TreeWalker is not allowed as a parent of FileLength
I think it might be due to the incompatibilty with checkstyle version in my eclipse.
How will I be able to find the appropriate checkstyle version with checkstyle xml file
This is an error in the template, independent of the Checkstyle version. FileLength goes directly under Checker, not under TreeWalker.
Example:
<module name="Checker">
<property name="severity" value="warning"/>
<module name="TreeWalker">
<property name="tabWidth" value="4"/>
<module name="JavadocType"/>
<!-- many others -->
</module>
<module name="NewlineAtEndOfFile"/>
<module name="FileLength"/>
<module name="JavadocPackage"/>
<!-- others -->
</module>

Categories