28

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.

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. 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.

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

4

2 回答 2

9

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

我会推荐你​​实现自己的AbstractProcessor

并像这样使用它:

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()
}

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

于 2013-03-23T10:33:00.193 回答
9

另一种解决方案(在我看来更简洁)可能是拥有两个子项目,然后简单地将包含注释处理器的那个作为主项目的依赖项。因此,给定两个包含子项目的目录:在项目coreannotation-processors根目录中,您还需要一个settings.gradle包含以下内容的文件:

include 'core'
include 'annotation-processors'

然后在核心项目的 gradle 文件中:

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

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

于 2013-03-23T11:37:08.813 回答