且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven在同一个编译过程中预处理和生成类的示例?

更新时间:2022-06-25 05:29:55

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

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.