简体   繁体   English

将注释处理器与Gradle集成

[英]Integrating annotation processors with Gradle

I need to write some annotation processors. 我需要编写一些注释处理器。 I found this blog post which mentions how that can be done in a general setting and with Eclipse. 我发现这篇博客文章提到了如何在一般环境和Eclipse中完成。

However I am using IntelliJ IDEA and Gradle, and woud like it if there were a better (as in, less tedious) approach to do it. 但是我使用的是IntelliJ IDEA和Gradle,并且如果有更好的(如同,不那么繁琐)的方法,那么它就像它一样。 What I am looking for: 我在找什么:

  1. I should be able to write both annotation processors and the code that will be consuming them in the same project and Gradle should handle adding the processors to class path and invoking them with javac at approrpiate stage. 我应该能够编写注释处理器和将在同一个项目中使用它们的代码,Gradle应该处理将处理器添加到类路径并在approrpiate阶段使用javac调用它们。
    OR 要么
  2. If the above is not possible and I have to create two separate projects, then at least it should be possible to keep them in the same git repository. 如果以上是不可能的,我必须创建两个单独的项目,那么至少应该可以将它们保存在同一个git存储库中。 Gradle should handle the build seamlessly. Gradle应该无缝地处理构建。
    OR 要么
  3. If neither is possible and I have to create two separate git repositories, then at the very least, Gradle should handle the things mentioned in the linked blog post seamlessly without further manual intervention. 如果两者都不可能并且我必须创建两个单独的git存储库,那么至少Gradle应该无缝地处理链接博客文章中提到的内容而无需进一步的手动干预。

My git and Gradle skills are beginner level. 我的git和Gradle技能是初学者级别的。 I would appreciate any help with this task. 我很感激任何帮助这项任务。 Thank you. 谢谢。

Yes, it is possible to move processor to separated module and use it from another module (see querydslapt below). 是的,可以将处理器移动到分离的模块并从另一个模块使用它(参见下面的querydslapt )。

I will recomend you to implement your own AbstractProcessor 我建议你实现自己的AbstractProcessor

and use it like that: 并使用它:

dependencies {
    ....
    // put dependency to your module with processor inside
    querydslapt "com.mysema.querydsl:querydsl-apt:$querydslVersion" 
}

task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
    source = sourceSets.main.java // input source set
    classpath = configurations.compile + configurations.querydslapt // add processor module to classpath
    // specify javac arguments
    options.compilerArgs = [
            "-proc:only",
            "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor" // your processor here
    ]
    // specify output of generated code
    destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}

You can find the full example here 你可以在这里找到完整的例子

Another solution (in my opinion cleaner) could be to have two subprojects and then simply make the one that contains annotation processors a dependency of the main one. 另一个解决方案(在我看来更干净)可能是有两个子项目 ,然后简单地使包含注释处理器的子项目与主要项目相关。 So given two directories with your subprojects: core and annotation-processors in the root of your project, you would need to also have a settings.gradle file with the following: 因此,给定子项目的两个目录:项目根目录中的coreannotation-processors ,您还需要具有以下settings.gradle文件:

include 'core'
include 'annotation-processors'

And then in the gradle file for the core project: 然后在核心项目的gradle文件中:

dependencies {
    compile project(':annotation-processors')
}

That should do it and you won't have to deal with custom compile tasks and their classpaths. 应该这样做,你不必处理自定义编译任务及其类路径。

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

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