In this tutorial, I will show you how to use Apache Maven’s Maven Model library to analyze and get information about dependencies and plugins being used for a Maven project.
First, I will create a new Maven project as an example:
Maven Model dependency is as follows:
1 2 3 4 5 |
<dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-model</artifactId> <version>3.9.1</version> </dependency> |
The Maven project that I use to read the information is here https://github.com/khanhnguyenj/huongdanjava.com/tree/master/spring-boot-actuator!
The contents of the pom.xml file in this project are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?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.0.5</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.huongdanjava</groupId> <artifactId>spring-boot-actuator</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-actuator</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
To read the information in this pom.xml file, first, we will use Java’s FileReader object to connect and prepare to read the contents of the pom.xml file:
1 2 |
String pomXml = "/Users/khanh/Documents/code/huongdanjava.com/spring-boot-actuator/pom.xml"; Reader reader = new FileReader(pomXml); |
Once prepared, we will use the object of the MavenXpp3Reader class of the Maven Model library to read and parse the contents of this pom.xml file into the Model object of this library:
1 2 |
MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); Model model = xpp3Reader.read(reader); |
The Model object will contain the complete information of the pom.xml file, represented by Java objects. And so it allows us to get the information we want!
For example, if you now want to get the information of all the dependencies being declared, you can use the getDependencies() method of the Model object as follows:
1 2 |
List<Dependency> dependencies = model.getDependencies(); dependencies.forEach(d -> System.out.println(d.getArtifactId())); |
As you can see, we will get the list of Dependency objects declared in the pom.xml file from this Model object.
Result:
Please note that if the pom.xml file uses the parent pom.xml file to manage the dependency version (like the example I’m using), to get the version information of a dependency, you need to read the content of the pom.xml file of this Parent object! Parent object information you can get as follows:
1 2 |
Parent parent = model.getParent(); System.out.println(parent.getArtifactId()); |
Result:
With the above example, I can also get the list of Plugins being declared as follows:
1 2 |
List<Plugin> plugins = model.getBuild().getPlugins(); plugins.forEach(p -> System.out.println(p.getArtifactId())); |
Result: