| 
 | 1 | +// `PmdModule` Performs checks on Java source files using https://pmd.github.io/[PMD] and generates reports from these checks.  | 
 | 2 | +//  | 
 | 3 | +// PMD is a source code analyzer for Java that finds common programming flaws like unused variables,  | 
 | 4 | +// empty catch blocks, unnecessary object creation, and more.  | 
 | 5 | +//  | 
 | 6 | +// To customize this plugin in a Java module:  | 
 | 7 | +//  | 
 | 8 | +// 1. Extend JavaModule with PmdModule.  | 
 | 9 | +// 2. Define a PMD ruleset file(s) with `pmdRulesets` (default is `pmd-rules.xml` in root).  | 
 | 10 | +// 3. Define the PMD version with `pmdVersion`.  | 
 | 11 | + | 
 | 12 | +package build  | 
 | 13 | + | 
 | 14 | +import mill._, javalib._  | 
 | 15 | +import mill.javalib.pmd.PmdModule  | 
 | 16 | + | 
 | 17 | +object `package` extends JavaModule, PmdModule {  | 
 | 18 | +  def pmdVersion = "7.15.0"  | 
 | 19 | +}  | 
 | 20 | + | 
 | 21 | +// Here's an example of a simple PMD ruleset file and a single Java source file.  | 
 | 22 | + | 
 | 23 | +/** See Also: pmd-ruleset.xml */  | 
 | 24 | +/** See Also: src/foo/EmptyCatchBlock.java */  | 
 | 25 | + | 
 | 26 | +// CAUTION: Be careful, rule formats can differ between major PMD versions.  | 
 | 27 | + | 
 | 28 | +// You can either run the `pmd` command on selected modules  | 
 | 29 | +// or use the external module `mill.javalib.pmd/`.  | 
 | 30 | + | 
 | 31 | +/** Usage  | 
 | 32 | + | 
 | 33 | +> ./mill pmd -s true -v false # <1>  | 
 | 34 | +Running PMD ...  | 
 | 35 | +Writing PMD output to .../out/pmd.dest/pmd-output.text ...  | 
 | 36 | +...src/foo/EmptyCatchBlock.java:7:	EmptyCatchBlock:	Avoid empty catch blocks...  | 
 | 37 | +PMD found 1 violation(s)  | 
 | 38 | + | 
 | 39 | +> ./mill mill.javalib.pmd/ # <2>  | 
 | 40 | +error: Running PMD ...  | 
 | 41 | +error: Writing PMD output to .../out/mill/javalib/pmd/PmdModule/pmd.dest/pmd-output.text ...  | 
 | 42 | +error: 1 tasks failed  | 
 | 43 | +error: mill.javalib.pmd.PmdModule.pmd PMD found 1 violation(s)  | 
 | 44 | + | 
 | 45 | +*/  | 
 | 46 | +// <1> `-s` shows the results on the console, `-v` does not fail the build  | 
 | 47 | +// <2> Use the external `mill.javalib.pmd` module with default configuration  | 
 | 48 | +//  | 
 | 49 | +// The `pmd` command accepts several options to customize its behavior. Mill supports:  | 
 | 50 | +//  | 
 | 51 | +// * `--stdout` / `-s` : Enable output to stdout. False by default. (PMD will write output to a file regardless of this option).  | 
 | 52 | +//  | 
 | 53 | +// * `--format` / `-f` : Output format of the report (`text`, `xml`, `html`). Defaults to `text`.  | 
 | 54 | +//  | 
 | 55 | +// * `--fail-on-violation` / `-v` : Fail if violations are found (true by default).  | 
 | 56 | +//  | 
 | 57 | +// For a full list of PMD options, see the PMD documentation:  | 
 | 58 | +// https://pmd.github.io/pmd/pmd_userdocs_cli_reference.html.  | 
 | 59 | +//  | 
 | 60 | +// Here's an example of producing HTML report:  | 
 | 61 | + | 
 | 62 | +/** Usage  | 
 | 63 | +> ./mill pmd -f "html" -s true -v false  | 
 | 64 | +Writing PMD output to .../out/pmd.dest/pmd-output.html ...  | 
 | 65 | +...<html><head><title>PMD</title></head><body>...  | 
 | 66 | +...<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align="center" cellspacing="0" cellpadding="3"><tr>...  | 
 | 67 | +...<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>...  | 
 | 68 | +...<tr bgcolor="lightgrey">...  | 
 | 69 | +...<td align="center">1</td>...  | 
 | 70 | +...<td width="*%">...src/foo/EmptyCatchBlock.java</td>...  | 
 | 71 | +...<td align="center" width="5%">7</td>...  | 
 | 72 | +...<td width="*"><a href="https://docs.pmd-code.org/pmd-doc-7.15.0/pmd_rules_java_errorprone.html#emptycatchblock">Avoid empty catch blocks</a></td>...  | 
 | 73 | +...</tr>...  | 
 | 74 | +...</table></body></html>...  | 
 | 75 | +...PMD found 1 violation(s)...  | 
 | 76 | +*/  | 
0 commit comments