Tutorials  /  JavaScript

Using the Maven Exec Plugin to Run Java Programs

Ccentron Redaktion · November 2024 ·3 min read ·JavaScript, Tutorial

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:

XML
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.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Step 2: Writing the Java Class

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

Java
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:

Console
mvn exec:java

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

Console
[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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren