Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行maven编译两次_Java_Maven_Ant - Fatal编程技术网

Java 运行maven编译两次

Java 运行maven编译两次,java,maven,ant,Java,Maven,Ant,我正在将ant项目迁移到maven,这个项目非常不寻常:它在编译步骤之间使用两个编译步骤和代码生成步骤。整个构建过程可描述如下: 编译src目录中的所有内容 运行内部java工具,将java指向已编译的类和用于编译这些类的JAR。此工具使用反射根据编译的类生成代码 编译生成的类并最终组装一个jar 我找到了一些建议创建自定义生命周期的链接,但我不知道从哪里开始。 如果有人能指出类似的项目配置,那将是非常棒的 用maven实现这一点最简单的方法是什么? 我想我应该使用ant maven插件,但我仍

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

  • 编译src目录中的所有内容
  • 运行内部java工具,将java指向已编译的类和用于编译这些类的JAR。此工具使用反射根据编译的类生成代码
  • 编译生成的类并最终组装一个jar
  • 我找到了一些建议创建自定义生命周期的链接,但我不知道从哪里开始。 如果有人能指出类似的项目配置,那将是非常棒的

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

    我建议您最好创建两个独立的maven项目。将(静态)Java源代码放入第一个项目中。将动态内容放入第二个项目,并添加对第一个项目的引用:

    <dependency>
      <groupId>org.yourgroup</groupId>
      <artifactId>sub-module-1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
    
    mvn install
    
    之后,您应该能够构建第二个项目。

    
    maven编译器插件
    1.5
    1.5
    默认编译
    **/cli/*
    build-java14-cli
    编译
    编译
    1.3
    1.3
    **/cli/*
    

    根据您的需要调整此配置

    好的,我终于想出了方法

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

    以下是我的配置:

    <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>
    
    
    org.codehaus.mojo
    execmaven插件
    1.2.1
    生成代码
    过程源
    JAVA
    测试
    我的代码生成器
    -目标
    ${project.build.directory}/generated sources/java
    -来源
    my.code.generator.Configuration
    org.codehaus.mojo
    构建助手maven插件
    1.1
    添加源
    过程源
    添加源
    ${project.build.directory}/generated sources/java
    maven编译器插件
    默认编译
    生成源
    生成生成的代码
    编译
    编译
    ${project.build.directory}/generated sources/java
    
    希望这将有助于某人


    更新您可能会注意到编译器在最终编译过程中不必要地编译所有源代码。要微调特定的编译器过程,您可能需要在项目中使用“exclude”/“include”配置。

    我希望您能从Maven中获得一些好处,以换取这个头痛的问题。我唯一真正想从Maven中获得的是它的依赖项管理。此外,现代maven还能够进行并行构建。可能值得一看Ivy的依赖关系管理。对于具有异常构建流的现有Ant项目,Ant+Ivy可以是比直接迁移到Maven更简单的迁移路径。无论如何,祝您迁移好运!谢谢你们指点常春藤,但我必须坚持使用maven,因为很多其他项目已经使用了它。如果有人感兴趣——目前我决定坚持使用多模块项目配置——这不是一个理想的解决方案,但它看起来像是一个简单的方法来实现我想要的。虽然我仍然没有失去为maven找到一个干净、更好的解决方案的希望。我想了想,但这对我来说似乎太过分了,有点困惑。生成的代码没有那么大,并且与原始包中的源代码密切相关。因此,如果可能的话,我会保留一个项目。这似乎非常接近我所需要的,但我看不到在给定的编译步骤之间嵌入代码生成步骤的简单方法。可能我错过了一些非常基本的maven功能,但在我看来,在其他插件中不可能引用其他插件的执行阶段,是吗?此代码指定了默认的
    generatedsourcesddirectory
    ,这是多余的。小尼特。无论如何,这是一个非常有用的例子。请注意,默认编译既神奇又必需。
    <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>