Updated April 17, 2023
Introduction to Maven exec plugin
It is used to execute java and system code from the maven command, basically, there are two goals i.e. exec: exec and exec: java. The first goal is exec: exec which was used to execute any of the programs in a different process, the second goal is exec: java which was used to execute the java program on the same server. To use any maven exec plugin in java first we need to add the same into the pom.xml file. Without adding the exec plugin into the pom.xml file we cannot use this in our project.
How does it work?
- Basically, it has two goals defined first one is exec: exec, and the second one is exec: java.
- The first goal is used to execute any code into the different processes. The second goal is used to run java code on the same server.
- Below is the version available for general use is as follows.
- Version 1.0
- Version 1.0.1
- Version 1.0.2
- Version 1.1
- Version 1.1.1
- Version 1.2
- Version 1.2.1
- Version 1.3
- Version 1.3.1
- Version 1.3.2
- Version 1.4.0
- Version 1.5.0
- Version 1.6.0
- Version 3.0.0
- To execute the java code using the maven exec plugin we need to configure the same into the build section of the pom.xml file.
- We need to add the below configuration in the pom.xml file to use the maven exec plugin in our project.
<plugin> -- Start of plugin section
<groupId> /* Start of groupId section */ name of groupid </groupId> -- end of groupid section.
<artifactId> /* Start of artifactId section */ exec-maven-plugin</artifactId> -- end of artifactId section.
<version> /* Start of version section */ version number </version> -- end of version section.
<configuration>
<mainClass> /* Start of mainClass section */ main class name </mainClass> -- end of mainClass section.
</configuration> -- -- end of configuration section.
</plugin> -- end of plugin section.
- The first section contains the plugin, the execution of maven exec starts from the plugin section. We need to end each section which was we have started.
- The second section contains the groupid, this is nothing but the name of groupid which was used in maven exec plugin.
- The third section contains the artifactid, this is nothing but the name of exec plugin which was used in the configuration of the maven exec plugin.
- The fourth section contains the version, this is nothing but the actual version number of exec plugin which was used in the configuration of the maven exec plugin.
- The fifth section contains the main class, this is nothing but the name of the main class of the exec plugin which was used in the configuration of maven exec plugin.
- Below is the important section which was we need to be used at the time of configuration of the maven exec plugin.
- Groupid
- ArtifactId
- Version
- Main class
- To configure it in our project we need to follow the below steps are as follows.
- Add this plugin configuration to the pom.xml file.
- Run the Maven build by using the exec: java goal.
- Add the configuration into the pom.xml file is very important. Without adding the configuration into the pom.xml file we cannot build the maven exec plugin.
- The mainClass element within the configuration is the most significant thing to notice. In that section, we have to define the class of java which was run by the exec: java goal.
- In the plugin configuration, we can explicitly provide all of the necessary execution details. Also we can supply all information or some information using system properties, depending on our use case.
- Below are the optional parameter which was used while configuring it are as follows.
- addOutputToClasspath – The type of this parameter is boolean.
- addResourceToClasspath – The type of this parameter is boolean.
- arguments – The type of this parameter is the list.
- async – The type of this parameter is boolean.
- asyncDestroyOnShutDown – The type of this parameter is boolean.
- classpathScope – The type of this parameter is string.
- commandlineArgs – The type of this parameter is string.
- environemntScript – The type of this parameter is file.
- environemntVariables – The type of this parameter is map.
- executable – The type of this parameter is string.
- executableDependency – The type of this parameter is executable dependency.
- longClasspath – The type of this parameter is boolean.
- longModulepath – The type of this parameter is boolean.
- outputFile – The type of this parameter is file.
- quietLogs – The type of this parameter is boolean.
- skip – The type of this parameter is boolean.
- sourceRoot – The type of this parameter is file.
Examples
Below is the example is as follows.
Example #1 – With default parameter
Code:
<plugin> -- Start of plugin section
<groupId> /* Start of groupId section */ org.codehaus.mojo</groupId> -- end of groupid section.
<artifactId> /* Start of artifactId section */ exec-maven-plugin</artifactId> -- end of artifactId section.
<version> /* Start of version section */ 1.6.0 </version> -- end of version section.
<configuration>
<mainClass> /* Start of mainClass section */ com.journaldev.maven.utils.BuildInfo</mainClass> -- end of mainClass section.
</configuration> -- -- end of configuration section.
</plugin> -- end of plugin section.
# mvn exec:java
Example #2 – With optional parameter
Code:
<plugin> -- Start of plugin section
<groupId> /* Start of groupId section */ org.codehaus.mojo</groupId> -- end of groupid section.
<artifactId> /* Start of artifactId section */ exec-maven-plugin</artifactId> -- end of artifactId section.
<version> /* Start of version section */ 1.6.0 </version> -- end of version section.
<configuration>
<executions>
<execution>
</configuration> -- -- end of configuration section.
</execution>
</executions>
</plugin> -- end of plugin section.
# mvn exec:java
Conclusion
It is very useful and important to execute java and system code from the maven command. There are two goals is used i.e. exec: exec and exec: java. To use it in the project we need to configure the same in pom.xml file.
Recommended Articles
We hope that this EDUCBA information on “Maven exec plugin” was beneficial to you. You can view EDUCBA’s recommended articles for more information.