8

So I'm trying to get the Hibernate Validator Annotation Processor working in a Kotlin project, to check my JSR 380 annotations, with not much luck.

Unfortunately the documentation does not mention how to set it up with Gradle, and obviously with Kotlin we have to use "Kapt" to enable java annotation processors.

Hibernate Validator Annotation Processor Documentation: http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-annotation-processor

Kapt Documentation: https://kotlinlang.org/docs/reference/kapt.html

I currently have the following config in my build.gradle file relating to the processor:

plugins {
    id "org.jetbrains.kotlin.kapt" version "1.3.11"
    ...
}

apply plugin: 'org.jetbrains.kotlin.kapt'
...

dependencies {
    implementation 'org.hibernate:hibernate-validator:6.0.14.Final'
    implementation 'org.glassfish:javax.el:3.0.1-b09'
    kapt 'org.hibernate:hibernate-validator-annotation-processor:6.0.14.Final'
    ...
}

kapt {
    arguments {
        arg('methodConstraintsSupported', 'false')
        arg('verbose', 'true')
    }
}

However whenever I build, I cannot see any output relating to the validator annotation processor and I do not get any build errors when deliberately applying an incorrect annotation (e.g. applying a @Min() annotation to a String field.

If someone could advise on how to get the processor working I would be eternally grateful! :)

Russell Briggs
  • 993
  • 8
  • 8

1 Answers1

1

I got this working in my build.gradle.kts like so (I'm using Kotlin Script as opposed to Groovy):

plugins {
    ...
    id("org.jetbrains.kotlin.kapt") version "1.3.72"
    ...
}

dependencies {
    ...
    kapt(
            group = "org.hibernate.validator",
            name = "hibernate-validator-annotation-processor",
            version = "6.0.2.Final"
    )
    ...
}

This correctly gave me errors when building, but only when I applied the validation annotation to the getter. When I was mistakenly applying it to just the constructor argument the validation did not work, and I saw no errors from the annotation processor. For example:

class Thing(
    @get:AssertTrue
    var name: String
)
Theozaurus
  • 955
  • 1
  • 8
  • 21
  • Even if it is hidden behind a lot of Gradle overhead, I do believe that this is the issue the OP experienced: You cannot put the annotation just on the property (as long as there is no explicit Kotlin support in Hibernate). – Michael Piefel Dec 21 '20 at 18:24