FKorni
FKorni

Reputation: 445

How to generate JPA Metamodel with Gradle 5.x

I am currently trying to upgrade from gradle 4.8.1 to 5.1.1 but fail with generating the hibernate metamodel for our code.

The problem is that gradle 5 ignores the annotation processor passed with the compile classpath, but all plugins I found are using this (i.e "-proc:only").

I tried to specify the annotation processor explicitly as pointed out by gradle (https://docs.gradle.org/4.6/release-notes.html#convenient-declaration-of-annotation-processor-dependencies) annotationProcessor 'org.hibernate:hibernate-jpamodelgen'

But this does not help and I still get the following error:

warning: Annotation processing without compilation requested but no processors were found.

Maybe also the plugins need to be updated, but as I said all plugins which I found are passing the annotation processor with the classpath. We are currently using this one: https://github.com/Catalysts/cat-gradle-plugins/tree/master/cat-gradle-hibernate-plugin

Upvotes: 31

Views: 20535

Answers (3)

user2583264
user2583264

Reputation: 21

Using Gradle 8.3 and IntelliJ IDEA 2023.1.2, after adding

dependencies {
    annotationProcessor("org.hibernate.orm:hibernate-jpamodelgen:6.3.1.Final")
}

to build.gradle.kts, running ./gradlew compileJava and refreshing Idea project, Idea automatically picked up the generated sources (located in build/generated/sources/annotationProcessor/java).

Upvotes: 2

Aurimas Niekis
Aurimas Niekis

Reputation: 43

When working with metamodels in your project, you might face issues such as error: package jakarta.persistence.metamodel does not exist errors during the build process. The original accepted solution provided uses code that is now deprecated in newer versions of Gradle. Additionally, it introduces a new generated compile target, which can cause further complications down the road.

My proposed alternative solution aims to address these concerns by utilizing updated Gradle features and avoiding potential issues.

First, we need to include the Hibernate JPA Model Generator (the org.hibernate:hibernate-jpamodelgen artifact) in the annotationProcessor configuration. This configuration instructs the Java compiler to use the Hibernate JPA Model Generator during the annotation processing phase, generating JPA metamodel classes from your entities at compile-time.

dependencies {
  annotationProcessor('org.hibernate.orm:hibernate-jpamodelgen:<version>')
}

Remember to replace <version> with the desired version number.

Second, we need to configure where Gradle should place the generated source files, ensuring the IDE and the build system correctly recognize and compile these files. The following Gradle configuration achieves that by adapting the annotationProcessorGeneratedSourcesDirectory option for each source set, storing the generated sources in a consistent and organization-friendly manner:

sourceSets.configureEach { sourceSet ->
  tasks.named(sourceSet.compileJavaTaskName).configure {
    options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/annotationProcessor/java/$sourceSet.name")
  }
}

By using configureEach, we ensure that this configuration is applied to all source sets in the project. For each source set (e.g., main, test), the generated source files will be organized into dedicated folders, respecting the project hierarchy.

In summary, this alternative solution avoids deprecated code and minimizes potential issues by using newer Gradle features. Adding this configuration will enable the generated JPA metamodel classes to be properly recognized, compiled, and used in the project without any issues.

Upvotes: 1

M. Schett
M. Schett

Reputation: 844

you can just remove the plugin for the jpa modelgen and just use

annotationProcessor('org.hibernate:hibernate-jpamodelgen:<version>')

Addtionally i use those settings to configure where the generated code should live.

tasks.withType(JavaCompile) {
  options.annotationProcessorGeneratedSourcesDirectory = file("src/generated/java")
}


sourceSets {
    generated {
        java {
            srcDirs = ['src/generated/java']
        }
    }
}

Upvotes: 72

Related Questions