1

There is a lot of different information of how to setup project so generating JPA metamodel works on build, but non of those works for me. I'm using Java 11, gradle 5.4.1 and STS (tried both STS 3.9.8 and 4.2.1). My gradle setup for annotations looks like this:

annotationProcessor(
            "javax.xml.bind:jaxb-api",
            "org.projectlombok:lombok",
            "org.hibernate:hibernate-jpamodelgen",
)
compileJava {
    def relativePath = "out/production/classes/generated"
    sourceSets.main.java {
        srcDir(relativePath)
    }
    def generatedSourceDir = project.file(relativePath)
    options.annotationProcessorGeneratedSourcesDirectory = generatedSourceDir
}

I've also done right click on the project > Properties > Java Compiler > Annotation Processing and selected:

  • Enable project specific settings
  • Enable annotation processing
  • Enable processing in editor
  • Generated source directors set to out/production/classes/generated

In > Factory Path I've selected:

  • Enable project specific setting
  • Added external JAR hibernate-jpamodelgen-5.3.7.Final.jar
  • Added external JAR javax.persistence-api-2.2.jar

Can anybody see that am I missing or doing wrong? Thank you.

Mario Rudman
  • 1,477
  • 2
  • 10
  • 15
  • 1
    Is it Eclipse-specific problem? (Do the files get generated if you run gradle manually?) I tried your setup, and it works perfectly for me (both command line and IntelliJ). There are 2 caveats: annotationProcessors come in the dependedncies section, this is not evident in your code. On top of that, due to recent improvements in Gradle 5.2 (and Intellij 2019.1) `compileJava` is now redundant) – Lesiak May 14 '19 at 11:09
  • @Lesiak Yes, files get generated if I run gradle manually. I guess that means it is Eclipse-specific problem. If anybody know how should STS be configured so that generating metamodel works, that would be great. – Mario Rudman May 14 '19 at 17:25

1 Answers1

2

I made it running using net.ltgt.apt-eclipse plugin. I followed the blog post: http://dplatz.de/blog/2018/gradle-apt.html

I am using Eclipse 2019-03 and Gradle 5.4.1

As there blog describes another annotation processor, I will descibe steps taken here

  1. Imported net.ltgt.apt-eclipse plugin. The build.gradle looks as follows:
plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
    id "net.ltgt.apt-eclipse" version "0.18"    
}

apply plugin: 'io.spring.dependency-management'

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

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    compileOnly 'org.projectlombok:lombok:1.16.18'
    runtimeOnly 'com.h2database:h2'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    annotationProcessor(
            "javax.xml.bind:jaxb-api",
            "org.projectlombok:lombok",
            "org.hibernate:hibernate-jpamodelgen"
    )

}
  1. Executed the followiing command from the command line:
gradle eclipseJdtApt eclipseFactorypath eclipseJdt
  1. Reimported gradle project. Cleaned project. Right-click the project and select Gradle / Refresh Gradle Project. Afterwards, Project / Clean

  2. Checked that JPA annotation processors are there in Properties / Java Compiler / Annotation Processing / Factory Path Note: I have following entries, in order:

    • jaxb-api-2.3.1.jar
    • lombok-1.18.6.jar
    • hibernate-jpamodelgen-5.3.9.Final.jar
    • javax.activation-api-1.2.0.jar -jboss-logging-3.3.2.Final.jar
  3. Run the build

Note that I removed custom directory for generated files, they are now generated to .apt_generated, but I am happy with that.

Hope that helps!

Lesiak
  • 12,048
  • 2
  • 17
  • 41