Maven is a build automation tool primarily used for Java projects. It has since been updated to support other languages like C#, Ruby, and Scala. Maven provides many build phases such as verify, test, compile, install, and more. In this article, we will explain how to run Maven from Java code. We will create a basic Maven project and write the logic in the main class to execute Maven commands.
Steps to Run Maven From Java Code
Here, we created a sample maven project by using the required dependencies using Spring Tool Suite IDE after that wrote the required logic for Run Maven From Java Code.
Step 1: Create a Simple Maven Project
Create a simple Maven project by using STS IDE. Below we provide the dependencies and project folder structure.
Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
Project Folder Structure:
Step 2: Update the pom.xml
After this Add below dependency in your project pom.xml after that update the project.
Maven Invoker dependency:
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.0.1</version>
</dependency>
pom.xml:
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavenbuild</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavenbuild</name>
<description>Spring Reactive</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 3: Add Logic to Run Maven from Java Code
Now open main class in the project. Then add below java logic to run maven from java code. Here, we write logic by using maven invoker dependency.
Java
package com.app;
import java.io.File;
import java.util.Collections;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.Invoker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MavenbuildApplication {
public static void main(String[] args) {
SpringApplication.run(MavenbuildApplication.class, args);
// Set Maven home directory
System.setProperty("maven.home", "D:\\Softwares\\apache-maven-3.8.8");
runMavenCommand();
}
public static void runMavenCommand() {
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("/src/pom.xml"));
request.setGoals(Collections.singletonList("clean install"));
Invoker invoker = new DefaultInvoker();
try {
invoker.execute(request);
System.out.println("Maven command executed successfully!");
} catch (Exception e) {
System.err.println("Error executing Maven command: " + e.getMessage());
e.printStackTrace();
}
}
}
Step 4: Run the Project
After this run this project, then we will get below output:
Output:

Notes:
- Ensure that the path to your Maven installation is correct when setting
maven.home
. - Make sure the path to the
pom.xml
file is correct relative to your project's root directory.
By following the above steps, we should be able to successfully run Maven commands from Java code.
Similar Reads
How to Run Java Code in Node.js ?
Running Java code within a Node.js environment can be useful for integrating Java-based libraries or leveraging Java's robust capabilities within a JavaScript application. This article will guide you through the steps required to execute Java code from a Node.js application, covering various methods
2 min read
Maven Compiler Plugin
The Maven Compiler Plugin helps you to compile Java source code. This plugin compiles your project's sources. Since version 3.0, the default compiler is javax.tools.JavaCompiler, which compiles Java sources. The compile goal is associated with the compile phase and is used to compile the primary sou
3 min read
How to pass java code a parameter from Maven for testing?
Maven is one of the most commonly used tools in Java projects for build automation, allowing the developer to manage the dependency of their project and many other operations, like compiling codes or running tests. Sometimes, you may want to pass some parameters from Maven to the Java code, so you c
3 min read
What is Core Java?
Java is a programming language that is class-based and object-oriented. It is designed to be general-purpose and aims to have fewer implementation dependencies, and serves as a computing platform for application development. However, many new to the software industry often ask what Core Java is. Wha
7 min read
Maven Commands and Options
Apache Maven is a powerful build automation tool mainly used for Java projects. It streamlines the build process by managing project dependencies, compiling source code, running tests, packaging applications, and deploying them. In this article, we will discuss some common Maven commands and options
5 min read
Java SE vs Java EE
Java is a highly popular and versatile programming language used globally to create various applications. Two major Java platforms are Java SE (Standard Edition) and Java EE (Enterprise Edition). Understanding the differences between these platforms is crucial for developers working on enterprise-le
5 min read
Maven Environment Setup
Maven is a build automation tool used for Java projects and other project types like C#, Scala, and more. The Maven project is managed by the Apache Software Foundation. Maven manages the build automation and lifecycle of software applications and generates WAR, JAR, and other executable files. In t
2 min read
How to run TestNG from command line?
Running TestNG from the command line is a powerful technique for managing your automated tests efficiently. TestNG is a popular testing framework that provides various features for running and configuring tests. While IDE-based execution is common, using the command line offers greater flexibility a
4 min read
Runtime JAR File in Java
JAR stands for Java Archive file. It is a platform-independent file format that allows bundling and packaging all files associated with java application, class files, audio, and image files as well. These files are needed when we run an applet program. It bundles JAR files use a Data compression alg
4 min read
Maven Central Repository
The Maven Central Repository is a widely used repository for Java developers to find open-source libraries and artifacts in Maven projects. It is the default repository in Apache Maven, a build automation tool primarily used for Java projects. Later, it was updated to support other types of projects
3 min read