SBT: how to run annotation processing plugin

Does anyone know how to set up an SBT project to run an annotation processor (APT)? I do a lab in a web project using some Java tools, such as QueryDSL, and I need to generate querydsl classes for my JPA model classes, similar to how the QueryDSL Maven plugin does it.

Thanks in advance.

+4
source share
1 answer

You can manually start the annotation processor (see below command) or implement the SBT task similar to the following:

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)

Update destinationDirectory, processorand classesToProcessas needed.

"-d" "-s" (. javac).

+4

All Articles