Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating annotation processors with Gradle

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:

  1. I should be able to write both annotation processors and the code that will be consuming them in the same project and Gradle should handle adding the processors to class path and invoking them with javac at approrpiate stage.
    OR
  2. If the above is not possible and I have to create two separate projects, then at least it should be possible to keep them in the same git repository. Gradle should handle the build seamlessly.
    OR
  3. If neither is possible and I have to create two separate git repositories, then at the very least, Gradle should handle the things mentioned in the linked blog post seamlessly without further manual intervention.

My git and Gradle skills are beginner level. I would appreciate any help with this task. Thank you.

like image 451
missingfaktor Avatar asked Mar 23 '13 07:03

missingfaktor


People also ask

What is Annotationprocessor in gradle?

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.

What is an annotation processor?

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.

What is kapt in gradle?

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.


2 Answers

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.

like image 61
erdi Avatar answered Sep 22 '22 22:09

erdi


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

like image 41
Michail Nikolaev Avatar answered Sep 20 '22 22:09

Michail Nikolaev