Jacoco CLI报告能否按类过滤?能否仅生成Jar中指定类的报告?
Hey there! Let’s tackle your Jacoco CLI questions clearly and practically:
1. 能否通过classfiles进行覆盖率报告过滤?
Absolutely! You can filter which classes are included in the coverage report either by pre-processing your class files (extracting only the ones you need) or by using Jacoco’s built-in filtering flags alongside --classfiles. The latter is usually more straightforward for most cases.
2. 能否针对Jar文件中的单个/部分类生成报告?
Yes, you absolutely can—even though --classfiles only accepts paths to Jars, directories, or zip files, Jacoco provides --includes and --excludes parameters to narrow down to specific classes inside those Jars. Here’s how to do it:
方法1:使用--includes指定目标类(推荐)
Use the fully qualified class name (including package) with the --includes flag to target exactly the classes you want. For example, if you want to generate a report only for pkg1.ClassA inside /path/trial.jar, run this command:
java -jar jacococli.jar report your-recording.exec \ --classfiles /path/trial.jar \ --includes pkg1.ClassA \ --html /path/your-report-directory
- To include multiple classes, separate them with commas:
--includes pkg1.ClassA,pkg2.ClassB - Use wildcards to match entire packages:
--includes pkg1.*(covers all classes inpkg1) - Use
--excludesto exclude specific classes if needed:--excludes pkg1.UnwantedClass
方法2:提取Jar中的目标类到单独目录
If you prefer working directly with individual class files, extract the specific classes from the Jar first using a tool like unzip, then point --classfiles to the extracted directory:
# Extract only the target class from the Jar unzip /path/trial.jar pkg1/ClassA.class -d /tmp/target-classes # Generate report using the extracted class file java -jar jacococli.jar report your-recording.exec \ --classfiles /tmp/target-classes \ --html /path/your-report-directory
Both approaches will let you generate a coverage report focused solely on the classes you care about, even if your .exec file contains data for the entire application.
内容的提问来源于stack exchange,提问作者Anubhav Bansal

