Search code examples
gradlequerydsl

Gradle 5 QueryDsl Generating Duplicate Classes


I am using queryDsl to generate Q classes with Gradle. It used to work fine with Gradle 3.5, but on upgrading to Gradle 5.5.1, it is failing with duplicate class error.

My generateQueryDsl task works fine generating the classes under 'gensrc/' but on compileJava, the classes are generated again under 'build/generated/' which ends up giving duplicate class error.

dependencies { 

   api("org.springframework.boot:spring-boot-starter-data-jpa") {
        exclude group: "org.hibernate", module: "hibernate-entitymanager"
        exclude group: "org.hibernate", module: "hibernate-core"
        exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
    }
    api("com.zaxxer:HikariCP:${hikaricpVersion}")

    api("com.h2database:h2:1.4.193")
    api("mysql:mysql-connector-java")
    api("com.microsoft.sqlserver:sqljdbc42:6.0.8112")
    api("org.springframework.data:spring-data-jpa")
    api("org.springframework:spring-jdbc")
    api("org.springframework:spring-orm")
    api("org.eclipse.persistence:javax.persistence:${eclipseLinkPersistenceVersion}")
    api("org.eclipse.persistence:eclipselink:${eclipseLinkVersion}")
    api("org.eclipse.persistence:org.eclipse.persistence.jpa:${eclipseLinkVersion}")

    api("com.mysema.querydsl:querydsl-sql:${queryDslVersion}")
    api("com.mysema.querydsl:querydsl-jpa:${queryDslVersion}")
    api("com.mysema.querydsl:querydsl-apt:${queryDslVersion}")

               annotationProcessor('com.mysema.querydsl:querydsl-apt:3.7.4:jpa')
               annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa")

} 

task generateQueryDSL(type: JavaCompile, group: 'build) {

                source = sourceSets.main.java
                classpath = configurations.compileClasspath
                options.annotationProcessorPath = configurations.annotationProcessor
                destinationDir = file('gensrc/main/java')

        }

        compileJava {
                dependsOn generateQueryDSL
        }

error: duplicate class: com.persistence.domain.model.QOrganizationBasedModel



and likewise for all generated classes

Solution

  • When you use the annotationProcessor configuration, the default compileJava task adds the processor to the compiler, and it will generate classes in build/generated/sources/annotationProcessor/java/main.

    In your case, you also declare an additional JavaCompile task, which you give the same annotationProcessor configuration, which will then generate the same classes again.

    To solve this, I would simply delete generateQueryDSL task entirely as compileJava most likely does everything you need already. And if you like the generated sources in a different folder, you can do that through CompileOptions, but I would recommend having them under the build folder for most cases.