这两天需要把某个java项目的a分支代码合并到b分支上去,目前主流使用的是a分支进线开发,b分支由于代码久远,结果合并比对的时候,由于代码格式的问题,导致合并的时候看起来乱七八糟的。所以再编写一篇关于直接向git提交的时候,自动对java代码进行格式化的案例。因为只要保证两个分支的代码格式化后的格式一致,那么在解决冲突的时候就不会出现格式乱对齐的问题,这样解决冲突的话,看起来就比较常规化,大大的缩短了解决冲突的时间。下面来演示一下:
使用到的工具有:
git-code-format-maven-plugin
这是一个专门为java项目诞生的git钩子插件,可以很方便的让java代码在提交的时候自动执行格式化的需求。
一、安装jdk
这里git-code-format-maven-plugin版目前支持的jdk版本最低是java11,所以我们需要下载一个jdk11,安装部署在开发电脑上,并且配置本开发电脑的java_home
并且还需要配置path
备注:
1、本机开发电脑这里安装的java_home环境必须最低jdk版本为jdk11.
2、这里配置环境变量的目的主要是当执行git push的时候,他会自动调用maven和java环境,所以必须要保证把这里jdk的环境安装在开发电脑上。
二、安装maven
接下来就是安装maven,和上面的jdk配置一样,直接下载最新版本的maven安装包即可。然后配置到环境变量中去。
然后再配置path
三、准备maven项目
这里我们准备一个maven的项目,把他提交到git上去
切记这里准备好项目之后,把他上传到git上,如果本地已经把这个项目clone下来了的话,需要把本地的项目删除掉,重新从git上clone这个项目。
重新克隆下来代码之后,在pom.xml里面添加如下的配置:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <git-code-format-maven-plugin.version>5.3</git-code-format-maven-plugin.version> </properties> <build> <plugins> <plugin> <groupId>com.cosium.code</groupId> <artifactId>git-code-format-maven-plugin</artifactId> <version>${git-code-format-maven-plugin.version}</version> <executions> <!-- On commit, format the modified files --> <execution> <id>install-formatter-hook</id> <goals> <goal>install-hooks</goal> </goals> </execution> <!-- On Maven verify phase, fail if any file (including unmodified) is badly formatted --> <execution> <id>validate-code-format</id> <goals> <goal>validate-code-format</goal> </goals> </execution> </executions> <dependencies> <!-- Enable https://github.com/google/google-java-format --> <dependency> <groupId>com.cosium.code</groupId> <artifactId>google-java-format</artifactId> <version>${git-code-format-maven-plugin.version}</version> </dependency> </dependencies> <configuration> <formatterOptions> <!-- Use AOSP style instead of Google Style (4-space indentation). --> <googleJavaFormat.aosp>false</googleJavaFormat.aosp> <!-- Format the javadoc --> <googleJavaFormat.formatJavadoc>true</googleJavaFormat.formatJavadoc> <!-- Fix import order and remove any unused imports, but do no other formatting. --> <googleJavaFormat.fixImportsOnly>false</googleJavaFormat.fixImportsOnly> <!-- Do not fix the import order. Unused imports will still be removed. --> <googleJavaFormat.skipSortingImports>false</googleJavaFormat.skipSortingImports> <!-- Do not remove unused imports. Imports will still be sorted. --> <googleJavaFormat.skipRemovingUnusedImports>false</googleJavaFormat.skipRemovingUnusedImports> </formatterOptions> </configuration> </plugin> </plugins> </build>
添加完成之后,我们就可以测试了,比如我们修改下代码的某个部分,把他的格式调乱一点:
然后咱们直接打开sourcetree,选中想要提交的代码:
然后直接点击提交
然后在提交日志里面可以看到自动执行了maven插件
然后回到idea,可以看到刚才调乱的代码已经被格式化了
最后上git上看看:
可以看到git服务器上的代码已经是被格式化的了。
备注:
1、这个项目一定要重新从git上clone下来。
2、这个项目一定要重新从git上clone下来。
3、这个项目一定要重新从git上clone下来。
4、这里主要是使用git-code-format插件,所以一般我们这里添加的pom.xml里面的插件代码不要提交到git上去,留在本地即可,因为这个插件目前还有很多兼容问题,如果服务器使用了CI/CD,有自动执行mvn install这样的命令就会出现报错中断。所以非常不建议把pom.xml内容提交到git,留在本地即可,执行git push的时候他会自动执行本地的pom内容。
5、如果后期不想用这个git-code-format这个钩子导致git提交失败的话,可参考《sourcetree使用指南(十四)删除git的钩子hook》。
6、如果添加了git-code-format插件之后,使用mvn install打包失败的话,可参考《maven项目添加了:git-code-format-maven-plugin之后,使用mvn install打包失败怎么办?》
还没有评论,来说两句吧...