简体   繁体   中英

Gradle generates Querydsl metadata twice via different annotation processors

I have a gradle build script. I want said script to generate QueryDSL-Metadata. Those metadata should be generated under the build/generated-sources/metamodel Folder.

The problem I am facing at the moment is that the metamodel is not only being generated once, but twice. Along with the desired target it is also being generated in the "default" buld/classes/... resulting in a "duplicate class"-error.

sourceSets {
    generated.java.srcDirs=['build/generated-sources/metamodel']
    main {
        java { srcDir 'src/main/java' }
    }
    test {
        java { srcDir 'src/main/test' }
    }
}

configurations { querydslapt }

dependencies {
    compile 'org.hibernate:hibernate-entitymanager:5.2.3.Final',
            'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final-redhat-1',
            'com.querydsl:querydsl-jpa:4.1.3',
            // ... others, non-hibernate/querydsl ...
    querydslapt 'com.querydsl:querydsl-apt:4.1.3'
}

task generateSources(type: JavaCompile, group: 'build', description:'Generates the QueryDSL query types') {
    source = sourceSets.main.java
    classpath = configurations.compile + configurations.querydslapt
    options.compilerArgs = ['-proc:only',
                            '-processor', 'com.querydsl.apt.hibernate.HibernateAnnotationProcessor']
    destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}

compileJava {
    dependsOn generateSources
    source generateSources.destinationDir
}

According to the gradle trace, the Problem appears to be that there are two AnnotatioProcessors in the mix. First, the HibernateAnnotationProcessor. Second, a JPAAnnotationProcessor, eventually generating the duplicate class. And I can't figure out why, the build script looks ok-ish. I know, it might be guesswork, but I am grateful for any suggestions. I even cleaned my gradle-cache, just in case. It might not even be a pure build-script related issue, but the behavior persists even if I run the script via console.

Gist, basically exactly what I "should" need

(older) Post regarding this issue

This thread's solution works for me, the idea is to hook the Annotation Processor into the javac , the HibernateAnnotationProcessor can be declared via compilerArgs , roughly like:

dependencies {
    compile 'org.hibernate:hibernate-entitymanager:5.2.3.Final',
            'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final-redhat-1',          
            'com.querydsl:querydsl-jpa:4.1.4',
            'com.querydsl:querydsl-apt:4.1.4',
            // other
}

ext {
    generatedSourcesDir = file("build/generated-sources/metamodel")
}

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir generatedSourcesDir
        }
    }
    test {
        java { srcDir 'src/main/test' }
    }
}

compileJava {
    doFirst {
        generatedSourcesDir.mkdirs()
    }
    options.compilerArgs += ['-s', generatedSourcesDir,
                             '-processor', 'com.querydsl.apt.hibernate.HibernateAnnotationProcessor']
}

But I still wonder why the first approach does not work (runs two annotation processors), so any idea is still highly appreciated.

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