Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hibernate Validator Annotation Processor with Kotlin (and Gradle)

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! :)

like image 747
Russell Briggs Avatar asked Jan 11 '19 21:01

Russell Briggs


Video Answer


1 Answers

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
)
like image 135
Theozaurus Avatar answered Sep 20 '22 10:09

Theozaurus