There are many situations when you want Maven to run a specific executable on your computer. A simple use case would be to run the JAR that you created. Another case would be to have Maven run commands that are not provided as plugins (for instance, create a native Windows installer).
Maven provides support to run any executable system in a separate process along with Java programs in the same virtual machine on which Maven runs. The Maven Exec plugin provides this support using the exec
goal (to run in a separate process) and the java
goal (to run Java programs in the same process).
simple-project
).mvn clean package exec:java –Dexec.mainClass="com.packt.cookbook.App"
We wanted to run the JAR file that we had created in the project. To do this, we called the java
goal of the Maven Exec plugin. We provided the plugin with the required parameter (mainClass
) so that it knew which main class needed to be run.
You could integrate the running of the executable as part of the project lifecycle. Let us do this for our example:
project-with-exec
).<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <id>hello-world</id> <phase>package</phase> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.packt.cookbook.App</mainClass> </configuration> </plugin>
mvn clean package
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project-with-exec --- [INFO] Building jar: C:projectsapache-maven-cookbookproject-with-exec arget project-with-exec-1.0-SNAPSHOT.jar [INFO] [INFO] --- exec-maven-plugin:1.3.2:java (hello-world) @ project-with-exec --- [WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6. 06:25:26.005 [com.packt.cookbook.App.main()] INFO com.packt.cookbook.App - Hell o World [INFO] ------------------------------------------------------------------------
The project is run during the package phase based on the configuration that we specified in the plugin.
The same can be done for non-Java executables; we need to invoke the exec
goal instead of the java
goal.
18.189.189.67