Tutorial

Exec Maven Plugin - Running Java Programs from Maven Build

Published on August 3, 2022
author

Pankaj

Exec Maven Plugin - Running Java Programs from Maven Build

Maven exec plugin allows us to execute system and Java programs from the maven command.

There are two goals of the maven exec plugin:

  1. exec:exec - can be used to execute any program in a separate process.
  2. 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.mojo</groupId>
		<artifactId>exec-maven-plugin</artifactId>
		<version>1.6.0</version>
		<configuration>
			<mainClass>com.journaldev.maven.utils.BuildInfo</mainClass>
		</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] ------------------------------------------------------------------------
$
Exec Maven Plugin Java Example
Exec Maven Plugin Java Example

References:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 31, 2020

Thank you Pankaj for the Maven tutorial, one of the best tutorial I came across in internet.

- Durga Prasad Patro

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.