【问题标题】:How to compile java files which are getting generated at run time如何编译在运行时生成的 java 文件
【发布时间】:2018-04-16 04:03:17
【问题描述】:

我希望编译由 Code Generate.java 在运行时生成的类。我可以通过 exec-maven-plugin 在运行时成功运行 Generate.java。它在 generate-source-java 中生成代码。但是这段代码没有被编译。我还想将它们添加到一个 jar 中,因为我正在使用 maven-assembly-plugin。
这是我的 pom 快照。

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
      <execution>
      <id>build-test-environment</id>
      <phase>generate-test-resources</phase>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
    </executions>
    <configuration>
      <mainClass>com.test.Generate</mainClass>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
      <execution>
        <phase>prepare-package</phase>
        <goals>
          <goal>add-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>${project.build.directory}/generated-sources-java</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <finalName>test</finalName>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

【问题讨论】:

    标签: maven plugins runtime exec-maven-plugin


    【解决方案1】:

    你很接近;插件需要绑定到不同的阶段。目前配置是在prepare-package 阶段添加新的源目录,这发生在所有内置编译完成之后。如果您将其配置为在生命周期的早期运行,我认为一切都会“正常工作”。

    操作测试代码的阶段是:

    • 生成测试源
    • 过程测试源
    • 生成测试资源
    • 流程测试资源
    • 测试编译

    对于这种情况,我将更改配置,如下所示:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.5.0</version>
      <executions>
        <execution>
          <id>build-test-environment</id>
          <phase>generate-test-sources</phase>  <!-- generating source code -->
          <!-- rest of config -->
        </execution>
      </executions>
          <!-- rest of config, consider moving into specific execution -->
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>add-test-sources</id>
          <phase>process-test-sources</phase>  <!-- do something with generated test sources -->
          <goals>
            <goal>add-test-source</goal>
          </goals>
          <!-- rest of config -->
      </execution>
    </executions>
    

    请注意,我也更改了 build-helper-maven-plugin 目标(更改为 add-test-source),因为这是我们正在处理的测试代码。

    Maven Lifecycle 概述文档中的更多详细信息。

    【讨论】:

    • 我试过了,还是不行。它甚至没有将代码复制到目标/类,而我当前的 pom 正在复制带有 .java 扩展名的生成代码。
    • 您是否使用了与上述相同的配置,只是更改了阶段?我没有重新键入所有现有配置。假设是,下一步是在设置了调试选项 (-X) 的情况下运行 Maven,并检查编译器插件在哪里寻找测试源。然后配置插件以确保每个插件的目标是链中下一个的源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2020-07-23
    • 2014-03-19
    相关资源
    最近更新 更多