Thursday, January 6, 2022

[SOLVED] AnnotationProcessor and dependencies

Issue

I'm using gradle / querydsl and JPA 2.1.

I would like to generate the querydsl metadata using APT (QEntities).

To do that I'm using the gradle-apt-plugin and gradle 4.7

In my project I configured the compileJava option using :

compileJava { options.annotationProcessorGeneratedSourcesDirectory = file("$projectDir/src/generated2/java") }

In my dependencies I added

compile 'org.springframework.boot:spring-boot-starter-data-jpa'" annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa"

The spring starter adds the org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final jar that contains javax.persistence.Entity class to the compileClasspath.

When launching the compileJava task I got the error : caused by: java.lang.NoClassDefFoundError: javax/persistence/Entity at com.querydsl.apt.jpa.JPAAnnotationProcessor.createConfiguration(JPAAnnotationProcessor.java:37)

Don't understand why the annotation processor cannot load this class.


Solution

In case somebody else is searching. This is my full solution (based on your response). The important parts are the net.ltgt.apt* plugins to activate the code generation also in eclipse, and the last three querydsl dependencies.

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


plugins { // activates the automatic code generation
    id 'net.ltgt.apt' version '0.18'
    id 'net.ltgt.apt-eclipse' version '0.18' 
}


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

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

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}


dependencies {
    runtimeOnly             'com.h2database:h2'
    implementation          'org.springframework.boot:spring-boot-starter-actuator'
    implementation          'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation          'org.springframework.boot:spring-boot-starter-web'
    providedRuntime         'org.springframework.boot:spring-boot-starter-tomcat'


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

    // querydsl
    annotationProcessor     'com.querydsl:querydsl-apt:4.1.3:jpa'
    annotationProcessor     'org.springframework.boot:spring-boot-starter-data-jpa' // needed because the query dsl annotation processor doesn't recognize javax.persistence.Entity
    compile                 'com.querydsl:querydsl-jpa:4.1.3'
}


Answered By - atavio