Krzysztof Antoniak
Krzysztof Antoniak

Reputation: 451

How to divide compilation of maven project into two parts?

In my Maven 1 Java project I use sort of external data to create classes for compilation, thus I have to divide compilation into two steps: compile program, analyze data and create classes, compile those classes.

How may I describe such scenario in my pom.xml file?

Upvotes: 1

Views: 326

Answers (1)

A_Di-Matteo
A_Di-Matteo

Reputation: 27812

Is Maven 1 an hard requirement? In Maven 3, you could apply the following configuration to your POM:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <id>retrieve-config</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- replace by your generation step -->
                <executable>echo</executable>
                <arguments>
                    <argument>public</argument>
                    <argument>class</argument>
                    <argument>Main{}</argument>
                    <argument>></argument>
                    <argument>Main.java</argument>
                </arguments>
                <workingDirectory>${basedir}/src/main2/</workingDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.9</version>
            <executions>
                <execution>
                    <id>second-compilation-add-sources</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main2</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>second-compilation-compile</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <excludes>
                            <exclude>src/main/java/**/*.java</exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

In the configuration above three plugins are:

  • Generating the required code (I used the Exec Maven Plugin as an example, but here you could either execute your generator application or another maven plugin which generates the code you need)
  • Add the new generated code (its folder) to the Maven compilation path
  • Compile the new generated source in a further compilation step (which excludes what was already compiled as part of normal compilation)

I just tried it and it works fine on my machine (Windows machine).
Please note that the configuration above is just an example, I dynamically wrote in a text file a simple Java class via the echo command, then added its folder (the src/main2, which I previously created) to the compilation path and then compiled it. All as part of the process-classes phase, which happens right after the compile phase.
Using this approach, you can also test the whole code (generated and not) as part of the standard test phase.

Upvotes: 1

Related Questions