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.
1 Answer. Show activity on this post. Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable Annotation Processing.
You can use Pretty Clean to clean the all of dev tools caches including SBT. PrettyClean also cleans the SBT project's target folder.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With