# Maven Java annotation processor docs or example

**URL:** https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367
**Category:** Support
**Created:** [December 18, 2015, 7:59pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367 "2015-12-18T19:59:43Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)
#### Post date: [December 18, 2015, 7:59pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/1 "2015-12-18T19:59:43Z")

</div>

I found an example with gradle and kapt … but I’m looking for a maven example or documentation.

---

<div class="post-metadata">

### Author: ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)
#### Post date: [December 22, 2015, 2:30am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/2 "2015-12-22T02:30:46Z")

</div>

Looking at kotlin-annotation-processing 1.0.0-beta-3595 it seems gradle specific. Looking at kotlin-maven-plugin … there is not similar support for annotation processing in maven.

Hmmm.

---

<div class="post-metadata">

### Author: ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)
#### Post date: [December 25, 2015, 11:31am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/3 "2015-12-25T11:31:54Z")

</div>

We don’t support `kapt` for Maven yet. What is your use case?

---

<div class="post-metadata">

### Author: ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)
#### Post date: [January 4, 2016, 11:54pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/4 "2016-01-04T23:54:11Z")

</div>

My use case is using Kotlin to write JPA entity beans. The annotation processor generates “query beans” which are used to create type safe Ebean ORM queries ( [http://ebean-orm.github.io/docs/query/typesafe#examples](http://ebean-orm.github.io/docs/query/typesafe#examples) ).

What the annotation processor does is read the properties/fields of the (JPA entity) bean (including properties/fields from the inheritance hierarchy) and generate source for the “query beans”. This is a bit like JPA meta model generation.

For the maven/Java case (entity beans written in Java) I can use a standard java annotation processor which is great (automated).

For the maven/Kotlin case (entity beans written in Kotlin) right now I can workaround it by having a manual step … I run a program that reads the compiled bytecode using ASM (reads the fields/properties of the kotlin entity beans) and generates the “query beans” java source.

Ideally I could register a kapt like processor that automates the above manual step so that the query beans are generated automatically after a Kotlin compile. It gets told which kotlin classes were compiled (with @Entity or @MappedSuperclass annotations) and has the ability to reflect/read the properties/fields including inherited properties/fields and from this generate java source much like a normal javac annotation processor (and this all occurs prior to maven running the javac compile so the kotlin-maven-plugin is run in process-sources phase).

Note that I have an example project in github but I think I’ll move it (currently it is at [avaje-ebeanorm-examples/e-kotlin-maven at master · ebean-orm-examples/avaje-ebeanorm-examples · GitHub](https://github.com/ebean-orm/avaje-ebeanorm-examples/tree/master/e-kotlin-maven)).

Cheers, Rob.

---

<div class="post-metadata">

### Author: ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)
#### Post date: [January 18, 2016, 12:50pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/5 "2016-01-18T12:50:39Z")

</div>

Thanks. We’ll look into it at some point, but can’t say exactly when, sorry

---

<div class="post-metadata">

### Author: ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)
#### Post date: [January 18, 2016, 6:08pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/6 "2016-01-18T18:08:04Z")

</div>

No problem. I saw the Kotlin compiler plugin API so my next option is to look into that and see if that provides an approach. I have not had the time to look into that yet but that is my plan and hopefully I can do that in the next 2 weeks (and report back how I go with that).

Cheers, Rob.

---

<div class="post-metadata">

### Author: ![miere](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miere/32/1758_2.png) [@miere](https://discuss.kotlinlang.org/u/miere)
#### Post date: [January 27, 2016, 12:40am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/7 "2016-01-27T00:40:49Z")

</div>

It would be awesome to use all those APT based libraries we already have at our current java stack, but using the amazing Kotlin syntax.

abreslav

Do you think it would be too hard to compatibilize with default Java APT API? I mean, make kotlin-maven-plugin to call default APT to generate source codes?

---

<div class="post-metadata">

### Author: ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)
#### Post date: [January 28, 2016, 12:27pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/8 "2016-01-28T12:27:56Z")

</div>

> [@miere](#):
>
> Do you think it would be too hard to compatibilize with default Java APT API? I mean, make kotlin-maven-plugin to call default APT to generate source codes?

Well, we can probably do the same as we do for Gradle: [https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/kapt/kaptStubGeneratorUtils.kt](https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/kapt/kaptStubGeneratorUtils.kt)

In essence, what it does is feeds Kotlin class-files to `javac` as if they were sources.

It’s substantial work, though not something huge.

---

<div class="post-metadata">

### Author: ![miere](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miere/32/1758_2.png) [@miere](https://discuss.kotlinlang.org/u/miere)
#### Post date: [January 28, 2016, 12:52pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/9 "2016-01-28T12:52:54Z")

</div>

In practice it means that we can create a new goal at the kotlin-maven-plugin to generate stubs in order to `javac` apply the APT as it does with ordinary java classes, right?

About these generated stub classes. Does gradle bundle it within the output jar? Once the Kotlin compiler generate a fully compatible `.class` file, it may led to class duplication issues, right?

I’m just asking to understand _exactly_ what the Gradle task does, so I can reproduce a similar a approach in a maven plugin.

---

<div class="post-metadata">

### Author: ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)
#### Post date: [January 28, 2016, 6:14pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/10 "2016-01-28T18:14:24Z")

</div>

I think these two blog posts may help:

- [http://blog.jetbrains.com/kotlin/2015/05/kapt-annotation-processing-for-kotlin/](http://blog.jetbrains.com/kotlin/2015/05/kapt-annotation-processing-for-kotlin/)
- [Better Annotation Processing: Supporting Stubs in kapt | The Kotlin Blog](http://blog.jetbrains.com/kotlin/2015/06/better-annotation-processing-supporting-stubs-in-kapt/)

And the best way to understand what’s done in Gradle is read the code, IMO 🙂 But feel free to ask more questions, we’ll be happy to explain the details

---

<div class="post-metadata">

### Author: ![miere](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miere/32/1758_2.png) [@miere](https://discuss.kotlinlang.org/u/miere)
#### Post date: [February 10, 2016, 9:19am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/11 "2016-02-10T09:19:36Z")

</div>

Hi @abreslav

I did investigate the Gradle code you point me out. I confess it took a while to understand the solution. Indeed, it was quite a simple solution, generating the [APT stubs](https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/kapt/kaptStubGeneratorUtils.kt) at the compile time.

I was thinking, wouldn’t it be easier if you manually call the `javax.tools.JavaCompiler` after generate the `.class` from the `.kt` source? I just took a look at the `.class` generated byte code and it kept the all annotations and method signatures needed by any ordinary Annotation Processor like Dagger.

I did [this](https://github.com/Skullabs/apt-runner-maven-plugin) piece of code as a proof of concept. Basically, it used [Fernflower](https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler/engine) to read the `.class` and let the `JavaCompiler` did its job. [Here](https://github.com/Skullabs/kikaha-kotlin/blob/master/pom.xml), you can find a maven project that uses this plugin. One of its dependencies exposes Java methods as RESTFul [endpoints](https://github.com/Skullabs/kikaha-kotlin/blob/master/source/samples/UserResource.kt#L14) at compile time.

If you think it make sense, it could apply the same approach at the [kotlin-maven-plugin](https://github.com/JetBrains/kotlin/tree/master/libraries/tools/kotlin-maven-plugin). Maybe, as a secondary goal.

I would be happy to hear a honest your feedback about it. If you have a better approach, please let me know, okay?

Best regards

---

<div class="post-metadata">

### Author: ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)
#### Post date: [February 25, 2016, 7:43am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/12 "2016-02-25T07:43:21Z")

</div>

Hi,

Sorry for the delay, we’ll look into it soon!

---

<div class="post-metadata">

### Author: ![yanex](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yanex/32/1489_2.png) [@yanex](https://discuss.kotlinlang.org/u/yanex)
#### Post date: [March 16, 2016, 1:18pm UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/13 "2016-03-16T13:18:43Z")

</div>

If I am right, you are saying about reading annotations from the class files generated by the Kotlin compiler.  
Actually, this is the way kapt works now. The APT wrappers generated in `kaptStubGeneratorUtils.kt` is just the small part of it.  
Annotation Processing API can read declarations from the class files directly, and there’s no need in Java decompiler. Furthermore, even if the decompiler (or any other stub generator) **always** produce the correct Java code, you would need to somehow exclude such source-stub files from the actual compilation stage, or remove the generated class files after the javac finishes. And, of course, generating such stubs is an extra work which affects build time.

---

<div class="post-metadata">

### Author: ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)
#### Post date: [October 12, 2016, 8:05am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/14 "2016-10-12T08:05:58Z")

</div>

> Annotation Processing API can read declarations from the class files directly, and there’s no need in Java decompiler.

How can that be done? Is there some mechanism / library to fire annotation processors from reading .class files? If so that would be exactly what I’m after.

Thanks, Rob.

---

<div class="post-metadata">

### Author: ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)
#### Post date: [October 21, 2016, 4:24am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/15 "2016-10-21T04:24:12Z")

</div>

Hi, Is there any update / plan for maven APT processing?

Thanks, Rob.

---

<div class="post-metadata">

### Author: ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)
#### Post date: [October 24, 2016, 7:15am UTC](https://discuss.kotlinlang.org/t/maven-java-annotation-processor-docs-or-example/1367/16 "2016-10-24T07:15:34Z")

</div>

Please feel free to upvote [https://youtrack.jetbrains.com/issue/KT-14478](https://youtrack.jetbrains.com/issue/KT-14478)
