有谁知道如何配置SBT项目以运行注释处理器(APT)?我正在使用QueryDSL等Java工具在Web项目上进行一些实验,并且需要以类似QueryDSL Maven插件的方式为我的JPA模型类生成querydsl类。

提前致谢。

最佳答案

您可以手动运行注释处理器(请参见下面的command)或实现类似于以下内容的SBT任务:

lazy val processAnnotations = taskKey[Unit]("Process annotations")

processAnnotations := {
  val log = streams.value.log

  log.info("Processing annotations ...")

  val classpath = ((products in Compile).value ++ ((dependencyClasspath in Compile).value.files)) mkString ":"
  val destinationDirectory = (classDirectory in Compile).value
  val processor = "com.package.PluginProcessor"
  val classesToProcess = Seq("com.package.Class1", "com.package.Class2") mkString " "

  val command = s"javac -cp $classpath -proc:only -processor $processor -XprintRounds -d $destinationDirectory $classesToProcess"

  failIfNonZeroExitStatus(command, "Failed to process annotations.", log)

  log.info("Done processing annotations.")
}


def failIfNonZeroExitStatus(command: String, message: => String, log: Logger) {
  val result = command !

  if (result != 0) {
    log.error(message)
    sys.error("Failed running command: " + command)
  }
}

packageBin in Compile <<= (packageBin in Compile) dependsOn (processAnnotations in Compile)


根据需要更新destinationDirectoryprocessorclassesToProcess

您还可以将“ -d”标志更改为“ -s”,具体取决于所使用的注释处理器的类型(请参见options for javac)。

07-24 13:51