简体   繁体   中英

Java annotation not usable from Kotlin project jar

I am working on library that I am transferring from Java to Kotlin. Since the projects is still mixed with both languages, I use kotlin-maven-plugin and maven-compiler-plugin as described in https://kotlinlang.org/docs/reference/using-maven.html . Kotlin version is 1.2.10.

I need to create an annotation in the library, that is going to be used in other projects. The annotation is a standard Java one, and I am able to build my library.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableSomeBehavior {

}

When I import the library in another project (a Java project), I can see the annotation. The auto-completion in my IDE (IntelliJ) actually proposes me annotation.. but for some reason, is not able to import it : when I validate the auto-completion choice, then I am getting a compilation error "cannot resolve symbol 'EnableSomeBehavior' ".

This is quite puzzling.. I've rebuilt several times (in case the jar was corrupted), but it didn't change anything.

I suspect it's something that has to do with the way the Kotlin/Java library is compiled and/or packaged.. The Java annotation is there in the jar, I can see it, and to some extent, the IDE can also see it, since it's proposing it. But somehow, it's not able to use it. It's not an IDE issue, because even compile with Maven from command line fails with the same error.

Any idea of what the problem could be ?

Thanks

Initially, I found that it was working better if the annotation was a Kotlin one instead of a Java one :

@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
annotation class EnableSomeBehavior

After rebuilding my library, IntelliJ wouldn't show any error in the project using the annotation. However, compilation would still fail !

I ended up finding that it's not a Kotlin issue at all but a Spring Boot packaging issue : Using Kotlin classes from jar built with kotlin-maven-plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM