I am having a project where there is code being generated by a third-party tool (typically invoked from the command line). I would like to align this tool with our maven build setup, i.e. I would like to have it invoked, for example before maven compile.
Is there a way to execute an arbitrary Java program using one of the numerous maven plugins? Where would I insert it in the POM?
You want to use the Exec Maven Plugin, using its exec goal, which lets you execute an external application.
About when the external application is going to be executed, you have to consider the default Maven Build Lifecycle. When you launch the build process, Maven executes the following (strictly ordered) phases:
You can decide at which of the steps above running the external application by configuring the phase element in the Exec Maven Plugin executions element:
<!-- Begin of POM -->
<project>
...
<build>
<plugins>
<!-- Begin of Exec Maven Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>validate</phase> <!-- Here, for example, validate -->
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>...</configuration>
</plugin>
<!-- Begin of Exec Maven Plugin -->
</plugins>
</build>
...
</project>
<!-- End of POM -->
Use maven-exec-plugin:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>exec-one</id>
<phase>verify</phase>
<configuration>
<executable>echo</executable>
<arguments>
<argument>exec one</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Just like any other plugin it should be specified in a "build" section of POM file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With