一. 统一编辑器格式化格式

  1. 下载code style 文件: code_style.xml

  2. IDEA 配置 在IntelliJ IDEA -> Preference -> Editor -> JAVA-> Code Style -> Java-> Scheme中,import code_style.xml 文件. idea config

  3. 配置每次提交,进行自动format

    1. IntelliJ IDEA -> Preference -> Version Control -> Commit 中, 配置 Reformat CodeOptimize Imports commit format config

二. 使用Checkstyle工具

  1. 下载Checkstyle文件,放入项目checkstyle文件夹中:

  2. 在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>
    
  3. 当执行 mvn verify 指令, 可校验项目是否符合相关规范. 后续可以在CI流程中,进行管控.

  4. 当有些文件需要屏蔽检查时,可以添加到 checkstyle-suppressionis.xml文件中

三. Checkstyle相关

checkstyle官网