0

I have spring boot projects with lots of files in .json apart from .java files.

For java formatting, we are using pre-commit hooks with google-java-format . However, for formatting .json files I am a bit struggling.

I have used a maven spotless plugin

<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>${spotless.version}</version>
    <configuration>
        <formats>
            <format>
                <includes>
                    <include>*.json</include>
                </includes>
                <prettier>
                    <!-- Specify at most one of the following 3 configs: either 'prettierVersion' (2.0.5 is default) , 'devDependencies' or 'devDependencyProperties'  -->
                    <prettierVersion>2.0.5</prettierVersion>
                    <!-- Specify config file and/or inline config, the inline always trumps file -->
                    <config>
                        <useTabs>true</useTabs>
                    </config>
                </prettier>
            </format>
        </formats>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The problem with this approach is, that it requires npm in the machine to be installed else mvn clean install will fail. Many machines on the jenkins server don't have npm pre-installed so it fails during the build.

Is there an easy way to solve this? PS: This project uses GIT as version control here.

Sharad
  • 838
  • 1
  • 7
  • 15
  • https://github.com/diffplug/spotless/issues/1258#event-7003326295 Currently it is not supported . – Sharad Jul 28 '22 at 18:10

1 Answers1

1

Introduction

Let's consider the limitation: the Spotless formatter steps must be Java-based, i.e. avoid using external programs (npm, etc.).

Let's consider Spotless Maven plugin 2.23.0 as the current version.

Let's call Java-based JSON document formatting and validation the feature.

Analysis

According to the feature matrix table, the following formatter steps are available for Spotless Gradle plugin, but not available for Spotless Maven plugin:

These feature steps are Java-based.

Summary

Currently, there are no such Spotless formatter steps available for Spotless Maven plugin — the feature is absent.

Possible solutions

Implement feature

  1. Implement the feature.
  2. Create a pull request to propose and collaborate on changes to the GitHub repository.

Request feature

Request the feature by creating an issue in the GitHub repository.

It looks like there was a quick attempt to request it. Please, see the comment:

Find and use additional formatter Maven plugin

Find and use additional formatter Maven plugin that has the feature.

For example, it seems that the following formatter Maven plugin supports JSON document formatting and validation:

<dependency>
    <groupId>net.revelc.code.formatter</groupId>
    <artifactId>formatter-maven-plugin</artifactId>
    <version>2.19.0</version>
</dependency>

Some related references: