java - 编译阶段后通过 Maven 插件生成新源

标签 java maven code-generation pom.xml

我有一个 Maven 项目,我需要在其中执行两个代码生成步骤。一个生成一些 Java 类型,然后第二个依赖于这些 Java 类型来生成更多代码。有没有办法在我的构建过程中同时执行这两个步骤?

目前我的步骤是:

  1. 执行第一个代码生成插件(在generate-sources期间)
  2. 将生成类型的目录添加到构建路径
  3. 执行第二个代码生成插件(在编译期间)

但是我的问题是,第二个代码生成插件生成的任何东西都不会被编译(因为编译阶段已经完成)。如果我将第二个代码生成插件附加到较早的阶段,它会失败,因为它需要第一个代码生成插件中的类出现在类路径中。

我知道我可以将它分成两个模块,一个依赖另一个模块,但我想知道这是否可以在一个 pom.xml 中实现。似乎需要一种在正常编译阶段完成后再次调用编译的方法。

有什么想法吗?

最佳答案

您始终可以配置编译器插件的两次执行,都与编译阶段相关联。在一个你包括额外的东西:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.3.2</version>
  <executions>
    <execution>
      <id>one</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration></configuration>
    </execution>  
    <execution>
      <id>two</id>
      <phase>compile</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
      <compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument>
      </configuration>
    </execution>
  </executions>
<plugin>

你也可以试试<includes><include>path/</include></includes>

根据official documentation :

When multiple executions are given that match a particular phase, they are executed in the order specified in the POM, with inherited executions running first.

但我不太明白你到底想要什么。 http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

关于java - 编译阶段后通过 Maven 插件生成新源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4595544/

相关文章:

java - 过于复杂的 oracle jdbc BLOB 处理

c# - 在运行时动态生成 DLL 程序集

maven - 无法在 build.gradle 的发布 block 中定义任何内容

c# - 解析 C# 代码(作为字符串)并插入其他方法

c# - 如何从 TypeDeclarationSyntax 获取 TypeSyntax

java - Hibernate:如何使用 HQL 设置 NULL 查询参数值?

java - 整数井字棋

java - 我如何获取日期时间戳作为 UTC

java - WildFly 中的依赖项问题 - 在 pom.xml 中定义的版本与运行时使用的版本不同

linux - 克隆 Maven 存储库(Archiva 管理器)