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
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
How to exclude TestNG Groups from Maven?
To exclude specific TestNG groups from execution when running tests through Maven, you can configure the maven-surefire-plugin in the Maven pom.xml file. The Surefire plugin is responsible for running the unit tests during the build process, and it supports excluding certain groups from being execut
2 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
Setting the Java Version in Maven
Setting the Java version in Maven is important for ensuring compatibility and proper compilation of our project. By configuring the Maven Compiler Plugin in the pom.xml file, we can specify both the source and target Java versions. This ensures that our project is compiled using the desired Java ver
2 min read
run() Method in Java Thread
The run() method is available in the thread class constructed using a separate Runnable object. Otherwise, this method does nothing and returns. We can call the run() method multiple times. The run() method can be called in two ways which are as follows: Using the start() method.Using the run() meth
4 min read
Byte Code in Java
Byte Code Byte Code can be defined as an intermediate code generated by the compiler after the compilation of source code(JAVA Program). This intermediate code makes Java a platform-independent language. How is Byte Code generated? Compiler converts the source code or the Java program into the Byte
2 min read
Building a Java Project with Maven
Maven is one of the most popular build automation tools. It is developed using the Java programming language and is primarily used for Java-based projects. However, it has been updated to support programming languages like C#, Ruby, and more. This article will teach us how to build a Java project wi
2 min read