Skip to content

Commit 6e8de84

Browse files
committed
📝 添加代码风格规范文档
1 parent d956c78 commit 6e8de84

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default defineConfig({
3737
{
3838
text: '更多',
3939
items: [
40+
{text: '代码风格规范', link: "/other/code-style"},
4041
{text: 'Maven 资源文件占位符使用', link: "/other/maven-resource-filter"},
4142
{text: '二开代码同步', link: "/other/syncing-fork"},
4243
{text: 'git commit emoji', link: "/other/git-emoji"},

docs/other/code-style.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 代码规范
2+
3+
项目使用 git 本地钩子,在代码提交时会进行代码格式和代码风格校验。
4+
必须需配置 Maven 的环境变量,否则会导致钩子校验失败,无法正常提交,可以通过在命令行窗口输入 `mvn -v` 确认环境变量配置是否正确。
5+
:::warning 注意
6+
拉取项目后,务必在根路径下先进行一次项目的 clean install 保证 git hooks 的正确安装。
7+
:::
8+
9+
## 代码格式化
10+
11+
项目统一使用 spring-javaformat Maven 插件进行代码格式化,所有代码必须使用该插件进行格式化再进行提交。
12+
可以通过图形化操作或者命令行执行 `mvn spring-javaformat:apply` 进行代码统一格式化。
13+
14+
对于部分特殊代码需要跳过格式化控制的,可以通过在上下文添加指定注释进行控制,示例如下:
15+
16+
```java
17+
// @formatter:off
18+
无需格式化的部分代码
19+
// @formatter:on
20+
```
21+
22+
23+
## 代码风格
24+
代码风格使用 Spring 的 CheckStyle 约束规则,同时通过 checkstyle maven 插件进行校验。
25+
为了方便修改,ballcat 拷贝了一份 spring 的 checkstyle.xml 文件置于 **ballcat-parent** 模块下。
26+
27+
28+
29+
## Idea 配置
30+
31+
代码格式化和代码风格默认都是在代码提交或者项目编译的时候进行检测,这并不友好,我们可以通过安装插件在代码编写过程中提前发现风格问题。
32+
33+
34+
### 格式化插件
35+
36+
Idea 默认的格式化行为不符合 spring-javaformat 的格式化规范,我们可以安装 spring-javaforamt 的插件来进行覆盖 Idea 的默认格式化行为。
37+
38+
由于 spring-javaforamt 插件并没有发布到 Idea 插件市场,所以需要我们手动下载安装。
39+
40+
目前 ballcat 使用的是 0.0.41 版本的 spring-javaforamt,对应版本的插件下载地址:[spring-javaformat-intellij-idea-plugin-0.0.41.jar](https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin/0.0.41/spring-javaformat-intellij-idea-plugin-0.0.41.jar)
41+
42+
下载完成后,通过以下方式选择下载的插件文件完成安装:
43+
44+
![install-idea-plugin-from-disk](./img/code-style/install-idea-plugin-from-disk.png)
45+
46+
> spring-javaforamt 其他版本的插件下载地址可以在 Maven 仓库中找到:[spring-javaformart-plugin-maven](https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin/)
47+
48+
49+
50+
### CheckStyle 插件
51+
52+
**插件安装**
53+
54+
在插件市场中,搜索 CheckStyle 插件并进行安装。
55+
56+
![CheckStyle Plugin Install](./img/code-style/checkstyle-plugin-install.png)
57+
58+
**插件配置**
59+
60+
spring-javaformat 配合 checkstyle 需要额外下载两个依赖包:
61+
- [spring-javaformat-config-0.0.41.jar](https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-config/0.0.41/spring-javaformat-config-0.0.41.jar)
62+
- [spring-javaformat-checkstyle-0.0.41.jar](https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-checkstyle/0.0.41/spring-javaformat-checkstyle-0.0.41.jar)
63+
64+
下载完成后,打开 Idea 的 Settings, 找到 Tools -> Checkstyle,按下图所示进行配置:
65+
66+
![checkstyle-plugin-config](./img/code-style/checkstyle-plugin-config.png)
67+
68+
1. 在 Third-Party Checks 一栏中添加刚才下载的依赖包:
69+
- spring-javaformat-checkstyle-0.0.41.jar
70+
- spring-javaformat-config-0.0.41.jar
71+
72+
2. 在 Configuration File 中添加 ballcat 的 checkStyle 配置文件
73+
74+
文件位于项目 **ballcat-parent** 模块下: **/src/checkstyle/checkstyle.xml**
75+
76+
3. CheckStyle 的版本设置为 9.3
77+
78+
### Idea 导包设置
79+
80+
风格规则中控制了导包顺序,需要设置下 idea 默认的导包顺序, 依次打开 Editor -> Code Style -> Java -> Imports,按如下规则进行设置:
81+
```java
82+
import java.*
83+
<blank line>
84+
import javax.*
85+
<blank line>
86+
import all other imports
87+
<blank line>
88+
import static all other imports
89+
```
90+
91+
92+
另外不允许使用 * 进行全量导入,所以以下两个设置的值需要设置的高一点:
93+
- Class count to use import with '*'
94+
- Names count to use static import with '*'
95+
42.6 KB
Loading
42.8 KB
Loading
44.5 KB
Loading

0 commit comments

Comments
 (0)