简体   繁体   English

如何将java注释处理器集成到java插件中

[英]How do you integrate the java annotation processor into the java plugin

I have a project that is laid out as follows: 我的项目布局如下:

src/
  java
  generated

src/java contains jpa entities and query classes that use the jpa metamodel classes that are generated by the hibernate metamodel annotation processor . src / java包含jpa实体和查询类,它们使用由hibernate元模型注释处理器生成的jpa元模型类。

What is the best way to incorporate annotation processing into the java plugin? 将注释处理合并到java插件的最佳方法是什么?

I currently have the following task defined, but it has a task dependency on compileJava which will fail because some of the code is dependent on the classes that are generated by the annotation processor. 我目前定义了以下任务,但它对compileJava有一个任务依赖,它将失败,因为某些代码依赖于注释处理器生成的类。

task processAnnotations(type: Compile) {
    genDir = new File("${projectDir}/src/generated")
    genDir.mkdirs()
    source = ['src/java']
    classpath = sourceSets.test.compileClasspath
    destinationDir = genDir
    options.compilerArgs = ["-proc:only"]
}

Here is simple setup that works and integrates seamlessly with netbeans. 这是一个简单的设置,可以与netbeans无缝集成。 Javac will basicly do all the job needed without much intervention. Javac将基本完成所需的所有工作,无需太多干预。 The rest are small treaks that will make it work with IDEs like Netbeans. 其余的都是小型的,它们可以与像Netbeans这样的IDE一起使用。

apply plugin:'java'

dependencies {
    // Compile-time dependencies should contain annotation processors
    compile(group: 'org.hibernate...', name: '...', version: '...')
}

ext {
    generatedSourcesDir = file("${buildDir}/generated-sources/javac/main/java")
}

// This section is the key to IDE integration.
// IDE will look for source files in both in both
//
//  * src/main/java
//  * build/generated-sources/javac/main/java
//
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir generatedSourcesDir
        }
    }
}

// These are the only modifications to build process that are required.
compileJava {
    doFirst {
        // Directory should exists before compilation started.
        generatedSourcesDir.mkdirs()
    }
    options.compilerArgs += ['-s', generatedSourcesDir]
}

And that's it. 就是这样。 Javac will make the rest of the job. Javac将完成剩下的工作。

The reason why processAnnotations depends on compileJava is that you put the test compile class path on the former task's compile class path, and the test compile class path contains the compiled production code (ie output of compileJava ). processAnnotations依赖于compileJava的原因是你将测试编译类路径放在前任务的编译类路径上,而测试编译类路径包含编译的生产代码(即compileJava输出)。

As to how to best solve the problem at hand, you shouldn't need a separate compile task. 至于如何最好地解决手头的问题,你不应该需要一个单独的编译任务。 The Java compiler can invoke annotation processors and compile their generated sources (along with the original sources) in one pass (see Annotation Processing ). Java编译器可以调用注释处理器并在一次传递中编译它们生成的源(以及原始源)(请参阅注释处理 )。 One thing you'll need to do is to put the annotation processor on the compile class path: 您需要做的一件事是将注释处理器放在编译类路径上:

configurations {
    hibernateAnnotationProcessor
}

dependencies {
    hibernateAnnotationProcessor "org.hibernate: ..."
}

compileJava.compileClasspath += configurations.hibernateAnnotationProcessor

(You don't want to add the annotation processor to the compile configuration because then it will be considered a dependency of the production code.) (您不希望将注释处理器添加到compile配置中,因为它将被视为生产代码的依赖项。)

From what I can tell, this is all there is to it (assuming you are using JDK6 or higher). 据我所知,这就是它的全部内容(假设您使用的是JDK6或更高版本)。

As of gradle 4.6 you simply need to define the dependency of your processor in the appropriate configuration: 从gradle 4.6开始,您只需要在适当的配置中定义处理器的依赖关系:

dependencies {
    ...

    annotationProcessor 'org.hibernate:hibernate-jpamodelgen:4.3.7.Final'
}

Ref: https://docs.gradle.org/4.6/release-notes.html#added-annotationprocessor-configurations 参考: https//docs.gradle.org/4.6/release-notes.html#added-annotationprocessor-configurations

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

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