Recently, when I was developing a project in the project team, I often encountered different code formats in the project. During merge, I often encountered a large number of conflicts. I searched online and found that Maven can integrate CheckStyle for code formatting review. Now I will do my experience to share.
Configure the CheckStyle plugin
Create a config folder in the project root directory and place the code specification configuration files in this folder, or you can customize them according to your own needs
Common syntax configurations are as follows:
checkstyle.sourceforge.net/config.html
Here I use the sun code specification, configuration file address is as follows:
Github.com/checkstyle/…
A plugin named Maven-checkstyle-plugin is used to execute checkstyle tasks in Maven. Here is a simple configuration. In the Maven project pom. XML file, add the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<configLocation>config/checkstyle.xml</configLocation>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnViolation>true</failOnViolation>
</configuration>
</execution>
</executions>
</plugin>
Copy the code
We defined the check Task to be executed during the Validate phase of Maven Lifecycle and fail the current build if any violations are found.
Maven-checkstyle-plugin comes with four specifications built in
- config/sun_checks.xml
- config/maven_checks.xml
- config/turbine_checks.xml
- config/avalon_checks.xml
The default value is sun_checks. XML. If you want to use the other three specifications, you simply configure Configuration.
After modifying the POM, you can run the CheckStyle check, and the check report file is in target\checkstyle-result.xml
Run the CheckStyle check
mvn checkstyle:check
Copy the code
Operation failure screenshot:
Screenshot of successful operation:
Initialize Git hooks
The main purpose of this step is to run the Check Style before committing the code, so that the code is formatted properly and cannot be committed if there is a problem.
Create a git-hooks folder from the config folder and place the pre-commit file in this folder. The pre-commit file is as follows:
#! /bin/sh
#set -x
echo "begin to execute hook"gst
mvn checkstyle:check
RESULT=$?
exit $RESULT
Copy the code
Create a new init.sh file in the root directory of the project. The content of the init.sh file is as follows:
cp config/git-hooks/pre-commit .git/hooks/
chmod +x .git/hooks/pre-commit
Copy the code
For Mac/Linux, the Windows user needs to manually copy the pre-commit file from the config/git-hooks directory to the.git/hooks/ directory of the project.
chmod +x ./init.sh
./init.sh
Copy the code
This script copies the Git commit hook to the.git/hooks/ directory of your project, so that it will be executed every time you commit.
If the code format is wrong, it will be submitted with an error, as shown below:
Code address: github.com/cxhello/spr…
Refer to the article
Github.com/checkstyle/…
Github.com/Snailclimb/…
Blog.csdn.net/frank823/ar…