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 CodeHere, 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 ProjectCreate 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 CodeNow 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.
|