CDLUC3 / mrt-doc

Documentation and Information regarding the Merritt repository
8 stars 4 forks source link

Consider code linter for Java code #1679

Open terrywbrady opened 9 months ago

terrywbrady commented 9 months ago

Fyi. This is how DSpace recommended a cross-IDE linter solution.

https://wiki.lyrasis.org/display/DSPACE/Code+Style+Guide

This uses maven-checkstyle-plugin

https://maven.apache.org/plugins/maven-checkstyle-plugin/

I did some experimentation with this.

I added the following to a pom.xml

                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-checkstyle-plugin</artifactId>
                                <version>${maven.checkstyle.plugin.version}</version>
                        </plugin>

I copied the DSpace checkstyle.xml file to my project.

https://github.com/DSpace/DSpace/blob/main/checkstyle.xml

At this point, I was able to run the check in maven

bash-4.2$ mvn checkstyle:check|grep -c ERROR
27

I then installed the following plugin

https://marketplace.visualstudio.com/items?itemName=shengchen.vscode-checkstyle

I made the following settings changes

        "java.configuration.updateBuildConfiguration": "interactive",
        "java.checkstyle.configuration": "${workspaceFolder}/log4j_stuff/checkstyle.xml",
        "java.checkstyle.version": "8.3",
        "java.configuration.runtimes": [
                {
                        "name": "JavaSE-11",
                        "path": "/usr/lib/jvm/java-11-openjdk",
                        "default":  true
                }
        ]

Due to some warnings, I had to remove this block from checkstyle.xml

    <!-- Maximum line length is 120 characters -->
    <module name="LineLength">
        <property name="fileExtensions" value="java"/>
        <property name="max" value="120"/>
        <!-- Only exceptions for packages, imports, URLs, and JavaDoc {@link} tags -->
        <property name="ignorePattern" value="^package.*|^import.*|http://|https://|@link"/>
    </module>

Here is the plugin config

image

If I run checkstyle from the command palette, the results appear in the Problems tab

image

Unfortunately, the "Format Document" option in VSCode does not produce changes that match the checkstyle settings.

image

I am curious to see if there is a separate plugin that will assist with re-formatting.

terrywbrady commented 8 months ago

The Red Hat Java Plugin has some formatting options

image

See https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings