3

I want add JPA metamodel to my project - Spring boot + gradle

I find a lot of examles how can i do it but all with Maven. Also I find this site: https://plugins.gradle.org/search?term=metamodel

and try first three plugins. With each plugins I get errors: error: cannot find symbol in classes marked lombok @Builder annotation and some classes is not entity. It is example some plugin:

    buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.github.iboyko.gradle.plugins:jpamodelgen-plugin:1.0.1"
    }
 }

    dependencies {
        compile ('org.hibernate:hibernate-jpamodelgen')
    }

1) Which plugin or method is the most official (correct) to create JPA metamodel in Spring boot + spring-data-jpa + gradle ?

2) How can I specify only the package with entities and not scan another classes?

3) how to make friends with it with lombok?

EDIT

I add this code to gradle file:

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

and it generate classes_ fine. After that I mark

generated/sources/java 

folder ass root of generated classes(rigth clik to this folder and mark as)

enter image description here

After that I try import generated classes in my repository and IDE show this:

enter image description here

For each module I have 2 module - my_module and my_module_main(I don't understand why) and all classes generate in my_module but all code in my_module_main. If I add this dependency - I have this:

enter image description here

and in generated class I have this: enter image description here

ip696
  • 6,574
  • 12
  • 65
  • 128

2 Answers2

9

I was struggling through the same issue. Finally I got it working by adding both dependencies in both compileOnly and annotationProcessor forms. Don't ask me why, but it somehow works.

compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
compileOnly 'org.hibernate:hibernate-jpamodelgen'
annotationProcessor('org.hibernate:hibernate-jpamodelgen')
Jan Cizmar
  • 372
  • 2
  • 11
1

Did you use Spring Initalizr to generate the project?

This is what you get from Initializr (expect the jpamodelgen that I added by myself):

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    runtime('com.h2database:h2')
    compileOnly('org.projectlombok:lombok')
    compileOnly ('org.hibernate:hibernate-jpamodelgen')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

// Add source set to define where the generated source code should go to
sourceSets.configureEach { sourceSet ->
    tasks.named(sourceSet.compileJavaTaskName).configure {
        options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
    }
}

You can find a demo project on GitHub:

https://github.com/simasch/demo-gradle-jpa

In IntelliJ you can right click on generated/sources/java and choose

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82