开发人员太多代码格式不统一?来试试 Spotless maven插件解决代码格式化问题

这里是weihubeats,觉得文章不错可以关注公众号小奏技术,文章首发。拒绝营销号,拒绝标题党

背景

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

Spotless

Spotless 是什么

Spotless 是支持多种语言的代码格式化工具,支持 Maven 和 Gradle 以 Plugin 的形式构建。目前github已经有4000+开源项目在使用Spotless进行格式化代码

我们下面使用maven进行演示

Spotless 常用命令

检查代码格式

mvn spotless:check

自动格式化代码

mvn spotless:apply

项目实战

目的

自动为代码添加licenseHeader和格式化代码

添加配置文件

  1. 设置自动添加请求头文件

这里我们设置自动添加Apache-2.0 license请求头

文件地址:

  • github: https://github.com/weihubeats/mq-idempotent/blob/develop/dev-support/license-header

注意, license-header最后要留有一行空格。不然license-headerpackage之间将没有空隙。

  1. 设置代码格式配置
  • spotless_mq-idempotent_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.blank_lines_before_first_class_body_declaration" 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>

添加maven插件配置

我们在项目pom中添加插件及配置

<plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>${spotless-maven-plugin.version}</version>
                <configuration>
                    <java>
                        <eclipse>
                            <file>${maven.multiModuleProjectDirectory}/dev-support/spotless_mq-idempotent_formatter.xml</file>
                        </eclipse>
                        <licenseHeader>
                            <file>${maven.multiModuleProjectDirectory}/dev-support/license-header</file>
                        </licenseHeader>
                    </java>
                    <pom>
                        <sortPom>
                            <encoding>UTF-8</encoding>
                            <nrOfIndentSpace>4</nrOfIndentSpace>
                            <keepBlankLines>true</keepBlankLines>
                            <indentBlankLines>false</indentBlankLines>
                            <indentSchemaLocation>true</indentSchemaLocation>
                            <spaceBeforeCloseEmptyElement>true</spaceBeforeCloseEmptyElement>
                            <sortModules>false</sortModules>
                            <sortExecutions>false</sortExecutions>
                            <predefinedSortOrder>custom_1</predefinedSortOrder>
                            <expandEmptyElements>false</expandEmptyElements>
                            <sortProperties>false</sortProperties>
                        </sortPom>
                    </pom>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>apply</goal>
                        </goals>
                        <phase>compile</phase>
                    </execution>
                </executions>
            </plugin>

格式化代码

可以看到格式化成功了

然后我们看看代码
在这里插入图片描述

可以看到自动帮我们添加了文件头和格式化了代码

参考

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
org.jacoco是一个用于Java代码覆盖率测试和报告生成的开源工具。它可以帮助开发人员分析他们的代码测试覆盖率情况,并生成详细的报告。在你提供的引用中,主要是关于二次开发的一些修改和使用方法。 根据第一个引用,jacoco二次开发主要加入了增量代码匹配的功能。具体的设计方案可以参考jacoco的增量代码实践。这个二次开发主要是新增了一些类和修改了一些核心类。新增的类位于org.jacoco.core.internal.diff包下,而修改的类主要包括org.jacoco.core.analysis.Analyzer、org.jacoco.core.analysis.CoverageBuilder和org.jacoco.core.internal.flow.ClassProbesAdapter。你可以通过查看commit记录来了解具体的修改内容。 关于使用方法,首先需要下载源码,并运行mvn spotless:apply命令。这个命令主要是用于进行文件头文件校验,如果新增加了类或者修改了类,需要重新校验。然后,运行mvn clean package -Dmaven.test.skip=true -Dmaven.javadoc.skip=true命令进行构建。构建完成后,你可以在target目录下找到org.jacoco.cli-0.8.7-SNAPSHOT-nodeps.jar,这是构建出的需要的包。 关于生成单元覆盖率报告,你可以运行maven test命令。然后,在target目录下会生成jacoco-ut文件夹,其中包含了index.html文件,你可以用浏览器打开这个文件来查看单元覆盖率报告。 至于你提到的最开始未找到单元测试报告的问题,可能是因为在pom.xml配置中没有加入report部分的依赖,并指定report的位置。请确保你的pom.xml文件中有正确的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值