2014-01-24 84 views
4

我正在将ant项目迁移到maven,这个项目很不寻常:它在这些编译步骤之间使用了两个编译步骤和代码生成步骤。运行maven编译两次

  1. 编译一切都在src目录
  2. 内部运行的Java工具,指向Java来用于编译这些类编译的类和JAR:整个构建过程可以描述如下。该工具使用反射基于编译的类生成代码。
  3. 编译生成的类并最终组装一个jar。

我发现了一些建议创建自定义生命周期的链接,但我不知道从哪里开始。 如果有人可以指向类似的项目配置,那将非常棒。

用maven实现这个最简单的方法是什么? 我想我应该使用蚂蚁maven插件,但我仍然不明白如何使它编译源两次,并指出它在第一编译步骤后生成的源。

+0

我希望你能从Maven中获得一些好处来换取这种头痛。 – dnault

+0

我真正想从maven获得的唯一一件事是它的依赖管理。现代的maven也可以做并行构建。 – Alex

+0

依赖管理可能值得关注常春藤。对于具有异常构建流的现有Ant项目,Ant + Ivy可能比直接转向Maven更简单。无论如何,您的迁移运气最好! – dnault

回答

5

OK,我终于想通了如何做到这一点。

基本上我在不同的构建阶段运行编译器,以便编译发生在生成源阶段,代码在生成源阶段生成,最后编译器在“编译”阶段生成最终编译。

这里是我的配置:

<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您可能会注意到编译器不必要地编译了最终编译器传递的所有源代码。要微调特定的编译器密码,您可能需要为项目使用“排除”/“包含”配置。

+0

这段代码指定了默认的'generatedSourcesDirectory',它是多余的。小尼特。无论如何,非常有帮助的例子。 – djeikyb

+0

注意默认编译既神奇又需要。 –

1

我建议你最好创建两个单独的Maven项目。将(静态)Java源放入第一个项目中。把动态的东西进入第二个项目和第一个项目添加引用:

<dependency> 
    <groupId>org.yourgroup</groupId> 
    <artifactId>sub-module-1</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
</dependency> 

现在,建造和安装的第一个项目:

mvn install 

之后,你应该能够建立第二项目。

+0

我曾想过这件事,但对我来说似乎过度了,有点令人困惑。生成的代码不是那么大,并且与原始包中的源代码密切相关。所以,如果可能的话,我会保留一个项目。 – Alex

1

Example: Configuring compile to run twice

<plugin> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <configuration> 
    <source>1.5</source> 
    <target>1.5</target> 
    </configuration> 
    <executions> 
    <execution> 
     <id>default-compile</id> 
     <configuration> 
     <excludes> 
      <exclude>**/cli/*</exclude> 
     </excludes> 
     </configuration> 
    </execution> 
    <execution> 
     <id>build-java14-cli</id> 
     <phase>compile</phase> 
     <goals> 
     <goal>compile</goal> 
     </goals> 
     <configuration> 
     <source>1.3</source> 
     <target>1.3</target> 
     <includes> 
      <include>**/cli/*</include> 
     </includes> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

调整此配置,以您的需求

+0

这似乎与我所需要的非常接近,但我没有看到在给定编译步骤之间嵌入代码生成步骤的简单方法。可能我错过了一些非常基本的Maven特性,但在我看来,不可能在另一个插件中引用其他插件的执行阶段,是吗? – Alex