@@ -5,32 +5,35 @@ import mill.api.{Discover, ExternalModule, TaskCtx}
55import mill .jvmlib .api .Versions
66import mill .scalalib .scalafmt .ScalafmtModule .sources
77import mill .scalalib .{CoursierModule , Dep , DepSyntax , OfflineSupportModule }
8- import mill .util .Jvm
8+ import mill .util .{ Jvm , Version }
99
1010/**
1111 * Checks Java source files with PMD static code analyzer [[https://pmd.github.io/ ]].
1212 */
1313trait PmdModule extends CoursierModule , OfflineSupportModule {
1414
1515 /**
16- * Runs PMD and returns the number of violations found (exit code) .
16+ * Runs PMD.
1717 *
1818 * @note [[sources ]] are processed when no [[PmdArgs.sources ]] are specified.
1919 */
2020 def pmd (@ mainargs.arg pmdArgs : PmdArgs ): Command [(exitCode : Int , outputPath : PathRef )] =
2121 Task .Command {
22- val (outputPath, exitCode) = pmd0(pmdArgs.format, pmdArgs.sources)()
22+ val res = pmd0(pmdArgs.format, pmdArgs.sources)()
2323 pmdHandleExitCode(
2424 pmdArgs.stdout,
2525 pmdArgs.failOnViolation,
26- exitCode,
27- outputPath,
26+ res. exitCode,
27+ res. outputPath,
2828 pmdArgs.format
2929 )
30- (exitCode, outputPath)
30+ (res. exitCode, res. outputPath)
3131 }
3232
33- protected def pmd0 (format : String , leftover : mainargs.Leftover [String ]) =
33+ protected def pmd0 (
34+ format : String ,
35+ leftover : mainargs.Leftover [String ]
36+ ): Task [(outputPath : PathRef , exitCode : Int )] =
3437 Task .Anon {
3538 val output = Task .dest / s " pmd-output. $format"
3639 os.makeDir.all(output / os.up)
@@ -54,9 +57,9 @@ trait PmdModule extends CoursierModule, OfflineSupportModule {
5457 else " net.sourceforge.pmd.cli.PmdCli"
5558 val jvmArgs = pmdLanguage().map(lang => s " -Duser.language= $lang" ).toSeq
5659
57- Task .log.info(" Running PMD..." )
60+ Task .log.info(" Running PMD ..." )
5861 Task .log.debug(s " with $args" )
59- Task .log.info(s " Writing PMD output to: $output... " )
62+ Task .log.info(s " Writing PMD output to $output ..." )
6063
6164 val exitCode = Jvm .callProcess(
6265 mainCls,
@@ -69,7 +72,7 @@ trait PmdModule extends CoursierModule, OfflineSupportModule {
6972 jvmArgs = jvmArgs
7073 ).exitCode
7174
72- (PathRef (output), exitCode)
75+ (outputPath = PathRef (output), exitCode = exitCode)
7376 }
7477
7578 private def pmdHandleExitCode (
@@ -87,7 +90,7 @@ trait PmdModule extends CoursierModule, OfflineSupportModule {
8790 reportViolations(failOnViolation, countViolations(output, format, stdout))
8891 case 5 =>
8992 reportViolations(failOnViolation, countViolations(output, format, stdout))
90- throw new RuntimeException (" At least one recoverable PMD error has occurred." )
93+ Task .fail (" At least one recoverable PMD error has occurred." )
9194 case x => Task .log.error(s " Unsupported PMD exit code: $x" )
9295 exitCode
9396 }
@@ -127,10 +130,9 @@ trait PmdModule extends CoursierModule, OfflineSupportModule {
127130 failOnViolation : Boolean ,
128131 violationCount : Option [Int ]
129132 )(implicit ctx : TaskCtx ): Unit = {
130- if (failOnViolation)
131- throw new RuntimeException (s " PMD found ${violationCount.getOrElse(" some" )} violation(s) " )
132- else
133- Task .log.error(s " PMD found ${violationCount.getOrElse(" some" )} violation(s) " )
133+ val msg = s " PMD found ${violationCount.getOrElse(" some" )} violation(s) "
134+ if (failOnViolation) Task .fail(msg)
135+ else Task .log.error(msg)
134136 }
135137
136138 /**
@@ -155,22 +157,19 @@ trait PmdModule extends CoursierModule, OfflineSupportModule {
155157 }
156158
157159 /** Helper to check if the version is <= 6. False by default. */
158- private def isPmd6OrOlder (version : String ): Boolean = {
159- version
160- .split(" :" ).lastOption
161- .flatMap(_.takeWhile(_ != '.' ).toIntOption)
162- .exists(_ < 7 )
163- }
160+ private def isPmd6OrOlder (version : String ): Boolean =
161+ ! Version .isAtLeast(version, " 7" )(using Version .IgnoreQualifierOrdering )
164162
165163 /** PMD version. */
166164 def pmdVersion : T [String ] = Task { Versions .pmdVersion }
167165}
168166
169167/**
170168 * External module for PMD integration.
171- * Allows usage via `import mill.javalib.pmd.PmdModule` in build.sc.
169+ *
170+ * Allows usage via `import mill.javalib.pmd/` in build.mill.
172171 */
173- object PmdModule extends ExternalModule , PmdModule {
172+ object PmdModule extends ExternalModule , PmdModule , TaskModule {
174173 lazy val millDiscover : Discover = Discover [this .type ]
175- def defaultCommandName () = " pmd"
174+ override def defaultTask () = " pmd"
176175}
0 commit comments