I need to write some annotation processors. I found this blog post which mentions how that can be done in a general setting and with Eclipse.
However I am using IntelliJ IDEA and Gradle, and woud like it if there were a better (as in, less tedious) approach to do it. What I am looking for:
My git and Gradle skills are beginner level. I would appreciate any help with this task. Thank you.
An annotation processor is a class which extends AbstractProcessor (in the package javax. annotation. processing ) providing the functionality needed to generate whatever it needs to generate, such as classes in the case of mapstruct.
Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.
1. Kapt is the Kotlin Annotation Processing Tool, and it's in pretty good shape these days. If you want to be able to reference generated code from Kotlin, you need to use kapt. To do that, simply include the plugin in your build.gradle file with the line : apply plugin: 'kotlin-kapt' 2.
Another solution (in my opinion cleaner) could be to have two subprojects and then simply make the one that contains annotation processors a dependency of the main one. So given two directories with your subprojects: core
and annotation-processors
in the root of your project, you would need to also have a settings.gradle
file with the following:
include 'core' include 'annotation-processors'
And then in the gradle file for the core project:
dependencies { compile project(':annotation-processors') }
That should do it and you won't have to deal with custom compile tasks and their classpaths.
Yes, it is possible to move processor to separated module and use it from another module (see querydslapt
below).
I will recomend you to implement your own AbstractProcessor
and use it like that:
dependencies { .... // put dependency to your module with processor inside querydslapt "com.mysema.querydsl:querydsl-apt:$querydslVersion" } task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') { source = sourceSets.main.java // input source set classpath = configurations.compile + configurations.querydslapt // add processor module to classpath // specify javac arguments options.compilerArgs = [ "-proc:only", "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor" // your processor here ] // specify output of generated code destinationDir = sourceSets.generated.java.srcDirs.iterator().next() }
You can find the full example here
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