Run maven build twice

I am running an ant project for maven, and this project is quite unusual: it uses two compilation steps and a code generation step between these compilation steps. The entire assembly process can be described as follows:

  • Compile everything in the src directory
  • Run the internal Java tool, point java to the compiled classes and banks used to compile these classes. This tool generates code based on compiled classes using reflection.
  • Compile the generated classes and finally build the jar.

I found some links that suggest creating a custom life cycle, but I don’t know where to start. If someone could point out a similar project configuration, that would be really cool.

What would be the easiest way to achieve this with maven? I assume that I should use the ant maven plugin, but I still do not understand how to get it to compile sources twice and point it to the generated sources after the first stage of compilation.

+4
source share
3 answers

OK, I finally figured out how to do this.

Basically, I run the compiler at different stages of the assembly, so that compilation takes place at the source-generation phase, the code is generated at the process sources phase, and finally the compiler does the final compilation at the “compile” stage.

Here is my configuration:

<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>

Hope this will be helpful to someone.

, , . , "" / "" .

+5

Maven. () Java . :

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

:

mvn install

.

+1

:

<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>

+1

Source: https://habr.com/ru/post/1523616/


All Articles