Search code examples
javamavenmaven-plugin

Is there a way to execute a program in maven to help build your project?


I am currently working on a project that uses a tool that takes the following example IDL file and generates about 5 Java classes from it.

struct Example { int x; int y; };

Is there a way to get Maven to use the command line tool that we use to automatically create those Java classes when it builds?


Solution

  • You can use maven-antrun-plugin plugin to run arbitrary Ant tasks or even any command-line program:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <configuration>
                            <target>
                                <exec executable="ls">
                                    <arg value="-l"/>
                                    <arg value="-a"/>
                                </exec>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    With this configuration your command-line program will be executed before the compilation, so generated Java sources will be available to the rest of the code.