简体   繁体   中英

QueryDSL issue with gradle

I am using QueryDSL with Lombok and gradle. Q classes are getting generated, also IDEA can see them and there is no compiler error but when i try to execute gradle build i get:

error: cannot find symbol
import QRole;

So files are generated but gradle build like can't see them. All of solutions i found are not working and what ever i do i still get this error. Here is my gradle.build:

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'checkstyle'
    id 'org.sonarqube' version '3.3'
    id 'jacoco'
    id 'idea'
    id 'com.github.ben-manes.versions' version '0.42.0'
}
dependencies {
    // Annotation processors
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion
    testAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion

    // Mapstruct
    implementation group: 'org.mapstruct', name: 'mapstruct', version: mapstructVersion
    annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: mapstructVersion

    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

    annotationProcessor group: 'javax.annotation', name: 'javax.annotation-api', version: javaxAnnotationVersion
    annotationProcessor group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.2-api', version: jpaVersion
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion
    compileOnly group: 'org.projectlombok', name: 'lombok', version: lombokVersion
    testAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion

    annotationProcessor group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.2-api', version: jpaVersion

    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

    implementation group: 'com.querydsl', name: 'querydsl-apt', version: querydslVersion
    implementation group: 'com.querydsl', name: 'querydsl-jpa', version: querydslVersion
    implementation group: 'com.querydsl', name: 'querydsl-sql', version: querydslVersion

    // Spring boot
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
    implementation group: 'org.springframework.retry', name: 'spring-retry', version: springRetry

    // Promotheus Monitoring (exposes /actuator/promotheus)
    runtimeOnly 'io.micrometer:micrometer-registry-prometheus'

    // Persistence
    runtimeOnly group: 'org.postgresql', name: 'postgresql', version: postgresVersion
    implementation group: 'org.flywaydb', name: 'flyway-core', version: flywayVersion
    implementation group: 'org.hibernate', name: 'hibernate-jcache', version: hibernateVersion

    implementation(group: 'org.hibernate', name: 'hibernate-spatial', version: hibernateVersion) {
        exclude group: 'org.postgresql', module: 'postgresql'
    }
    implementation group: 'com.vladmihalcea', name: 'hibernate-types-52', version: hibernateTypesVersion

    // Apache commons
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: apacheCommonsLangVersion
    implementation group: 'org.apache.commons', name: 'commons-collections4', version: apacheCommonsCollectionVersion

    // Swagger
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: swaggerVersion
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: swaggerVersion

    // Jackson Datatype Jts
    implementation group: 'com.bedatadriven', name: 'jackson-datatype-jts', version: jacksonDatatypeJTSVersion

    // JWT
    implementation group: 'io.jsonwebtoken', name: 'jjwt', version: jjwtVersion

    // Logging dependencies
    runtimeOnly group: 'net.logstash.logback', name: 'logstash-logback-encoder', version: logstashVersion

    // Tests
    testImplementation(group: 'org.springframework.boot', name: 'spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation group: 'org.springframework.security', name: 'spring-security-test'

    // Mockito
    testImplementation group: 'org.mockito', name: 'mockito-core', version: mockitoVersion
    testImplementation group: 'org.mockito', name: 'mockito-inline', version: mockitoVersion

    //JUnit
    testImplementation group: 'junit', name: 'junit', version: junitVersion

    //CSV
    implementation group: 'org.apache.commons', name: 'commons-csv', version: apacheCommonsCsvVersion

    // S3
    implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: awsSdkVersion

    // SES
    implementation group: 'com.amazonaws', name: 'aws-java-sdk-ses', version: awsSdkVersion

    // AWS Secrets Manager
    implementation group: 'com.amazonaws', name: 'aws-java-sdk-secretsmanager', version: awsSdkVersion

    // RDS IAM access control
    implementation group: 'io.magj', name: 'iam-jdbc-driver', version: '0.1.8'

    // Scheduling
    implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-spring', version: shedLockVersion
    implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-provider-jdbc-template', version: shedLockVersion
}

dependencies {
    compileOnly "com.querydsl:querydsl-jpa:${querydslVersion}"
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor(
            "com.querydsl:querydsl-apt:${querydslVersion}:jpa",
            "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJPAVersion}",
            "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}",
            "org.projectlombok:lombok"
    )
}

Working with Gradle, QueryDSL and Lombok together is a tricky job, but luckily there are few plugins which makes it easy. Here is the code that works.

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'idea'
    id 'io.franzbecker.gradle-lombok' version '5.0.0'
    id 'com.ewerk.gradle.plugins.querydsl' version '1.0.10'
}

querydsl {
    library = 'com.querydsl:querydsl-apt:5.0.0'
    jpa = true
}

configurations {
    querydsl.extendsFrom implementation, compileClasspath
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

project.afterEvaluate {
    project.tasks.compileQuerydsl.options.compilerArgs = [
            "-proc:only",
            "-processor", project.querydsl.processors() +
                    ',lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
    ]
}

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

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.querydsl:querydsl-jpa:5.0.0'
}

io.franzbecker.gradle-lombok and com.ewerk.gradle.plugins.querydsl are the required plugins to work all 3 correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM