When using code analysis tools to scan a project, it will output all the problems in each file at once. This makes it difficult to perform an effective check on the codes under development before cleaning up all the existing problems.
In response to this pain point, I extended those tools including checkstyle, PMD and jacoco to perform scanning only on incremental changes of code lines.
- You can run the following commands to install the check tools
# Download the install.sh
curl https://raw.githubusercontent.com/yangziwen/diff-check/master/hooks/install.sh > install.sh
# Install the hook to a specified git repository
sh install.sh --repo-path=${the_absolute_path_of_your_git_repository}
# Or install the hook globally
sh install.sh --global=true
- You can enable or disable the check in the following way
# Enable the check of checkstyle
git config diff-check.checkstyle.enabled true
# Disable the check of checkstyle
git config diff-check.checkstyle.enabled false
# Enable the check of PMD
git config diff-check.pmd.enabled true
# Disable the check of PMD
git config diff-check.pmd.enabled false
- You can set the check rules in the following way
git config diff-check.checkstyle.config-file /custom_checks.xml
git config diff-check.pmd.rulesets rulesets/java/ali-all.xml
- You can set the exampt path in the following way
git config diff-check.checkstyle.exclude-regexp .+-client/.*
git config diff-check.pmd.exclude-regexp .+-client/.*
- Config the maven plugin
<plugin>
<groupId>io.github.yangziwen</groupId>
<artifactId>diff-jacoco-maven-plugin</artifactId>
<version>0.0.6</version>
<configuration>
<excludes>
<!-- Specify the path to exclude -->
<exclude>io/github/yangziwen/quickdao/example/entity/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- config the path of the report -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-diff</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
- Run the unit tests
# Incremental code coverage scanning is achieved by specifying commit or branch for `jacoco.diff.oldrev` and `jacoco.diff.newrev`
# If these two parameters are not specified, a full coverage scan will be performed
mvn test -Djacoco.diff.oldrev=9ac9182 -Djacoco.diff.newrev=HEAD
# Use `jacoco.diff.against` parameter,you can compare the difference between HEAD and any other commit or branch, and get the code coverage result between them.
mvn test -Djacoco.diff.against=origin/master
# Use the `jacoco.author.name` or `jacoco.author.email` parameters to complete the coverage scanning according to the incremental code of the specified submitter
mvn test -Djacoco.diff.against=origin/master -Djacoco.author.name=yangziwen
You can run a single test class and get the report of it
mvn clean test -Djacoco.diff.against=origin/master -DskipTests=false -Dtest=io.github.yangziwen.quickdao.example.repository.UserSpringJdbcRepositoryTest -DfailIfNoTests=false
- Get the result
Full Code Coverage | Incremental Code Coverage |
---|---|
- In addition to the sun_checks.xml and google_checks.xml provided by checkstyle by default, two other configurations, custom_checks.xml and custom_full_checks.xml which basically conform to the Alibaba code specification, have been added. You can also use your favorite style configuration by specifying the absolute file path with -c option.
- Add the
lombok.config
file to the root folder of your project, and add a line oflombok.addLombokGeneratedAnnotation = true
in the file, then all the methods generated by lombok will be ignored in the code coverage scanning(https://projectlombok.org/features/configuration). - Scanning with a changed file that has not been submitted and also not been added to the staging area may cause the modified code line calculated being inconsistent with the code line of the actual scanned file in the workspace, so please submit all changes first.