java 将注解处理器与 Gradle 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15584472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:09:18  来源:igfitidea点击:

Integrating annotation processors with Gradle

javaintellij-ideaannotationsgradle

提问by missingfaktor

I need to write some annotation processors. I found this blog postwhich 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. What I am looking for:

但是,我正在使用 IntelliJ IDEA 和 Gradle,如果有更好的(例如,不那么乏味的)方法来做到这一点,我会喜欢它。我在找什么:

  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.
    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. Gradle should handle the build seamlessly.
    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.
  1. 我应该能够在同一个项目中编写注释处理器和使用它们的代码,Gradle 应该处理将处理器添加到类路径并在适当的阶段用 javac 调用它们。
    或者
  2. 如果上述方法不可行并且我必须创建两个单独的项目,那么至少应该可以将它们保存在同一个 git 存储库中。Gradle 应该无缝地处理构建。
    或者
  3. 如果两者都不可能并且我必须创建两个单独的 git 存储库,那么至少,Gradle 应该无缝地处理链接博客文章中提到的事情,而无需进一步的人工干预。

My git and Gradle skills are beginner level. I would appreciate any help with this task. Thank you.

我的 git 和 Gradle 技能是初级水平。我将不胜感激这项任务的任何帮助。谢谢你。

回答by Michail Nikolaev

Yes, it is possible to move processor to separated module and use it from another module (see querydslaptbelow).

是的,可以将处理器移动到单独的模块并从另一个模块使用它(见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

你可以在这里找到完整的例子

回答by erdi

Another solution (in my opinion cleaner) could be to have two subprojectsand then simply make the one that contains annotation processors a dependency of the main one. So given two directories with your subprojects: coreand annotation-processorsin the root of your project, you would need to also have a settings.gradlefile 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.

应该这样做,您将不必处理自定义编译任务及其类路径。