Question

I have a simple code generator G, that reads an interface A from my project and generates a new interface B from it. Therefore I need to achieve this:

  1. Compile A
  2. Execute G
  3. Compile B

Steps 1 and 3 are handled by the maven-compiler-plugin, while for step 2 I use the maven-exec-plugin. Currently steps 1 and 2 work well, but I can't figure out how to run the compiler plugin again to compile the newly generated version of B.

Is this possible with maven, or is there another approach to solve my problem?

Solution:

Based on the answer of khmarbaise I added this to my pom.xml to let the first compile run in the generate-sources phase and the code generation in the process-sources phase what makes the generated class available in the compile phase:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <executions>
                <execution>
                    <id>pre-compile</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.example.MyCodeGenerator</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Was it helpful?

Solution

Lets do the compile in generate-sources. Simply configure the maven-compiler-plugin to run in that particular lifecycle-phase and put the generated code (compiled code) somewhere else than by default. Second let your execution run in a phase afterwards (process-sources) and finally let the rest do as usual. The result of that is that you have to bind the maven-compiler-plugin to the generate-sources phase, the exec-plugin to the process-sources lifecycle-phase.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top