将注解处理器与 Gradle 集成 [英] Integrating annotation processors with Gradle

查看:43
本文介绍了将注解处理器与 Gradle 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一些注释处理器.我找到了这篇博文,其中提到了如何在一般环境中和使用 Eclipse 完成此操作.

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.

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

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:

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

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

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

推荐答案

是的,可以将处理器移动到单独的模块并从另一个模块使用它(参见下面的 querydslapt).

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

我会建议您实现自己的抽象处理器

并像这样使用它:

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

这篇关于将注解处理器与 Gradle 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆