• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / whitespace-maven-plugin / 652

22 Mar 2026 02:40AM UTC coverage: 18.803%. Remained the same
652

push

github

hazendaz
[mvn] Update maven wrapper

22 of 140 branches covered (15.71%)

Branch coverage included in aggregate %.

44 of 211 relevant lines covered (20.85%)

0.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/main/java/com/github/dantwining/whitespace/WhitespaceUtils.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2011-2026 Hazendaz
4
 */
5
package com.github.dantwining.whitespace;
6

7
import java.io.File;
8
import java.io.IOException;
9
import java.nio.charset.Charset;
10
import java.nio.file.Files;
11
import java.nio.file.StandardOpenOption;
12
import java.util.ArrayList;
13
import java.util.Collection;
14
import java.util.List;
15

16
import org.apache.commons.io.FileUtils;
17
import org.apache.commons.lang3.StringUtils;
18
import org.apache.maven.plugin.MojoExecutionException;
19
import org.apache.maven.plugin.MojoFailureException;
20
import org.apache.maven.plugin.logging.Log;
21

22
/**
23
 * The Class WhitespaceUtils.
24
 */
25
public final class WhitespaceUtils {
26

27
    /**
28
     * Instantiates a new whitespace utils.
29
     */
30
    private WhitespaceUtils() {
31
        // Do not instantiate
32
    }
33

34
    /**
35
     * Detect whitespace.
36
     *
37
     * @param verify
38
     *            the verify
39
     * @param searchBaseDirectory
40
     *            the search base directory
41
     * @param extensions
42
     *            the extensions
43
     * @param mavenLog
44
     *            the maven log
45
     * @param encoding
46
     *            the character encoding used for resources
47
     * @param failOnReadError
48
     *            the boolean flag to choose treatment for read errors
49
     *
50
     * @throws MojoExecutionException
51
     *             the mojo execution exception
52
     * @throws MojoFailureException
53
     *             the mojo failure exception
54
     */
55
    public static void detectWhitespace(boolean verify, File searchBaseDirectory, String extensions, Log mavenLog,
56
            String encoding, boolean failOnReadError) throws MojoExecutionException, MojoFailureException {
57

58
        if (!searchBaseDirectory.isDirectory()) {
1✔
59
            mavenLog.debug("Skipping non-existent directory: " + searchBaseDirectory.getAbsolutePath());
1✔
60
            return;
1✔
61
        }
62
        String cleanExtensions = StringUtils.deleteWhitespace(extensions);
1✔
63
        mavenLog.info("Processing the following extensions: " + cleanExtensions);
1✔
64

65
        Collection<File> matchingFiles = FileUtils.listFiles(searchBaseDirectory, cleanExtensions.split(","), true);
1✔
66

67
        List<String> verifyFailed = new ArrayList<>();
1✔
68

69
        for (File matchingFile : matchingFiles) {
1✔
70
            mavenLog.debug("Reading file: " + matchingFile.getAbsolutePath());
1✔
71

72
            List<String> lines;
73
            try {
74
                lines = Files.readAllLines(matchingFile.toPath(), Charset.forName(encoding));
1✔
75
            } catch (IOException e) {
×
76
                if (failOnReadError) {
×
77
                    throw new MojoExecutionException(
×
78
                            "Failed to read lines from " + matchingFile.getAbsolutePath() + ": " + e.getMessage(), e);
×
79
                } else {
80
                    mavenLog.warn(
×
81
                            "Failed to read lines from " + matchingFile.getAbsolutePath() + ": " + e.getMessage());
×
82
                    // Skip this file and continue processing others
83
                    continue;
×
84
                }
85
            }
1✔
86

87
            boolean isFileModified = false;
1✔
88
            List<String> trimmedLines = new ArrayList<>(lines.size());
1✔
89
            int lineNumber = 0;
1✔
90

91
            for (String line : lines) {
1✔
92

93
                if (mavenLog.isDebugEnabled()) {
1!
94
                    lineNumber++;
×
95
                }
96

97
                String trimmedLine = StringUtils.stripEnd(line, null);
1✔
98

99
                boolean isLineModified = !trimmedLine.equals(line);
1✔
100

101
                if (mavenLog.isDebugEnabled() && isLineModified) {
1!
102
                    mavenLog.debug("Whitespace found on line " + lineNumber);
×
103
                }
104

105
                trimmedLines.add(trimmedLine);
1✔
106

107
                isFileModified = isFileModified || isLineModified;
1✔
108
            }
1✔
109

110
            if (isFileModified) {
1✔
111

112
                if (verify) {
1✔
113
                    verifyFailed.add(matchingFile.getAbsolutePath());
1✔
114
                } else {
115
                    try {
116
                        Files.write(matchingFile.toPath(), trimmedLines, Charset.forName(encoding),
1✔
117
                                StandardOpenOption.TRUNCATE_EXISTING);
118
                    } catch (IOException e) {
×
119
                        throw new MojoExecutionException("Failed to write lines to " + matchingFile.getAbsolutePath(),
×
120
                                e);
121
                    }
1✔
122
                }
123

124
            }
125
        }
1✔
126

127
        if (!verifyFailed.isEmpty()) {
1✔
128
            throw new MojoFailureException("Trailing whitespace found in " + verifyFailed);
1✔
129
        }
130
    }
1✔
131

132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc