Maven在同一个编译过程中预处理和生成类的示例? [英] Maven example of annotation preprocessing and generation of classes in same compile process?

查看:151
本文介绍了Maven在同一个编译过程中预处理和生成类的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人在编译时预先处理类注释的maven项目有一个干净的例子,后续生成的类将在同一个编译过程中编译?

Does anyone have a clean example of a maven project preprocessing class annotations at compile time with subsequent generation of classes to be compiled in the same compilation process?

有没有人有一个逐步的过程来实现这样的项目?

Does anyone have a step-by-step procedure to implement such a project?

推荐答案

在网上的现有文档中导航很多,我想出了以下内容:

After navigating a lot in existing documentation on the net, I came up with the following:

需要澄清的内容:


  • 为了处理给定项目P上的注释,首先需要在单独的库中编译注释处理器S. P应该对S具有依赖性。

  • 在Java 5中实现注释处理与Java 6完全不同。

  • Java 5依赖于apt的单独执行。相应的教程此处这里帮助理解Java 5中注释处理和实现的基础知识。新手阅读的好读物。

  • 使用Maven 在Java 5 中实现注释处理非常棘手。需要在 tools.jar 中添加本地依赖项才能访问这些教程中描述的API。不干净。一些调用apt的第三方插件可用,但没有很好的文档记录。

  • 那些使用Java 6的人应该根据上面的内容快速启动它们的处理器教程。

  • In order to process annotations on a given project P, you first need an annotation processor compiled in a separate library S. P should have a dependency on S.
  • Implementing annotation processing in Java 5 is absolutely not the same thing as in Java 6.
  • Java 5 relies on a separate execution of apt. The corresponding tutorials here and here help understanding the basics of annotation processing and implementation in Java 5. Good reading for newbies.
  • Implementing annotation processing in Java 5 with Maven is tricky. One needs to add a local dependency to tools.jar to access the API described in these tutorials. Not clean. Some third-party plugins calling the apt are available, but not well documented.
  • Those using Java 6 should not jump-start implementing their processors according to the above tutorials.

带有Maven的Java 6中的注释处理


  • 一个新的以处理注释:可插入注释处理

  • 要实现处理器,请创建一个单独的Maven项目。以上教程或这个解释如何进行。这是我们的库S.

  • 然后,创建项目P并在S上添加Maven依赖项。

  • 目前有issue maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.htmlrel =nofollow noreferrer> maven-compiler-plugin ,但有一种解决方法这里。使用它来编译生成的代码作为现有注释代码的一部分。

  • A new package has been delivered in Java 6 to process annotations: the Pluggable Annotation Processing.
  • To implement a processor, create a separate Maven project. The above tutorial or this one explains how to proceed. This is our library S.
  • Then, create your project P and add a Maven dependency on S.
  • There is currently an issue with the maven-compiler-plugin, but a workaround is available here. Use it to compile your generated code as part of existing annotated code.

...和代码生成


  • 一个很棒的Java代码生成库,名为 CodeModel 可从Maven central获得。 此处提供了一个很好的教程。 。 javax注释处理包提供了一些生成输出的工具。

  • A great Java code generation library called CodeModel is available from Maven central. A good tutorial is available here. The javax annotation processing package offers some tools to generate output too.

这篇关于Maven在同一个编译过程中预处理和生成类的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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