AnnotationProcessor Querydsl java.lang.NoClassDefFoundError


(François) #1

Hello,

I have a multiproject and some of my subprojects uses JPA over Querydsl. To do that, at compile time we need to generate the querydsl classes (Q) using APT

I tried with and without the gradle-apt-plugin. During compile time I got the following error

Caused by: java.lang.NoClassDefFoundError: javax/persistence/Entity
    at com.querydsl.apt.jpa.JPAAnnotationProcessor.createConfiguration(JPAAnnotationProcessor.java:37)
    at com.querydsl.apt.AbstractQuerydslProcessor.process(AbstractQuerydslProcessor.java:79)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    at com.sun.tools.javac.main.Main.compile(Main.java:523)

I have the following build.gradle for my subproject

project(':echanges:infrastructure:jpa-db-adapter') {
  apply plugin: 'jacoco'
  apply plugin: 'idea'
  apply plugin: 'net.ltgt.apt'
  apply from:   "$rootDir/gradle/org.gfc.integtest.gradle"

  tasks.withType(JavaCompile) {
      options.annotationProcessorGeneratedSourcesDirectory = file("$projectDir/src/generated3/java")
  }
  dependencies {

    annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa"

    compile project(':echanges:application-core:core')
    implementation project(':commons')

    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'javax.inject:javax.inject:1'
    compile "com.querydsl:querydsl-jpa:$querydslVersion"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"

    testCompile 'org.springframework.boot:spring-boot-starter-test'
    
    integrationTestCompile 'com.oracle:ojdbc6:11.2.0.3'
}

sourceSets {
    main {
        java {
            srcDirs = ["$projectDir/src/main/java", "$projectDir/src/generated/java"]
        }
    }
}

}

The JPA annotations are declared inside the hibernate-jpa-2.1-api. The spring data jpa pull this jar transitive

Thanks for your help


(Stefan Oehme) #2

Looks like the queryDsl processor depends on javax.persistence, but doesn’t declare a dependency on it. To work around, you’ll have to manually add it to the annotationProcessor configuration. Consider opening a bug for queryDsl. They should add a dependency on it so it automatically brings it in for processing.

Note that the processor path (used for loading annotation processors and compiler plugins) and the compile classpath (what your code is allowed to link against) are two separate and different things. Dependencies on one are not visible to the other.