你如何将java注释处理器集成到java插件中 [英] How do you integrate the java annotation processor into the java plugin

查看:78
本文介绍了你如何将java注释处理器集成到java插件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其布局如下:

  src / 
java
生成



src / java包含jpa实体和查询类,它们使用jpa元模型类, a href =http://www.hibernate.org/subprojects/jpamodelgen.html =nofollow> hibernate元模型注释处理器。


将注释处理合并到java插件中的最佳方式是什么?



我目前定义了以下任务,但它对compileJava具有任务依赖性,的代码依赖于由注释处理器生成的类。

 任务processAnnotations(类型:编译){
genDir = new File($ {projectDir} / src / generated)
genDir.mkdirs()
source = ['src / java']
classpath = sourceSets.test。 compileClasspath
destinationDir = genDir
options.compilerA rgs = [-proc:only]
}


解决方案

processAnnotations 取决于 compileJava 的原因是您将测试编译类路径放在前任务的编译类路径,并且测试编译类路径包含已编译的生产代码(即输出 compileJava )。



至于如何最好地解决手头的问题,您不应该需要单独的编译任务。 Java编译器可以调用注释处理器并一次编译其生成的源代码(以及原始源代码)(请参阅 Annotation Processing )。您需要做的一件事就是将注释处理器放在编译类路径中:

 配置{
hibernateAnnotationProcessor

$ b依赖关系{
hibernateAnnotationProcessororg.hibernate:...
}

compileJava.compileClasspath + =配置。 hibernateAnnotationProcessor

(您不想将注释处理程序添加到编译配置,因为它会被认为是生产代码的依赖。)



从我所知道的情况来看,这就是它的全部(假设您使用的是JDK6或更高版本)。


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.

What is the best way to incorporate annotation processing into the java plugin?

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.

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"]
}

解决方案

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 (i.e. output of 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). 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.)

From what I can tell, this is all there is to it (assuming you are using JDK6 or higher).

这篇关于你如何将java注释处理器集成到java插件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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