3

I want to generate 'Q' classes using querydsl. The problem that I'm facing is described by errors:

> Task :compileJava FAILED 
Attempt to recreate a file for type com.my.dinner.rest.database.entity.QIngredientAmount
Attempt to recreate a file for type com.my.dinner.rest.database.entity.QUser
Attempt to recreate a file for type com.my.dinner.rest.database.entity.QRating
Attempt to recreate a file for type com.my.dinner.rest.database.entity.QIngredient
Attempt to recreate a file for type com.my.dinner.rest.database.entity.QRecipe
error: Attempt to recreate a file for type
com.my.dinner.rest.database.entity.QIngredientAmount
error: Attempt to recreate a file for type com.my.dinner.rest.database.entity.QUser
error: Attempt to recreate a file for type com.my.dinner.rest.database.entity.QRating
error: Attempt to recreate a file for type com.my.dinner.rest.database.entity.QIngredient
error: Attempt to recreate a file for type com.my.dinner.rest.database.entity.QRecipe
5 errors

Files are generated but build is failing, I suppose that classes are generated multiple times but I don't know how to fix it. I was using tips described here:

https://youtrack.jetbrains.com/issue/IDEA-212487?_ga=2.72682843.91471992.1592753352-1655482529.1589711163#focus=streamItem-27-3491579.0-0

but it does not work in my case.

I've tried every build.gradle configuration that I have found in Google, Github but nothing helped.

My build.gradle file:

plugins {
    id 'groovy'
    id 'java'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
    id 'io.franzbecker.gradle-lombok' version '1.8'
}

sourceCompatibility = '11'
group 'com.my.dinner.rest'
version '1.0-SNAPSHOT'

def springBootVersion = '2.3.1.RELEASE'
def springFrameworkVersion = '5.2.7.RELEASE'
def lombokVersion = '1.18.12'
def querydslVersion = '4.3.1'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '3.0.4', ext: 'pom'

    // Spring
    compile group: 'org.springframework', name: 'spring', version: springFrameworkVersion, ext: 'pom'
    compile group: 'org.springframework', name: 'spring-webmvc', version: springFrameworkVersion

    compile group: 'org.springframework.boot', name: 'spring-boot', version: springBootVersion
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging', version: springBootVersion
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion

    // Hibernate
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.17.Final'
    compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.1.5.Final'

    // Querydsl
    implementation group: 'com.querydsl', name: 'querydsl-jpa', version: querydslVersion
    implementation group: 'com.querydsl', name: 'querydsl-apt', version: querydslVersion
    implementation group: 'com.querydsl', name: 'querydsl-core', version: querydslVersion

    // Others
    compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
    compileOnly group: 'org.projectlombok', name: 'lombok', version: lombokVersion
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
    compile group: 'com.typesafe', name: 'config', version: '1.4.0'
    compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.8'

    annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBootVersion
    annotationProcessor group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
    annotationProcessor group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion
    annotationProcessor group: 'com.querydsl', name: 'querydsl-apt', version: querydslVersion
    annotationProcessor group: 'com.querydsl', 'name': 'querydsl-apt', version: querydslVersion, classifier: 'jpa'

    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootVersion
    testCompile group: 'junit', name: 'junit', version: '4.13'
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.200'
}

def querydslSrcDir = "$buildDir/generated"

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

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

querydsl {
    library = "com.querydsl:querydsl-apt"
    jpa = true
    querydslSourcesDir = querydslSrcDir
}

configurations {
    querydsl.extendsFrom compileClasspath

    compileOnly {
        extendsFrom annotationProcessor
    }
}

clean.doLast {
    file(querydslSrcDir).deleteDir()
}

Gradle version: 6.5

Intellij version: 2020.1

3
  • compileOnly { extendsFrom annotationProcessor } why? Commented Jun 22, 2020 at 21:12
  • Maybe the querydsl plugin is not compatible with the way newer versions of Gradle runs the processor itself when you have the libraries in the annotationProcessor configuration? Commented Jun 22, 2020 at 23:30
  • Check the location to where your generated classes are generated. I suspect the error may happen when there is a mess with output paths for generated and normal classes compilation. E.g. querydsl.extendsFrom compileClasspath - not sure but it may change the output paths. Gradle uses separate output directory for generated classes and you may change it with your configuration.
    – Andrey
    Commented Jun 23, 2020 at 7:46

1 Answer 1

4

So i solved it randomly changing build.gradle file.

I deleted unnecessary plugins and a whole section below dependecies and it generated all classes in:

build/generated/annotationProcessor/java/main

My updated build.gradle file:

plugins {
    id 'groovy'
    id 'java'
}

sourceCompatibility = '11'
group 'com.my.dinner.rest'
version '1.0-SNAPSHOT'

def springBootVersion = '2.3.1.RELEASE'
def springFrameworkVersion = '5.2.7.RELEASE'
def lombokVersion = '1.18.12'
def querydslVersion = '4.3.1'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '3.0.4', ext: 'pom'

    // Spring
    compile group: 'org.springframework', name: 'spring', version: springFrameworkVersion, ext: 'pom'
    compile group: 'org.springframework', name: 'spring-webmvc', version: springFrameworkVersion

    compile group: 'org.springframework.boot', name: 'spring-boot', version: springBootVersion
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBootVersion
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging', version: springBootVersion
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion

    // Hibernate
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.17.Final'
    compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.1.5.Final'

    // Querydsl
    implementation group: 'com.querydsl', name: 'querydsl-jpa', version: querydslVersion
    implementation group: 'com.querydsl', name: 'querydsl-apt', version: querydslVersion
    implementation group: 'com.querydsl', name: 'querydsl-core', version: querydslVersion

    // Others
    compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
    compileOnly group: 'org.projectlombok', name: 'lombok', version: lombokVersion
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
    compile group: 'com.typesafe', name: 'config', version: '1.4.0'
    compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.8'

    annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBootVersion
    annotationProcessor group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
    annotationProcessor group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion
    annotationProcessor group: 'com.querydsl', name: 'querydsl-apt', version: querydslVersion
    annotationProcessor group: 'com.querydsl', 'name': 'querydsl-apt', version: querydslVersion, classifier: 'jpa'

    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootVersion
    testCompile group: 'junit', name: 'junit', version: '4.13'
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.200'
}

Versions:

  • Java: 11.0.7
  • Gradle: 6.5
  • Intellij: 2020.1.1
  • Ubuntu: 20.04 LTS

So I think that all questions about this problem that I have looked before were bound with older gradle versions, and there were many workarounds that don't need to be applied in my version of gradle.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.