如何编译在运行时生成的Java文件

3

我想编译在Code Generate.java中生成的类。 我能够通过exec-maven-plugin在运行时成功运行Generate.java。 它在generated-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>
1个回答

1

你离正确的方法很近了;插件需要绑定到不同的阶段(phase)。目前配置是在prepare-package阶段添加新的源目录,在所有内置编译完成后才进行。如果你将其配置为在生命周期的早期运行,我想一切都会"正常工作"。

用于操作测试代码的阶段(phase)如下:

  • generate-test-sources
  • process-test-sources
  • generate-test-resources
  • process-test-resources
  • test-compile

对于这种情况,我建议将配置更改为以下内容:

<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生命周期概述文档。

我尝试过了,但它不起作用。它甚至没有将代码复制到target/classes目录下,而我的当前pom文件正在复制带有.java扩展名的生成代码。 - Jaydeep Vishwakarma
你使用了与上面相同的配置,只是更改了阶段吗?我没有重新输入所有现有的配置。假设是的,下一步是使用设置为调试选项(-X)运行Maven,并检查编译器插件正在查找测试源的位置。然后配置插件以确保每个插件的目标是链中下一个插件的源。 - user944849

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接