- Запуск программ Java в сборке Maven
- 1: Настройка плагина в pom.xml
- 2: Запуск сборки maven с целью exec:java
- Exec maven plugin java parameters
- Usage
- Exec goal
- Command line
- POM Configuration
- Java goal
- Differences compared to plain command line
- Command line
- POM Configuration
- Exec Maven Plugin — Running Java Programs from Maven Build
- Step 1: Adding exec-maven-plugin Configurations to pom.xml
- Step 2: Running the maven build with exec:java goal
- References:
Запуск программ Java в сборке Maven
Плагин exec в Maven позволяет выполнять системные и Java-программы из командной строки maven.
У данного плагина есть две цели:
- exec:exec — выполняет любую программу в отдельном процессе.
- exec:java — может запускать программы Java на той же виртуальной машине.
В этом руководстве мы узнаем, как использовать цель exec:java для запуска программы Java в сборке maven.
1: Настройка плагина в pom.xml
Если вы хотите использовать какой-либо плагин maven, вам нужно настроить его в разделе сборки pom.xml. Просто добавьте приведенную ниже конфигурацию плагина в файл проекта pom.xml.
plugin> groupId>org.codehaus.mojogroupId> artifactId>exec-maven-pluginartifactId> version>1.6.0version> configuration> mainClass>com.journaldev.maven.utils.BuildInfomainClass> configuration> plugin>
Наиболее важным моментом, на который следует обратить внимание, является элемент mainClass внутри configuration . Здесь мы указываем класс Java, который будет выполняться целью exec:java.
Вот содержимое класса Java. Это простой класс, в котором мы выводим сведения о версии Java и текущее время.
package com.journaldev.maven.utils; import java.time.LocalDateTime; public class BuildInfo public static void main(String[] args) String javaVersion = Runtime.version().toString(); String time = LocalDateTime.now().toString(); System.out.println("********\nBuild Time: " + time + "\nJava Version: " + javaVersion + "\n********"); > >
2: Запуск сборки maven с целью exec:java
Если мы запустим сборку maven с целью exec:java, мы получим следующий результат:
$ mvn exec:java [INFO] Scanning for projects... [INFO] [INFO] --------------- com.journaldev.maven:maven-example-jar >--------------- [INFO] Building maven-example-jar 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ maven-example-jar --- ******** Build Time: 2020-01-10T12:44:17.718061 Java Version: 13.0.1+9 ******** [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.591 s [INFO] Finished at: 2020-01-10T12:44:17+05:30 [INFO] ------------------------------------------------------------------------ $
Exec maven plugin java parameters
- MojoHaus /
- Exec Maven Plugin /
- Usage
- | Last Published: 2022-07-19
- | Version: 3.1.0
- Maven
Usage
Exec goal
You can formally specify all the relevant execution information in the plugin configuration. Depending on your use case, you can also specify some or all information using system properties.
Command line
Using system properties you would just execute it like in the following example.
mvn exec:exec -Dexec.executable="maven" [-Dexec.workingdir="/tmp"] -Dexec.args="-X myproject:dist"
POM Configuration
Add a configuration similar to the following to your POM:
. . org.codehaus.mojo exec-maven-plugin 3.1.0 . exec maven /tmp -X myproject:dist .en_US
Java goal
This goal helps you run a Java program within the same VM as Maven.
Differences compared to plain command line
The goal goes to great length to try to mimic the way the VM works, but there are some subtle differences. Today all differences come from the way the goal deals with thread management.
command line | Java Mojo |
The VM exits as soon as the only remaining threads are daemon threads | By default daemon threads are joined and interrupted once all known non daemon threads have quit. The join timeout is customisable The user might wish to further cleanup by stopping the unresponsive threads. The user can disable the full extra thread management (interrupt/join/[stop]) |
Read the documentation for the java goal for more information on how to configure this behavior.
If you find out that these differences are unacceptable for your case, you may need to use the exec goal to wrap your Java executable.
Command line
If you want to execute Java programs in the same VM, you can either use the command line version
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] .
POM Configuration
or you can configure the plugin in your POM:
. . org.codehaus.mojo exec-maven-plugin 3.1.0 . java com.example.Main argument1 .. myproperty myvalue
Note: The java goal doesn’t spawn a new process. Any VM specific option that you want to pass to the executed class must be passed to the Maven VM using the MAVEN_OPTS environment variable. E.g.
Otherwise consider using the exec goal.
Exec Maven Plugin — Running Java Programs from Maven Build
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
- exec:exec — can be used to execute any program in a separate process.
- exec:java — can be used to run a Java program in the same VM.
In this tutorial, we will learn how to use exec:java to run a Java program from our maven project.
Step 1: Adding exec-maven-plugin Configurations to pom.xml
If you want to use any maven plugin, you need to configure it in the pom.xml build section. Just add the below plugin configuration to your project pom.xml file.
plugin> groupId>org.codehaus.mojogroupId> artifactId>exec-maven-pluginartifactId> version>1.6.0version> configuration> mainClass>com.journaldev.maven.utils.BuildInfomainClass> configuration> plugin>
The most important point to note here is the “mainClass” element inside the “configuration”. This is where we specify the Java class that will be executed by the exec:java goal.
Here is the content of the Java class. It’s a simple class where we are printing Java version details and the current time.
package com.journaldev.maven.utils; import java.time.LocalDateTime; public class BuildInfo public static void main(String[] args) String javaVersion = Runtime.version().toString(); String time = LocalDateTime.now().toString(); System.out.println("********\nBuild Time: " + time + "\nJava Version: " + javaVersion + "\n********"); > >
Step 2: Running the maven build with exec:java goal
Here is the output when we run the maven build with the exec:java goal.
$ mvn exec:java [INFO] Scanning for projects... [INFO] [INFO] --------------- com.journaldev.maven:maven-example-jar >--------------- [INFO] Building maven-example-jar 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ maven-example-jar --- ******** Build Time: 2020-01-10T12:44:17.718061 Java Version: 13.0.1+9 ******** [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.591 s [INFO] Finished at: 2020-01-10T12:44:17+05:30 [INFO] ------------------------------------------------------------------------ $
References:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.