Executing Java programs using the Exec Maven Plugin

The maven exec plugin permits the execution of system and Java programs directly from the maven command.

The maven exec plugin has two objectives.

    1. exec:exec – can be utilized to run any application in a distinct process.

 

    exec:java – can be employed to execute a Java program within the same virtual machine.

In this guide, we will discover the process of executing a Java program from our Maven project using exec:java.

First, include the necessary configurations for the exec-maven-plugin in the pom.xml file.

To utilize a maven plugin, it is necessary to set it up in the build section of the pom.xml. Simply incorporate the plugin configuration provided below into your project’s pom.xml file.

<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>exec-maven-plugin</artifactId>
		<version>1.6.0</version>
		<configuration>
			<mainClass>com.scdev.maven.utils.BuildInfo</mainClass>
		</configuration>
</plugin>

The main focus should be on the “mainClass” element within the “configuration”. This is where we indicate the Java class that will be executed through the exec:java goal.

This is the Java class content. It is a basic class that displays the Java version details and the current time.

package com.scdev.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: Execute the Maven build by using the goal exec:java.

This is the result obtained when the Maven build is executed with the exec:java objective.

$ mvn exec:java
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------< com.scdev.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 Example

Citations:
– Sources:
– Bibliography:

  • Exec Maven Plugin Home Page

 

 

more  tutorials

error Attempting to install Java on a MacBook.(Opens in a new browser tab)

Step-by-step Guide: Installing Rust on Ubuntu 20.04(Opens in a new browser tab)

The Spring @Async Annotation enables asynchronous processing.(Opens in a new browser tab)

Java Tutorial for beginners(Opens in a new browser tab)

Tutorial on how to set up a Hibernate Tomcat JNDI DataSource.(Opens in a new browser tab)

 

 

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds