Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT: How to run an annotation processing plugin

Does anyone knows how to configure a SBT project to run an annotation processor (APT)? I'm doing some lab on a web project, using some Java tools like QueryDSL, and i need to generate querydsl classes for my JPA model classes, in a similar way QueryDSL Maven plugin does.

Thanks in advance.

like image 203
accbel Avatar asked Sep 02 '14 12:09

accbel


People also ask

How do I run an annotation processor?

1 Answer. Show activity on this post. Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable Annotation Processing.

How do I clear my sbt cache?

You can use Pretty Clean to clean the all of dev tools caches including SBT. PrettyClean also cleans the SBT project's target folder.


1 Answers

You could manually run the annotation processor (see command below) or implement an 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, processor, and classesToProcess as necessary.

You might also change the "-d" flag to "-s" depending on the type of annotation processor you have (see options for javac).

like image 155
Johan Boberg Avatar answered Oct 20 '22 02:10

Johan Boberg