一. 统一编辑器格式化格式
下载code style 文件: code_style.xml
IDEA 配置 在
IntelliJ IDEA
->Preference
->Editor
->JAVA
->Code Style
->Java
->Scheme
中,import code_style.xml 文件.配置每次提交,进行自动format
IntelliJ IDEA
->Preference
->Version Control
->Commit
中, 配置Reformat Code
和Optimize Imports
二. 使用Checkstyle工具
下载Checkstyle文件,放入项目checkstyle文件夹中:
在maven中,引入checkstyle插件
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>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.2.0</version> <configuration> <configLocation>checkstyle/checkstyle.xml</configLocation> <consoleOutput>true</consoleOutput> <detail>true</detail> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <id>checkstyle</id> <phase>validate</phase> <goals> <goal>check</goal> </goals> <configuration> <failOnViolation>true</failOnViolation> <failsOnError>true</failsOnError> </configuration> </execution> </executions> </plugin>
当执行
mvn verify
指令, 可校验项目是否符合相关规范. 后续可以在CI流程中,进行管控.当有些文件需要屏蔽检查时,可以添加到
checkstyle-suppressionis.xml
文件中