AnnotationProcessor和依赖项

问题描述 投票:3回答:1

我正在使用gradle / querydsl和JPA 2.1。

我想使用APT(QEntities)生成querydsl元数据。

要做到这一点,我正在使用gradle-apt-plugin和gradle 4.7

在我的项目中,我使用以下命令配置了compileJava选项:

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

在我的依赖项中,我添加了

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

spring starter添加了org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final jar,其中包含javax.persistence.Entity类到compileClasspath。

启动compileJava任务时出现错误:caused by: java.lang.NoClassDefFoundError: javax/persistence/Entity at com.querydsl.apt.jpa.JPAAnnotationProcessor.createConfiguration(JPAAnnotationProcessor.java:37)

不明白为什么注释处理器无法加载此类。

gradle querydsl apt
1个回答
6
投票

如果有人正在搜索。这是我的完整解决方案(根据您的回复)。重要的部分是net.ltgt.apt*插件,用于在eclipse中激活代码生成,以及最后三个querydsl依赖项。

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'
}
© www.soinside.com 2019 - 2024. All rights reserved.