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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6967514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:04:41  来源:igfitidea点击:

Maven example of annotation preprocessing and generation of classes in same compile process?

javamavencode-generationpreprocessor

提问by Jér?me Verstrynge

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?

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

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

有没有人有实施这样一个项目的分步程序?

回答by Jér?me Verstrynge

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

在网络上的现有文档中浏览了很多之后,我想出了以下内容:

What needs to be clarified:

需要澄清的是:

  • 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 hereand herehelp understanding the basics of annotation processing and implementation in Java 5. Good reading for newbies.
  • Implementing annotation processing in Java 5 with Mavenis tricky. One needs to add a local dependency to tools.jarto 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 notjump-start implementing their processors according to the above tutorials.
  • 为了处理给定项目 P 上的注释,您首先需要一个在单独的库 S 中编译的注释处理器。 P 应该依赖于 S。
  • 在 Java 5 中实现注解处理与在 Java 6 中完全不同。
  • Java 5 依赖于 apt 的单独执行。此处此处的相应教程有助于理解 Java 5 中注释处理和实现的基础知识。适合新手阅读。
  • 使用 Maven在 Java 5 中实现注释处理很棘手。需要添加一个本地依赖tools.jar来访问这些教程中描述的 API。不干净。一些调用 apt 的第三方插件可用,但没有很好的文档记录。
  • 那些使用 Java 6 的人应该根据上述教程快速开始实现他们的处理器。

Annotation Processing in Java 6 with Maven

使用 Maven 在 Java 6 中进行注释处理

  • A new packagehas 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 thisone 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 issuewith the maven-compiler-plugin, but a workaround is available here. Use it to compile your generated code as part of existing annotated code.
  • Java 6 中提供了一个新的来处理注解:Pluggable Annotation Processing
  • 要实现处理器,请创建一个单独的 Maven 项目。上面的教程或这个教程解释了如何进行。这是我们的图书馆 S。
  • 然后,创建您的项目 P 并在 S 上添加 Maven 依赖项。
  • 目前的问题Maven的编译器插件,但已有了解决方法在这里。使用它来编译您生成的代码作为现有注释代码的一部分。

...and code generation

...和代码生成

  • A great Java code generation library called CodeModelis available from Maven central. A good tutorial is available here. The javax annotation processing package offers some tools to generate output too.
  • Maven 中心提供了一个名为CodeModel 的优秀 Java 代码生成库。这里有一个很好的教程。javax 注释处理包也提供了一些工具来生成输出。

回答by dizzi

maven-processor-plugin can do that...

maven-processor-plugin 可以做到这一点......

https://code.google.com/p/maven-annotation-plugin/

https://code.google.com/p/maven-annotation-plugin/

Example from documentation:

文档中的示例:

<build> <plugins>
  <!-- Run annotation processors on src/main/java sources -->
  <plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
      <execution>
        <id>process</id>
        <goals>
          <goal>process</goal>
        </goals>
        <phase>generate-sources</phase>
      </execution>
    </executions>
  </plugin>
  <!-- Disable annotation processors during normal compilation -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <compilerArgument>-proc:none</compilerArgument>
    </configuration>
  </plugin>
</plugins> </build>

回答by khmarbaise

The Maven-Antlr-Plugin exactly does this. It generates Java classes from a grammar and the compile plugin compiles the generated classes. May it might be usefull the maven-annotation-plugin

Maven-Antlr-Plugin 正是这样做的。它从语法生成 Java 类,编译插件编译生成的类。可能对maven-annotation-plugin有用