简体   繁体   English

如何在 gradle 中与构建任务分开执行注释处理

[英]How execute annotation processing in gradle separatly from build task

I want to run QueryDsl Q-type generation in a separate task.我想在单独的任务中运行 QueryDsl Q 类型生成。 I want that Q-type classes not to be created in a regular compileJava task, but the compiler sees the AnnotationProcessor in the classpath and creates them itself.我希望不在常规 compileJava 任务中创建 Q 类型类,但编译器会在类路径中看到 AnnotationProcessor 并自行创建它们。 I tried nulling the annotationProcessorPath, but then I couldn't restore its configuration in a separate task.我尝试将 annotationProcessorPath 清空,但随后无法在单独的任务中恢复其配置。 May be is it possible to exclude somehow dependencies of subtasks from the classpath?是否有可能以某种方式从类路径中排除子任务的依赖关系?

compileJava {
    options.annotationProcessorPath = null
}

tasks.register('generateQTypes'){
    group 'build'
    description 'Generate Q-Type classes with QueryDsl library'
    dependencies {
        annotationProcessor(
                'com.querydsl:querydsl-apt:4.1.4:jpa',
                'javax.persistence:javax.persistence-api:2.2',
                'javax.annotation:javax.annotation-api:1.3.1')
    }
    compileJava {
        options.annotationProcessorPath = classpath
    }
}

What is the best way to solve this problem?Thanks in advance!解决此问题的最佳方法是什么?提前致谢!

I made the following solution我做了以下解决方案

class QTypeGenerator extends DefaultTask {
    @TaskAction
    addDependencies() {
        project.dependencies {
            annotationProcessor(
                    'com.querydsl:querydsl-apt:4.1.4:jpa',
                    'javax.persistence:javax.persistence-api:2.2',
                    'javax.annotation:javax.annotation-api:1.3.1')
        }
    }
}

tasks.register("generateQTypeClasses", QTypeGenerator) {
    group('build')
    description('Generate Q-type classes by queryDsl in build directory with default path')
    finalizedBy('compileJava')
    doLast {
        println("Q-types classes will gererated and stored in ${compileJava.options.annotationProcessorGeneratedSourcesDirectory}")
    }
}

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

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