【问题标题】:JSON files formatting in Spring boot based projects基于 Spring Boot 的项目中的 JSON 文件格式
【发布时间】:2022-07-11 08:19:51
【问题描述】:

除了 .java 文件之外,我有很多包含 .json 文件的 Spring Boot 项目。

对于 java 格式,我们使用带有 google-java-format 的预提交钩子。但是,对于格式化 .json 文件,我有点吃力。

我使用了一个 maven 一尘不染的插件

<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>

这种方法的问题是,它需要在机器中安装npm,否则mvn clean install 将失败。 jenkins 服务器上的许多机器没有预先安装 npm,因此在构建过程中会失败。

有没有简单的方法来解决这个问题? PS:本项目这里使用GIT作为版本控制。

【问题讨论】:

    标签: java maven spotless


    【解决方案1】:

    简介

    让我们考虑一下限制:Spotless 格式化程序步骤必须基于 Java,即避免使用外部程序(npm 等)。

    让我们将 Spotless Maven 插件 2.23.0 视为当前版本。

    我们将基于 Java 的 JSON 文档格式化和验证称为该功能。

    分析

    根据feature matrix table,以下格式化程序步骤适用于 Spotless Gradle 插件,但不适用于 Spotless Maven 插件:

    这些功能步骤基于 Java。

    总结

    目前,没有可用于 Spotless Maven 插件的此类 Spotless 格式化程序步骤 - 该功能不存在。

    可能的解决方案

    实现功能

    1. 实现该功能。
    2. 创建一个拉取请求以提议和协作对 GitHub repository 的更改。

    请求功能

    通过在GitHub repository 中创建问题来请求该功能。

    似乎有人试图请求它。请看评论:

    查找和使用其他格式化程序 Maven 插件

    查找并使用具有该功能的其他格式化程序 Maven 插件。

    例如,以下格式化程序 Maven 插件似乎支持 JSON 文档格式化和验证:

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

    一些相关参考资料:

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2014-12-27
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      相关资源
      最近更新 更多