Java 17 升级后未生成 QueryDSL Q 类

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

我正在将使用 Spring Boot 2.7.5 和 QueryDSL 5.0 的 Gradle 7.5 项目从 Java 8 升级到 Java 17。该项目在升级之前可以正常工作,但事后,它无法构建,因为最终的 CompileJava 执行记录了“无法代码中所有 Q 类引用都出现“查找符号”错误。

我认为这些类要么以不同的方式存储,要么没有生成。

此外,如果这有帮助,我将使用 VS Code 作为 IDE,但从标准 Windows 命令提示符运行所有 Gradle 命令。

我查看了 Stack Overflow 和其他网站以了解与此相关的问题。不幸的是,尽管这个问题在不同的情况下被问过,但他们的答案并不适合我的情况。以下是我查看过的一些地方,以防对其他人有所帮助:

这是我的 build.gradle。请注意,当我有“sourceCompatability = 1.8”并且尚未开始使用工具链符号甚至 Java 17 时,这是有效的。还要知道我不是从头开始创建此文件,而是正在编辑其他人开始的文件.

此外,“bootRun”任务中的“compileJava”命令失败了(“运行”应用程序的主要任务是“MyAppRun”。它依次执行“preCompile”、“generateJPA”、“generateQueryDSL”、和“bootRun”按该顺序)。我预计“generateQueryDSL”任务中需要进行一些更改。

我对 Gradle 和 QueryDSL 非常陌生,希望得到任何建议。

提前致谢!

plugins {
    id 'org.springframework.boot' version '2.7.5'
    // id 'net.ltgt.apt' version '0.18'
    id 'java'
}

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

group = 'com.mydomain'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

// sourceCompatibility = '17.0.6'

repositories {
    mavenLocal()
    mavenCentral()
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
}

configurations {
    hibernatetool
    querydslapt
}

configurations.implementation.canBeResolved = true

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java']
        }
    }

    generated {
        java {
            srcDir "$projectDir/generated/java"
        }
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-jersey'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.security:spring-security-config:5.6.8'
    implementation 'org.springframework.security:spring-security-core:5.6.8'
    implementation 'org.springframework.security:spring-security-oauth2-resource-server:5.6.8'
    implementation 'org.springframework.security:spring-security-oauth2-jose:5.6.8'
    runtimeOnly group: 'com.nimbusds', name: 'oauth2-oidc-sdk', version: '6.23'
    implementation 'org.hibernate:hibernate-core:5.6.12.Final'
    implementation 'org.hibernate:hibernate-tools:5.6.2.Final'
    implementation 'org.springframework.data:spring-data-rest-hal-explorer'
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.3.2'
    implementation group: 'org.json', name: 'json', version: '20200518'
    runtimeOnly 'org.postgresql:postgresql'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        exclude group: 'junit', module: 'junit'
    }

    hibernatetool group:'org.hibernate', name:'hibernate-core', version:'5.6.12.Final'
    hibernatetool group:'org.hibernate', name:'hibernate-tools', version:'5.6.2.Final'
    hibernatetool group:'org.hibernate', name:'hibernate-entitymanager', version:'5.6.12.Final'
    hibernatetool group:'org.postgresql', name:'postgresql'

    implementation("com.querydsl:querydsl-core:5.0.0")
    implementation("com.querydsl:querydsl-jpa:5.0.0")
    implementation('com.google.guava:guava:31.1-jre')
    implementation('com.google.code.findbugs:jsr305:3.0.2')
    annotationProcessor("com.querydsl:querydsl-apt:5.0.0:jpa")
    annotationProcessor('org.springframework.boot:spring-boot-starter-data-jpa')
    annotationProcessor('org.springframework.boot:spring-boot-starter-data-rest')
    annotationProcessor('javax.persistence:javax.persistence-api:2.2')
    annotationProcessor('javax.annotation:javax.annotation-api:1.3.2')
    implementation fileTree(include: ['*.jar'], dir: './libs')

    /* annotationProcessor     'com.querydsl:querydsl-apt:4.2.1: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.2.1'*/
    
}

springBoot {
    if (project.hasProperty('import')) {
        mainClass = 'com.mydomain.MyAppservices.ImportApplication'
    } else {
        mainClass = 'com.mydomain.MyAppservices.MyAppApplication'
    }
}

bootRun {
    if (project.hasProperty('import')) {
        args project.import
    }

    compileJava {
        options.compilerArgs << "-proc:none"
    }
}

task preCompile (type: JavaCompile) {
    source = sourceSets.main.java.srcDirs
    include 'com/mydomain/MyAppservices/config/MyAppReverseEngineeringStrategy.java'
    classpath = configurations.implementation
    destinationDirectory = file("build/classes/java/main")
}

task generateJPA  {
    group 'MyApp'
    description 'Generate JPA Entities'
    doLast{
        delete 'src/main/java/com/mydomain/MyAppservices/model', '*'
        delete fileTree('src/main/java/com/mydomain/MyAppservices/views') { include 'Q*' }
        ant {
            taskdef(name: 'hibernateTool',
                classname: 'org.hibernate.tool.ant.HibernateToolTask',
                classpath: configurations.hibernatetool.asPath)
            hibernateTool { 
                jdbcconfiguration(propertyFile: 'resources/hibernate.reveng.properties',
                                detectmanytomany: 'true',
                                reversestrategy: 'com.mydomain.MyAppservices.config.MyAppReverseEngineeringStrategy',
                                packagename: 'com.mydomain.MyAppservices.model',
                                revengfile: 'resources/hibernate.reveng.xml')
                hbm2java(destdir: 'src/main/java', ejb3: 'true', jdk5: 'true')
                classpath { 
                    pathElement(path: "build/classes/java/main")
                }   
            }
        }
    }
}

task generateQueryDSL (type: JavaCompile) {
    group 'MyApp'
    description "Generate QueryDSL classes"
    options.compilerArgs << "-s"
    options.compilerArgs << "src/main/java/"
    options.compilerArgs << "-proc:only"
    options.compilerArgs << '-processor'
    options.compilerArgs << 'com.querydsl.apt.jpa.JPAAnnotationProcessor'
    options.annotationProcessorPath = configurations.annotationProcessor
    source = sourceSets.main.java.srcDirs
    classpath = configurations.implementation + configurations.annotationProcessor
    destinationDirectory = file("build/classes/java/main")
}

generateJPA.dependsOn preCompile
generateQueryDSL.dependsOn generateJPA

task generate {
    group 'MyApp'
    description "Generate all JPAs and QueryDSL classes"
    dependsOn generateQueryDSL
}

task MyAppRun {
    dependsOn bootRun
    dependsOn generate
    tasks.findByName('bootRun').mustRunAfter 'generate'
}

task cleanGenerated {
    doLast{
        delete 'src/main/java/com/mydomain/MyAppservices/model', '*'
    }
}

java spring-boot gradle upgrade querydsl
1个回答
0
投票

为了回答我自己的问题,一位同事最终在

generateJPA
任务的末尾更改了一行从

hbm2java(destdir: 'src/main/java', ejb3: 'true', jdk5: 'true')

hbm2java(destdir: 'src/main/java', ejb3: 'true', jdk5: 'true', templatepath: 'resources/templates')

并确保我们有一个包含以下内容的 /resources/templates/pojo/Pojo.ftl 文件:

${pojo.getPackageDeclaration()}

<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/> {

<#if !pojo.isInterface()>
<#include "PojoFields.ftl"/>

<#include "PojoConstructors.ftl"/>
   
<#include "PojoPropertyAccessors.ftl"/>

<#include "PojoToString.ftl"/>

<#include "PojoEqualsHashcode.ftl"/>

<#else>
<#include "PojoInterfacePropertyAccessors.ftl"/>

</#if>
<#include "PojoExtraClassCode.ftl"/>

}
</#assign>

${pojo.generateImports()}
${classbody}

这为我们解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.