【发布时间】:2018-09-26 19:13:39
【问题描述】:
我正在编写一个 sbt 任务来生成一些托管源,为此我使用fast-classpath-scanner 来提取scapegoat inspection class 的所有子类。
我首先将它实现为普通的 scala project 并且它有效,但是当尝试将它实现为 SBT 任务时我遇到了一个问题,那就是替罪羊罐子不是在运行任务的类路径中,所以类路径扫描器失败了。
我在project/plugins.sbt 文件中有任务的依赖项。
//adding the generation task dependencies
libraryDependencies ++= Seq(
"com.sksamuel.scapegoat" %% "scalac-scapegoat-plugin" % "1.3.4",
"io.github.lukehutch" % "fast-classpath-scanner" % "2.18.1"
)
我在build.sbt文件中实现了生成任务:
//source generator task (simplified)
sourceGenerators in Compile += Def.task {
//_root_ is needed because there is a sbt.io package in scope
import _root_.io.github.lukehutch.fastclasspathscanner.FastClasspathScanner
import _root_.io.github.lukehutch.fastclasspathscanner.matchprocessor.SubclassMatchProcessor
import com.sksamuel.scapegoat.Inspection
import scala.collection.mutable
val inspectionClass = classOf[Inspection]
val fastCPScanner = new FastClasspathScanner(inspectionClass.getPackage.getName)
val inspections = mutable.ListBuffer.empty[Inspection]
fastCPScanner
.matchSubclassesOf(
inspectionClass,
new SubclassMatchProcessor[Inspection] {
override def processMatch(matchingClass: Class[_ <: Inspection]): Unit = {
inspections += matchingClass.newInstance()
println("Hello World")
}
}
).scan()
val lines = inspections.toList.zipWithIndex map {
case (inspection, idx) => s"${inspections.text} -> ${idx}"
}
val scapegoatInspectionsFile = (sourceManaged in Compile).value / "scapegoat" / "inspections.scala"
IO.writeLines(scapegoatInspectionsFile, lines)
Seq(scapegoatInspectionsFile)
}.taskValue
注意:任务编译运行完美,问题是生成的文件是空的,因为扫描仪没有发现检查。这可以确认是因为processMatch 方法中的print("Hello world") 语句没有被执行(控制台没有输出)。
【问题讨论】:
-
project/plugins.sbt用于指定插件 for sbt 以便能够构建您的项目,但不能用于项目本身。您能否确认您是否将该代码放入project/plugins.sbt或只是普通的build.sbt?很可能您唯一需要做的就是将其移至build.sbt -
@J0HN 我更新了问题以进一步澄清我的问题。