Skip to content

Commit cd0baa7

Browse files
svaningelgemSteven Van Ingelgem
andauthored
Exclude files which are excluded by jacoco (#21)
* version bump * wip * Make tests work at least * Cleaning up tests even further * WIP * WIP * Improving coverage * WIP * Optimize imports * reformat * Fix only knowing the last projects exclusions. * WIP * Exclude all generated code by default. * All tests are working again now * All tests are working again now * Improved coverage a tiny bit. * reformat/optimize * Updated README.md * Adding some other test for increasing the branch coverage * Trying to improve coverage for these branches. * Improving code coverage * reformat --------- Co-authored-by: Steven Van Ingelgem <[email protected]>
1 parent 3559aad commit cd0baa7

28 files changed

+1272
-477
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ updates:
99
ignore:
1010
# Ignore patch updates for Maven plugins to reduce noise
1111
- dependency-name: "org.apache.maven.plugins:*"
12-
update-types: ["version-update:semver-patch"]
12+
update-types: [ "version-update:semver-patch" ]
1313

1414
- package-ecosystem: "github-actions"
1515
directory: "/"

README.md

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# JaCoCo Console Reporter Maven Plugin
22

3-
A custom Maven plugin that generates a textual tree-like coverage report from JaCoCo's execution data files, displaying coverage metrics (Class %, Method %, Branch %, Line %) for packages, source files, and the entire project.
3+
A custom Maven plugin that generates a textual tree-like coverage report from JaCoCo's execution data files, displaying
4+
coverage metrics (Class %, Method %, Branch %, Line %) for packages, source files, and the entire project.
45

56
## Features
7+
68
- Reads coverage data from `jacoco.exec` files
79
- Analyzes class files from the project's build output directory
810
- Outputs a hierarchical console-based report with coverage metrics
@@ -12,14 +14,19 @@ A custom Maven plugin that generates a textual tree-like coverage report from Ja
1214
- Automatic scanning for `jacoco.exec` files across modules
1315
- Support for custom JaCoCo execution file patterns
1416
- Combined weighted coverage score based on customizable weights
17+
- Exclude target directory to ignore generated files
1518

1619
## Prerequisites
20+
1721
- Maven 3.x
1822
- JaCoCo plugin configured in your project to generate `jacoco.exec`
1923

2024
## Installation
25+
2126
Add the plugin to your project's pom.xml:
27+
2228
```xml
29+
2330
<build>
2431
<plugins>
2532
<plugin>
@@ -40,7 +47,9 @@ Add the plugin to your project's pom.xml:
4047
```
4148

4249
## Usage
50+
4351
Run the plugin after tests:
52+
4453
```bash
4554
mvn verify
4655
```
@@ -49,23 +58,25 @@ Ensure the JaCoCo plugin has executed beforehand to generate jacoco.exec.
4958

5059
## Configuration
5160

52-
| Parameter | Description | Default Value |
53-
|-----------------------|-----------------------------------------------------------|------------------------------------------|
54-
| `jacocoExecFile` | Path to the JaCoCo execution data file | `${project.build.directory}/jacoco.exec` |
55-
| `classesDirectory` | Directory containing compiled classes | `${project.build.outputDirectory}` |
56-
| `deferReporting` | Defer reporting until the end (for multi-module projects) | `true` |
57-
| `showFiles` | Whether to show individual source files in the report | `false` |
58-
| `showTree` | Whether to show the tree structure in the report | `true` |
59-
| `showSummary` | Whether to show the summary information | `true` |
60-
| `scanModules` | Automatically scan for exec files in project modules | `false` |
61-
| `baseDir` | Base directory for module scanning | `${project.basedir}` |
62-
| `additionalExecFiles` | Additional exec files to include in the report | `[]` |
63-
| `weightClassCoverage` | Weight for class coverage in combined score | `0.1` |
64-
| `weightMethodCoverage`| Weight for method coverage in combined score | `0.1` |
65-
| `weightBranchCoverage`| Weight for branch coverage in combined score | `0.4` |
66-
| `weightLineCoverage` | Weight for line coverage in combined score | `0.4` |
61+
| Parameter | Description | Default Value |
62+
|-------------------------------|-----------------------------------------------------------|------------------------------------------|
63+
| `jacocoExecFile` | Path to the JaCoCo execution data file | `${project.build.directory}/jacoco.exec` |
64+
| `classesDirectory` | Directory containing compiled classes | `${project.build.outputDirectory}` |
65+
| `deferReporting` | Defer reporting until the end (for multi-module projects) | `true` |
66+
| `showFiles` | Whether to show individual source files in the report | `false` |
67+
| `showTree` | Whether to show the tree structure in the report | `true` |
68+
| `showSummary` | Whether to show the summary information | `true` |
69+
| `scanModules` | Automatically scan for exec files in project modules | `false` |
70+
| `baseDir` | Base directory for module scanning | `${project.basedir}` |
71+
| `additionalExecFiles` | Additional exec files to include in the report | `[]` |
72+
| `weightClassCoverage` | Weight for class coverage in combined score | `0.1` |
73+
| `weightMethodCoverage` | Weight for method coverage in combined score | `0.1` |
74+
| `weightBranchCoverage` | Weight for branch coverage in combined score | `0.4` |
75+
| `weightLineCoverage` | Weight for line coverage in combined score | `0.4` |
76+
| `ignoreFilesInBuildDirectory` | Ignore autogenerated files in build directory | `true` |
6777

6878
## Default Output
79+
6980
```text
7081
[INFO] Overall Coverage Summary
7182
[INFO] Package │ Class, % │ Method, % │ Branch, % │ Line, %
@@ -85,6 +96,7 @@ Ensure the JaCoCo plugin has executed beforehand to generate jacoco.exec.
8596
```
8697

8798
## Output with all options on
99+
88100
```text
89101
[INFO] Overall Coverage Summary
90102
[INFO] Package │ Class, % │ Method, % │ Branch, % │ Line, %
@@ -119,6 +131,7 @@ This will wait with generating the report until the last module in the build.
119131
If your JaCoCo plugin uses a non-default location for the execution data file:
120132

121133
```xml
134+
122135
<plugin>
123136
<groupId>io.github.svaningelgem</groupId>
124137
<artifactId>jacoco-console-reporter</artifactId>
@@ -135,6 +148,7 @@ If your JaCoCo plugin uses a non-default location for the execution data file:
135148
You can configure which parts of the report are displayed:
136149

137150
```xml
151+
138152
<plugin>
139153
<groupId>io.github.svaningelgem</groupId>
140154
<artifactId>jacoco-console-reporter</artifactId>
@@ -156,6 +170,7 @@ You can configure which parts of the report are displayed:
156170
You can adjust the weights used to calculate the combined coverage score:
157171

158172
```xml
173+
159174
<plugin>
160175
<groupId>io.github.svaningelgem</groupId>
161176
<artifactId>jacoco-console-reporter</artifactId>
@@ -170,6 +185,56 @@ You can adjust the weights used to calculate the combined coverage score:
170185
</plugin>
171186
```
172187

188+
### Excluding Files from Coverage
189+
190+
The plugin supports excluding specific files or packages from coverage reports. There are two ways to configure
191+
exclusions:
192+
193+
1. **Using JaCoCo Exclusions**: The plugin automatically respects exclusion patterns defined in your JaCoCo plugin
194+
configuration:
195+
196+
```xml
197+
<plugin>
198+
<groupId>org.jacoco</groupId>
199+
<artifactId>jacoco-maven-plugin</artifactId>
200+
<configuration>
201+
<excludes>
202+
<exclude>com/example/generated/**/*</exclude>
203+
<exclude>**/*Controller.class</exclude>
204+
<exclude>com/example/model/*</exclude>
205+
</excludes>
206+
</configuration>
207+
</plugin>
208+
```
209+
210+
2. **Automatically Exclude Build Directory Files**: By default, the plugin will ignore files in the build directory,
211+
which are typically auto-generated. You can disable this with:
212+
213+
```xml
214+
<plugin>
215+
<groupId>io.github.svaningelgem</groupId>
216+
<artifactId>jacoco-console-reporter</artifactId>
217+
<configuration>
218+
<ignoreFilesInBuildDirectory>false</ignoreFilesInBuildDirectory>
219+
</configuration>
220+
</plugin>
221+
```
222+
223+
#### Exclusion Pattern Syntax
224+
225+
The exclusion pattern for JaCoCo is:
226+
227+
- `*` matches any character except path separators
228+
- `**` matches any directory
229+
- File paths use `/` as separator regardless of the operating system
230+
- Patterns without the `.class` suffix will automatically match class files
231+
232+
For example:
233+
234+
- `com/example/model/*` - Excludes all files directly in the model package
235+
- `com/example/generated/**/*` - Excludes all files in generated and its subpackages
236+
- `**/*Controller.class` - Excludes all files ending with "Controller.class" in any package
237+
173238
## Implementation Details
174239

175240
The plugin works by:
@@ -181,6 +246,7 @@ The plugin works by:
181246
5. Calculating coverage metrics (class, method, branch, line) for each node
182247
6. Generating a tree-like report to the console
183248
7. Computing a weighted combined coverage score
249+
8. Applying exclusion patterns to filter out specific files or packages
184250

185251
## Contributing
186252

docs/gpg.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
2-
3-
41
## Generate keys
2+
53
```bash
64
gpg --gen-key
75
```
6+
87
## List them
8+
99
```bash
1010
gpg --list-keys
1111
```
12+
1213
Output is like:
14+
1315
```text
1416
$ gpg --list-secret-keys --keyid-format=long
1517
/home/steven/.gnupg/pubring.kbx
@@ -21,29 +23,37 @@ ssb rsa3072/3D2ADE7187466D99 2025-02-22 [E] [expires: 2027-02-22]
2123
```
2224

2325
## Upload them
26+
2427
```bash
2528
gpg --keyserver keyserver.ubuntu.com --send-keys 99CEEF7F4D5085C2
2629
```
2730

2831
## Show the public key
32+
2933
```bash
3034
gpg --armor --export 99CEEF7F4D5085C2
3135
```
36+
3237
Output:
38+
3339
```text
3440
-----BEGIN PGP PUBLIC KEY BLOCK-----
3541
[...]
3642
-----END PGP PUBLIC KEY BLOCK-----
3743
```
3844

3945
## Set the GPG key in your github profile
46+
4047
URL: https://github.com/settings/keys
4148

4249
## Export the private key
50+
4351
```bash
4452
gpg --armor --export-secret-key [email protected]
4553
```
54+
4655
Output:
56+
4757
```text
4858
-----BEGIN PGP PRIVATE KEY BLOCK-----
4959
[...]

jacoco-console-reporter/src/main/java/io/github/svaningelgem/CharsetDetector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
public class CharsetDetector {
1111
static CharsetDetector instance = null;
1212

13-
private CharsetDetector() { }
13+
private CharsetDetector() {
14+
}
1415

1516
public static CharsetDetector getInstance() {
1617
if (instance == null) {

jacoco-console-reporter/src/main/java/io/github/svaningelgem/Defaults.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Defaults {
2323
final String divider;
2424

2525
static Defaults instance = null;
26+
2627
public static Defaults getInstance() {
2728
if (instance == null) {
2829
instance = new Defaults();
@@ -71,6 +72,6 @@ public Defaults(Charset currentCharset) {
7172
@NotNull String formatCoverage(double covered, double total) {
7273
if (total <= 0) return " ***** (0/0)";
7374
double percentage = covered / total * 100;
74-
return String.format("%5.2f%% (%d/%d)", percentage, (int)covered, (int)total);
75+
return String.format("%5.2f%% (%d/%d)", percentage, (int) covered, (int) total);
7576
}
7677
}

jacoco-console-reporter/src/main/java/io/github/svaningelgem/DirectoryNode.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public boolean shouldInclude() {
4343
return !sourceFiles.isEmpty() || subdirectories.values().stream().anyMatch(DirectoryNode::shouldInclude);
4444
}
4545

46-
private <T extends FileSystemNode> void printNodes(org.apache.maven.plugin.logging.Log log, String prefix,
47-
String format, String packagePath, boolean showFiles, @NotNull List<T> nodes, boolean extraCheck) {
46+
<T extends FileSystemNode> void printNodes(org.apache.maven.plugin.logging.Log log, String prefix,
47+
String format, String packagePath, boolean showFiles, @NotNull List<T> nodes, boolean extraCheck) {
4848
for (int i = 0; i < nodes.size(); i++) {
4949
boolean isLast = (i == nodes.size() - 1) && extraCheck;
5050
FileSystemNode node = nodes.get(i);
@@ -53,13 +53,12 @@ private <T extends FileSystemNode> void printNodes(org.apache.maven.plugin.loggi
5353
}
5454
}
5555

56-
private @NotNull String determineNewPrefix(@NotNull String oldPrefix, boolean isLast) {
56+
@NotNull String determineNewPrefix(@NotNull String oldPrefix, boolean isLast) {
5757
String prefix = oldPrefix;
5858

5959
if (prefix.endsWith(Defaults.getInstance().corner)) {
6060
prefix = prefix.substring(0, prefix.length() - Defaults.getInstance().corner.length()) + Defaults.getInstance().lastDirSpace;
61-
}
62-
else if (prefix.endsWith(Defaults.getInstance().tee)) {
61+
} else if (prefix.endsWith(Defaults.getInstance().tee)) {
6362
prefix = prefix.substring(0, prefix.length() - Defaults.getInstance().tee.length()) + Defaults.getInstance().verticalLine;
6463
}
6564

0 commit comments

Comments
 (0)