本文最后更新于:2022年11月9日 晚上
背景 在一些大型项目或开源项目,由于开发人员太多,导致各个代码格式不统一。会让整体项目的代码可读性变差, spotless-maven-plugin就是不错的选择
Spotless 是什么 Spotless 是支持多种语言的代码格式化工具,支持 Maven 和 Gradle 以 Plugin 的形式构建。目前github已经有4000+开源项目在使用Spotless
进行格式化代码
我们下面使用maven
进行演示
Spotless 常用命令 检查代码格式 mvn spotless:check
自动格式化代码 mvn spotless:apply
项目实战 目的 自动为代码添加licenseHeader
和格式化代码
添加配置文件
设置自动添加请求头文件
这里我们设置自动添加Apache-2.0 license
请求头
注意, license-header
最后要留有一行空格。不然license-header
和package
之间将没有空隙。
设置代码格式配置
项目根目录添加 spotless_formatter.xml 和 license-header 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <profiles version ="13" > <profile kind ="CodeFormatterProfile" name ="'Hippo4j 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 ="false" /> <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.blank_lines_before_first_class_body_declaration" value ="1" /> <setting id ="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call.count_dependent" value ="16|5|80" /> </profile > </profiles >
添加maven插件配置 我们在项目pom中添加插件及配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <plugin > <groupId > com.diffplug.spotless</groupId > <artifactId > spotless-maven-plugin</artifactId > <version > 2.22.1</version > <configuration > <applySkip > false</applySkip > <java > <eclipse > <file > spotless_formatter.xml</file > </eclipse > <licenseHeader > <file > license-header</file > </licenseHeader > </java > </configuration > <executions > <execution > <goals > <goal > apply</goal > </goals > <phase > compile</phase > </execution > </executions > </plugin >
格式化代码
可以看到格式化成功了
然后我们看看代码
可以看到自动帮我们添加了文件头和格式化了代码
References