Edward Kennedy Edward Kennedy - 3 months ago 7
Java Question

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?

Answer

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

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <configuration>
                        <tasks>
                            <exec executable="ls">
                                <arg value="-l"/>
                                <arg value="-a"/>
                            </exec>
                        </tasks>
                    </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.

Comments