Intellij中使用Spotless 格式化代码

news2024/9/21 22:32:47

Spotless简介

在一些大型项目或开源项目,由于开发人员太多,导致各个代码格式不统一。会让整体项目的代码可读性变差。统一代码格式使用maven中的Spotless插件就是不错的选择。

Spotless 是一个代码格式化工具,它有以下功能:

  • 支持的开发语言有java, kotlin, scala, sql, javascript, css, json, yaml, etc。
  • 可以提示哪里不规范,同时也支持自动修正(批量的将所有类格式化)
  • 支持maven|gradle plugin等插件

Spotless 有多种 Java 代码格式化方式,例如:googleJavaFormat、eclipse、prettier 等。基于定制化的考虑,可以采用 eclipse 进行 Java 代码格式化。

示例

第一步:在项目根目录创建license-header文件

/*
 * Copyright 2023 杭州云亮科技有限公司
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

第二步:在项目根目录创建shardingsphere_eclipse_formatter.xml文件(可省略)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-->
<profiles version="13">
    <profile kind="CodeFormatterProfile" name="'ShardingSphere Apache Current'" version="13">
        <setting id="org.eclipse.jdt.core.compiler.source" value="1.8"/>
        <setting id="org.eclipse.jdt.core.compiler.compliance" value="1.8"/>
        <setting id="org.eclipse.jdt.core.compiler.codegen.targetPlatform" value="1.8"/>
        <setting id="org.eclipse.jdt.core.formatter.indent_empty_lines" value="true"/>
        <setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="4"/>
        <setting id="org.eclipse.jdt.core.formatter.lineSplit" value="200"/>
        <setting id="org.eclipse.jdt.core.formatter.comment.line_length" value="200"/>
        <setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>
        <setting id="org.eclipse.jdt.core.formatter.indentation.size" value="1"/>
        <setting id="org.eclipse.jdt.core.formatter.comment.format_javadoc_comments" value="false"/>
        <setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional" value="insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_default" value="do not insert"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_enum_constants" value="16"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_labeled_statement" value="do not insert"/>
        <setting id="org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case" value="do not insert"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_conditional_expression" value="80"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_assignment" value="16"/>
        <setting id="org.eclipse.jdt.core.formatter.blank_lines_after_package" value="1"/>
        <setting id="org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer" value="2"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_resources_in_try" value="160"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration" value="10"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="106"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration" value="106"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration" value="106"/>
        <setting id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call.count_dependent" value="16|5|80"/>
    </profile>
</profiles>

第三步:在pom.xml的标签下添加插件:

<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>2.36.0</version>
    <configuration>
        <java>
            <googleJavaFormat>
                <version>1.16.0</version>
                <style>AOSP</style>
                <reflowLongStrings>true</reflowLongStrings>
            </googleJavaFormat>
            <eclipse>
                <file>${project.basedir}/shardingsphere_eclipse_formatter.xml</file>
            </eclipse>
            <licenseHeader>
                <file>${project.basedir}/license-header</file>
            </licenseHeader>
        </java>
    </configuration>
    <!--将 Spotless apply 绑定到 compile 阶段,这样本地执行 mvn install 时就能自动格式化-->
    <executions>
        <execution>
            <goals>
                <goal>apply</goal>
            </goals>
            <phase>compile</phase>
        </execution>
    </executions>
</plugin>

上面插件中添加 Java 文件 licenseHeader 和 Java 代码格式化。

Spotless 支持格式化指定目录,以及排除指定目录的功能,详情参考 https://github.com/diffplug/spotless/tree/main/plugin-maven#java。如无指定,执行 check 或 apply 时,默认项目全量代码。

第四步:执行代码格式化

在这里插入图片描述
运行结果:
在这里插入图片描述
添加 Java 文件 licenseHeader 和 Java 代码格式化。

IDEA 格式化

开发者如果在写代码过程中,想检查单个文件是否符合规范,执行 mvn spotless:checkmvn spotless:apply 显得有些笨重,因为格式化范围默认是整个项目。

我们可以使用 shardingsphere_eclipse_formatter.xml 替换 IntelliJ IDEA 原有格式化功能,这样在写代码过程中可以随时格式化,极大提升了开发效率。

  • 安装插件 Eclipse Code Formatter
    在这里插入图片描述
  • 选择 shardingsphere_eclipse_formatter.xml 为默认格式化模板
    在这里插入图片描述
    使用 IDEA 代码格式化快捷键,就可以完成 Spotless 代码格式化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/509117.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

300. 最长递增子序列

300. 最长递增子序列 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子…

tp6 对接阿里云短信

1、获取AccessKey ID,AccessKey Secret&#xff0c;第一次会提示需要创建 2、添加签名 3、创建模板 composer版本太老了&#xff0c;可能会导致下载失败&#xff0c;建议升级下版本 官方提供的最新依赖版本&#xff0c;我的会报错&#xff0c;下载不了&#xff0c;提示用2.0.…

Android Studio 下真机调试

文章目录 一、开启真机调试二、断开真机调试 一、开启真机调试 准备USB调试线&#xff0c;一端插在电脑USB接口上&#xff0c;另一端插在手机充电口上。 下面以自己的手机&#xff08;huawei nova 5 &#xff09;为例&#xff1a;点击手机界面上的设置应用。 然后往下找到 【关…

经典神经网络(1)LeNet及其在Fashion-MNIST数据集上的应用

经典神经网络(1)LeNet 1、卷积神经网络LeNet 之前对于Fashion-MNIST服装分类数据集&#xff0c;为了能够应⽤softmax回归和多层感知机&#xff0c;我们⾸先将每个大小为28 28的图像展平为⼀个784维的固定⻓度的⼀维向量&#xff0c;然后⽤全连接层对其进⾏处理&#xff0c;此…

函数式接口的介绍和使用(FunctionInterface)——Consumer,Supplier,Predicate、Function

函数式接口(Functional Interface)就是一个有且仅有一个抽象方法&#xff0c;但是可以有多个非抽象方法的接口。 函数式接口可以被隐式转换为 lambda 表达式。 函数式接口都添加了 FunctionalInterface 注解&#xff0c;这个是jdk1.8才引进的。例如 因为函数式接口里面只是…

【C++】| 01——泛型编程 | 模板

系列文章目录 【C】| 01——泛型编程 | 模板 文章目录 1. 认识泛型编程2. 函数模板1.1 函数模板的语法1.1.1 定义模板1.1.2 应用模板实现函数1.1.3 使用模板函数(实例化)1.1.3.1 隐式使用(实例化)1.1.3.2 显式使用(实例化)1.1.3.3 使用函数模板的注意事项(实例化) 2. 类模板2.…

第1章 Nginx简介

基于 Nginx版本 1.14.2 &#xff0c;Tomcat版本 9.0.0 演示 第1章 Nginx简介 1.1 Nginx发展介绍 Nginx &#xff08;engine x&#xff09; 是一个高性能的Web服务器和反向代理服务器&#xff0c;也可以作为邮件代理服务器。 Nginx 特点是占有内存少&#xff0c;并发处理能力…

南京邮电大学数据库实验二(DBMS的数据库保护)

文章目录 一、实验目的和要求二、实验环境(实验设备)三、实验原理及内容(1) DBMS的数据库保护功能(2) 安全控制中的访问控制机制(3) 事务的提交与回滚(4) 并发控制的锁机制 三、实验内容1.以root账户登录数据库管理系统&#xff0c;创建用户U1和U2&#xff0c;密码自定。2.创建…

Redis 入门教程(简单全面版)

1 安装&#xff1a; 1.1 生产环境安装 注意&#xff1a; 1、如果安装过程有问题可以参考源代码中的 README.md 文件 2、如果服务器只安装一个 redis 通常选择 /usr/local/redis 作为安装目录&#xff0c;如果安装多台则建议带上 服务名称 区分&#xff08;建议带上 服务名称 区…

jvm-狂神课程

一、JVM JVM就是Java虚拟机&#xff0c;Java虚拟机就是JVM 1. JVM位置 1、Java程序&#xff08;跑的环境是在jvm&#xff08;虚拟机&#xff09;跑的&#xff0c;也可以说是在jre上跑的&#xff09;java运行是需要在特定的环境的也就是这个jre这种。 2、jvm&#xff08;也就是…

别不信:这些细节关乎你的物联网设备的命运!

《高并发系统实战派》-- 值得拥有 一、设备接入层网络协议的意义 随着物联网的发展&#xff0c;越来越多的设备需要接入云平台进行远程监控和管理。设备接入层网络协议起到了承担设备接入网络的功能&#xff0c;为物联网平台提供了数据交互的基础。设备接入层网络协议对于物联…

【C++ 入坑指南】(01)学习路线

入门 1. 推荐书籍 《Accelerated C》&#xff0c;《Essential C》二选一精读。《A Tour of C》选读。 《Accelerated C》很适合新手&#xff0c;因为只有短短不到 300 页&#xff0c;在普遍一样的入门书籍里面是一股清流。容易通读完&#xff0c;减少挫败感。就这样的篇幅&am…

【每天学习一点新知识】如何绕过CDN查真实ip

1、什么是CDN 为了防止流量过大网络堵塞&#xff0c;我们就在靠近用户的地方&#xff0c;建一个缓存服务器&#xff0c;把远端的内容复制一份&#xff0c;放在这里&#xff0c;简单来说就是将内容缓存在终端用户附近。 2、怎么绕过cdn找到远端服务器的真实ip呢&#xff1f; &…

FS2462是泛海微自主开发的5A降压型同步整流芯片

FS2462是泛海微自主开发的5A降压型同步整流芯片&#xff0c;是国内首家大电流同步5A芯片&#xff0c;内部集成极低RDS内阻20豪欧金属氧化物半导体场效应晶体管的(MOSFET)。输入工作电压宽至4.75V到21V&#xff0c;输出电压1.0V可调至20V。5A的连续负载电流输出可保证系统各状态…

Landing AI:计算机视觉数据标注AI平台

【产品介绍】 Landing AI是一家由人工智能领域的知名专家、Coursera联合创始人、前百度首席科学家、Google大脑创始负责人吴恩达博士创立的公司&#xff0c;旨在为各行各业提供先进的计算机视觉解决方案。 Landing AI的核心产品是LandingLens&#xff0c;一个基于云端的计算机视…

前端开发代码规范工具

规范化是前端工程化的一个重要部分。现在&#xff0c;有许多工具能够辅助我们实行代码的规范化,比如你一定知道的 ESLint 和 Prettier。 今天&#xff0c;来聊聊这些工具的工作原理和基本使用&#xff0c;了解它们是如何发挥作用的&#xff0c;以及如何更好地利用这些工具去规…

AI成功破译古老未知语言,人工智能技术开辟历史研究新时代

近年来&#xff0c;人工智能在各个领域取得了突飞猛进的发展&#xff0c;成为了当今社会讨论的热点。尽管有关其使用的争议不断&#xff0c;但AI技术在某些方面的作用已经不容忽视。 最近&#xff0c;以色列特拉维夫大学和阿里尔大学的研究者们联手研发了一款能够破译古老未知…

Nature:刘清华团队揭示调控睡眠时间的关键分子通路

导读 你能做到一周不睡觉吗&#xff1f;良好的睡眠对我们保证生活质量十分重要。不过&#xff0c;有些人每天只需睡4-6个小时&#xff0c;有些人则需要8个小时&#xff08;可能还不够&#xff09;&#xff0c;这是什么原因导致的&#xff1f; 其实&#xff0c;这也是很多科学…

阿里云服务器镜像是什么意思?

阿里云服务器镜像是云服务器的装机盘&#xff0c;镜像是为云服务器安装操作系统的。云服务器镜像系统怎么选择&#xff1f;云服务器操作系统镜像分为Linux和Windows两大类&#xff0c;Linux可以选择Alibaba Cloud Linux&#xff0c;Windows可以选择Windows Server 2022数据中心…

怎样恢复删除的视频?这5个方法才是正确答案!

案例&#xff1a;怎样恢复删除的视频&#xff1f; 【我是个视频爱好者&#xff0c;平常会在电脑中存很多视频&#xff0c;但也经常会将很多视频误删&#xff0c;怎样恢复删除的视频呢&#xff1f;希望大家给我一些建议&#xff01;】 在摄影摄像技术较发达的今天&#xff0c;…