Using the Maven Exec Plugin to Run Java Programs

The Maven Exec Plugin is a versatile tool that allows you to execute Java and system programs directly from your Maven build process. This tutorial will guide you through the steps to configure and use the plugin, specifically focusing on its exec:java goal, which lets you run a Java program within the same JVM.

The Maven Exec Plugin provides two primary goals:

  • exec:exec: Execute any external program in a separate process.
  • exec:java: Run a Java program within the same virtual machine (VM) as your Maven build.

In this tutorial, we will focus on setting up and using the exec:java goal.

Step 1: Configuring the Plugin in pom.xml

To use the Maven Exec Plugin, it must first be configured within the build section of your pom.xml file. Below is an example of the required configuration:

	org.codehaus.mojo
	exec-maven-plugin
	1.6.0
	
		com.example.maven.utils.BuildInfo
	

The critical part of this configuration is the <mainClass> element, where you specify the fully qualified name of the Java class to be executed by the exec:java goal.

Step 2: Writing the Java Class

Here’s an example Java class, BuildInfo, which prints the Java version and the current time:

package com.example.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 3: Running the Build with the exec:java Goal

Once the plugin is configured and the Java class is written, you can run the Maven build with the following command:

Upon successful execution, you should see output similar to the following:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------< com.example.maven:maven-demo >---------------
[INFO] Building maven-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ maven-demo ---
********
Build Time: 2024-11-18T14:30:00
Java Version: 17.0.7
********
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.721 s
[INFO] Finished at: 2024-11-18T14:30:00+01:00
[INFO] ------------------------------------------------------------------------

Summary

The Maven Exec Plugin simplifies the process of running Java programs directly from your Maven build. By properly configuring the plugin in `pom.xml` and specifying the Java class in the “ element, you can quickly execute Java code without needing additional scripts or tools. This functionality is especially useful for tasks like testing, validating build information during development, automating repetitive tasks, or integrating custom logic into your build process. It ensures a seamless and efficient workflow, making it an invaluable tool for developers handling complex Java projects.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: