I've been asked the question, "What's your code style?" At that time, I thought, "Encapsulate the function, encapsulate the code that appears more than twice into the function for reuse, with as few exits as possible". "Function length should not exceed 30 lines", "write good comments and documentation", "variable names should fully reflect the meaning of parameters", "write good test code", etc. In retrospect, the code format is also important, especially the introduction of checkStyle, SpotBugs,Jacoco and other code style and test coverage plugins. The same can be said. This article shows you how to introduce these plug-ins into your project.
1. Introduce the CheckStyle plugin in Maven project and configure it
Set the checkstyle. XML file and which phase to bind to Maven. Post your own:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> < version > 3.1.1 < / version > < configuration > <configLocation>${project.basedir}/src/main/resources/checkstyle.xml</configLocation> <encoding>UTF-8</encoding> <consoleOutput>true</consoleOutput> <failsOnError>true</failsOnError> <linkXRef>false</linkXRef> </configuration> <dependencies> <dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.38</version> </dependency> </dependencies> <executions> <id>validate</id> <phase>validate</phase> <goals> <goal>check</goal> </goals> </execution> </executions> </plugin>Copy the code
Enter the checkstylewebsite.
The checkstyle.xml file will show you how to configure it. If necessary, you can refer to Google’s version directly, but it definitely needs to be modified according to the actual situation, for example:
Modification 1:
Modification 2:
After the configuration is complete, import the configuration file in IDEAL, first install the Checstyle plugin in IDEA:
Add a custom checkstyle.xml file:
Checkstyle plugin or runmvn validate
Command scan items:
2. Introduce SpotBugs to detect bugs in your code
Install SpotBugs in the Maven project. Pom. XML is configured as follows:
< plugin > < groupId > com. Making. Spotbugs < / groupId > < artifactId > spotbugs maven - plugin < / artifactId > < version > 4.1.3 < / version > <dependencies> <! -- overwrite dependency on spotbugs if you want to specify the version of spotbugs --> <dependency> < the groupId > com. Making. Spotbugs < / groupId > < artifactId > spotbugs < / artifactId > < version > 4.2.0 < / version > < / dependency > </dependencies> </plugin>Copy the code
Once configured, you can use the MVN Spotbugs: Check or MVN Spotbugs: GUI command to check for bugs in your code.
3. Jacoco plug-in is introduced to verify the test code coverage
To introduce the Jacoco, POM. XML configuration in Maven project, you can go to the official website, for example:
< plugin > < groupId > org. Jacoco < / groupId > < artifactId > jacoco maven - plugin < / artifactId > < version > 0.8.5 < / version > <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>BUNDLE</element> <limits> <limit> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.60</ limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin>Copy the code
Running MVN verify command, you can see in the log shows the jacoco information, and in the project target/site/jacoco/index. The HTML file, you can see the project test coverage report (screenshot from jacoco website) :
In practice, if the Bean’s get()/set() methods use lombok, jacoco will also consider equals,hashcode methods to be tested, so a new lombok. Config file will be created in the SRC directory. Refer to stack Overflow answers, modify the pom. XML file, or add lombok.config:
lombok.addLombokGeneratedAnnotation = true
Copy the code
When encountering a class that needs to be excluded, such as the boot class of SpringBoot, configure exclude in pom.xml. For example:
<configuration>
<excludes>
<exclude>com/example/springboot/Application.class</exclude>
</excludes>
</configuration>
Copy the code
4. Configure the Github CI Workflow
Configure CI in the Action bar of your Git project repository:
For example, you can use the Java With Maven template to change the last sentence of the maven.yml file to:
run: mvn verify
Copy the code
Thus, all submissions from the branch trunk need to be verified by executing the Maven verify command.