How fix all check style issues with Intellij Idea? - maven

I have an xml configuration file for maven checkstyle plugin. I have installed Intellij Idea plugin and see all checkstyle violation. Is there a way to fix most of them automatically? For example auto formatting setting? Is there a way to setup Intellij Idea to autoformat from checktyle config file?

There is an option now.
checkstyle-IDEA since 4.24.0 features import of checkstyle config.
Please install CheckStyle-IDEA plugin
(http://plugins.jetbrains.com/plugin/1065?pr=idea), it can be found
via plug-in repository (Settings|Plugins|Browse repositories). Go to
Settings|Editor|Code Style, choose a code style you want to import
CheckStyle configuration to. Click on the gear, then 'import scheme', choose
"CheckStyle Configuration" and select a corresponding CheckStyle
configuration file. Click OK. At the end you will see a message
"CheckStyle configuration settings were imported to ... scheme".
If you encounter any problems, you can create a new issue for the
CheckStyle plug-in here:
https://github.com/jshiell/checkstyle-idea/issues. This issue is
actually a duplicate of
https://github.com/jshiell/checkstyle-idea/issues/126.
CheckStyle plug-in version where the feature is available is 4.24.0
Copied from https://youtrack.jetbrains.com/issue/IDEA-61520#comment=27-1292600. Copied the answer from here
Edit: Quote has been changed for clarity and to meet the current UI design.

I have the same problem. As far as I know, the rules used for reformatting cannot be imported from Checkstyle config. You can configure your Checkstyle rules in the Idea's Code Style config (File -> Settings -> Code Style) and then reformat the code (Code -> Reformat Code [Ctrl+Alt+L]) according to the rules (you can apply it to the whole code base at once).

import your code style config first
reformat your code.

Related

Can I have gradle support in IDEA without importing it?

I have quite big IDEA project which we incrementally migrate to gradle. I don't want to "import" newly migrated parts right now, but I do want to have IDEA support (syntax highlighting, code completion, etc) for writing gradle scripts. From brief searching it is still not obvious how to get it. Is it possible it all?
As long you have the Groovy plugin installed and enabled, and have *.gradle listed as a Registered Pattern for "Groovy" in Settings > Editor > File Types you should get groovy features when editing a build.gradle file.

Autofix maven checkstyle errors

I'm using IntelliJ IDEA along with google checks rules to validate code quality.
Also, I have CheckStyle IDEA plugin installed in my IDE.
This plugin identifies all the checkstyle issues.
But I'm looking for an autofix solution for these errors (at least for indentation issues and import order). Is there any plugin available for that, or can I configure Checkstyle IDEA to do the same?

Minor Annoyance: edu.emory.mathcs.backport.java.util

I know this is trivial, but I bet I am not the only developer being annoyed by this issue:
I have a maven project building a Spring Hibernate frameowrk for a webapp.
I am building using Eclipse.
I use Eclipse shortcuts to import dependencies and often do so almost subconsciously.
Somewhere in my project dependencies I have a jar that includes the following package.
edu.emory.mathcs.backport.java.util
I often accidentally import this package instead of java.util and get compile or runtime errors.
QUESTION: How can I find out which dependency includes this package so I can (hopefully) exclude it using the Maven enforcer plugin.
This does not directly answer your question but may help you prevent the accidental import in the first place:
In Eclipse you can add a type filter in Preferences → Java → Appearance → Type Filters.
From the description there:
All types whose fully qualified name matches the selected filter strings will not be shown in the 'Open Type' dialog. They will also be ignored in content assist or quick fix proposals and when organizing imports. For example 'java.awt.*' will hide all types from the awt packages.
You can run mvn dependency:tree from the command line. It will show a tree of your dependencies and their transitive dependencies.
Within Eclipse you can open your pom.xml using the Maven POM Editor (default editor for POM files) and click on Dependency Hierarchy.
Within this view you can use a filter or select the dependency on the right side and the dependency tree for this dependency will be shown on the left side.
I recently encountered this issue with a Maven project in IntelliJ IDEA.
You can view transitive dependencies using the Maven command mvn dependency:tree to figure out where this package is creeping in.
However, it's worthwhile to mention that this problem can occur for Gradle or Maven projects. If you are using IntelliJ, then another workaround that will work for all of your projects - regardless of build tool - is to exclude the package altogether from the IDE's editor settings.
The steps to exclude the edu.emory.mathcs.backport.java.util package when using auto-import for all of your projects opened in IntelliJ are as follows (this is valid for IntelliJ IDEA 2022.2):
Ctrl + Alt + S (or click on File, then Settings)
Editor > General > Auto Import
Under the exclusion section, add the following exclusion with an IDE scope: edu.emory.mathcs.backport.java.util.*
Reference Screenshot

how to make checkstyle ignore xml files in my application

i've configured my checkstyle plugin with customized checkstyle.xml and it works fine with the java classes but having a lot of warnings with the xml files can anyone suggest how to disable the check style from any xml file in my application.
thanks in advance.
I can think of two ways to do that:
You could suppress findings from non-Java files. This works very well inside and outside of your IDE.
If you are using Eclipse, you can also configure Eclipse not to feed the non-Java files to Checkstyle. In order to do that, right-click your project and click Checkstyle. Uncheck use simple configuration. Specify a file set using \.java$ as regexp. You will see the list of files in the bottom half of the dialog window change to only Java files.
In earlier versions of Checkstyle, I think I remember that there also was a file name filter in the configuration XML, but I can't seem to find it in the docs now, so maybe that feature is no more.
There is a basedir property at the start of the Checker module in the check style config file. Uncomment it if it is commented.
Set it's value to the folder you want to apply your checkstyle rules to.
E.g. src folder of any eclipse project only contains java files.
<!-- If you set the basedir property below, then all reported file names
will be relative to the specified directory. See http://checkstyle.sourceforge.net/5.x/config.html#Checker -->
<property name="basedir" value="/MyEclipeProject/src"/>

converting an eclipse formatter file to a checkstyle file

I have an eclipse formatting file (which contains some configurations) and i want to make the same thing (these configurations) for a checkstyle file to use after with maven build.
Unfortunately, there is no way to convert a formatter file to a checkstyle ruleset at the moment. You have to do it the other way around as explained here.
Otherwise you have to adjust every setting in your formatter manually for checkstyle.
The Eclipse-Checkstyle plugin provides an export feature to export your preferences.

Resources