简体   繁体   English

Gradle通过不同的注释处理器两次生成Querydsl元数据

[英]Gradle generates Querydsl metadata twice via different annotation processors

I have a gradle build script. 我有一个gradle构建脚本。 I want said script to generate QueryDSL-Metadata. 我想说脚本来生成QueryDSL-Metadata。 Those metadata should be generated under the build/generated-sources/metamodel Folder. 这些元数据应在build / generated-sources / metamodel文件夹下生成

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. 连同所需目标一起也在“默认” buld / classes / ...中生成,从而导致“重复类”错误。

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. 根据gradle跟踪,问题似乎在于混合中有两个AnnotatioProcessor。 First, the HibernateAnnotationProcessor. 首先,HibernateAnnotationProcessor。 Second, a JPAAnnotationProcessor, eventually generating the duplicate class. 其次,一个JPAAnnotationProcessor,最终生成重复的类。 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. 我什至清理了gradle缓存,以防万一。 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: 该线程的解决方案对我的作品,这个想法是勾标注处理进入的javac,HibernateAnnotationProcessor可以通过compilerArgs声明,大致是这样:

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. 但是我仍然想知道为什么第一种方法不起作用(运行两个注释处理器),所以任何想法仍然受到高度赞赏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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