Reputation: 92046
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.
Upvotes: 29
Views: 20511
Reputation: 3775
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
Upvotes: 9
Reputation: 6954
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.
Upvotes: 9