运行 Maven 编译两次 [英] Run maven compilation twice

查看:31
本文介绍了运行 Maven 编译两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 ant 项目迁移到 maven,这个项目很不寻常:它使用两个编译步骤和这些编译步骤之间的代码生成步骤.整个构建过程可以描述如下:

I'm in process of migrating ant project to maven and this project is quite unusual: it uses two compilation steps and code generation step between those compilation steps. The whole build process can be described as follows:

  1. 编译src目录下的所有内容
  2. 运行内部 java 工具,将 java 指向已编译的类和用于编译这些类的 jar.此工具使用反射根据已编译的类生成代码.
  3. 编译生成的类并最终组装一个 jar.

我发现了一些建议创建自定义生命周期的链接,但我不知道从哪里开始.如果有人能指出类似的项目配置,那就太好了.

I found a few links that suggest to create a custom lifecycle but I've no idea where to start. If someone can point to similar project configuration that would be really great.

使用 maven 实现这一目标的最简单方法是什么?我想我应该使用 ant maven 插件,但我仍然不明白如何让它编译两次源代码并在第一个编译步骤后将其指向生成的源代码.

What would be the simplest way of achieving this with maven? I guess I should use ant maven plugin but I still don't understand how to make it compile sources twice and point it to the generated sources after first compilation step.

推荐答案

好的,我终于知道怎么做了.

OK, I finally figured out how to do that.

基本上,我在不同的构建阶段运行编译器,以便在生成源阶段进行编译,在处理源阶段生成代码,最后编译器在编译"阶段进行最终编译.

Basically I run compiler on different build phases so that compile happens on generate-sources phase, code is generated on process-sources phase and finally compiler makes final compilation on 'compile' phase.

这是我的配置:

<build>
<plugins>
  <!-- Code generation, executed after the first compiler pass -->
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
      <execution>
        <id>generateCode</id>
        <phase>process-sources</phase>
        <goals>
          <goal>java</goal>
        </goals>
        <configuration>
          <classpathScope>test</classpathScope>
          <mainClass>my.code.Generator</mainClass>
          <arguments>
            <argument>-target</argument>
            <argument>${project.build.directory}/generated-sources/java</argument>
            <argument>-source</argument>
            <argument>my.code.generator.Configuration</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <id>add-source</id>
        <phase>process-sources</phase>
        <goals>
          <goal>add-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>${project.build.directory}/generated-sources/java</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>


  <!-- Custom compilation mode -->
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <id>default-compile</id>
        <phase>generate-sources</phase>
      </execution>
      <execution>
        <id>build-generated-code</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
        <configuration>
          <generatedSourcesDirectory>${project.build.directory}/generated-sources/java</generatedSourcesDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>

希望这对某人有帮助.

更新 您可能会注意到编译器在最终编译器传递时不必要地编译了所有源代码.要微调您的特定编译器传递,您可能需要为您的项目使用排除"/包含"配置.

UPDATE You may probably notice that compiler unnecessarily compiles all the sources on final compiler pass. To fine tune your particular compiler pass you may want to use 'exclude'/'include' configuration for your project.

这篇关于运行 Maven 编译两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆