in this short article there is a Spring boot application (for visualizing Java 9 platform modules and their dependencies) implemented.
How do I retrieve only non-platform modules from my custom Spring boot application to visualize their dependencies?
I am able to retrieve the data from a standard standalone application in this way:
public static void main(String[] args) {
new Main().getModules();
}
public Map<String, HashSet<?>> getModules() {
var nodes = new HashSet<Node>();
var edges = new HashSet<Edge>();
fillNodeAndEdges2(nodes, edges);
Map<String, HashSet<?>> mapa1 = Map.of("nodes", nodes, "edges", edges);
Iterator<Entry<String, HashSet<?>>> it = mapa1.entrySet().iterator();
while (it.hasNext()) {
Entry<String, HashSet<?>> entry = it.next();
System.out.println(entry);
}
return mapa1;
}
private void fillNodeAndEdges2(HashSet<Node> nodes, HashSet<Edge> edges) {
Module myModule = this.getClass().getModule();
ModuleDescriptor myModuleDescriptor = myModule.getDescriptor();
Set<Requires> requires = myModuleDescriptor.requires();
String moduleName = myModule.getName();
for (Requires require : requires) {
System.out.println("require:" + require);
nodes.add(new Node(moduleName));
edges.add(new Edge(moduleName, require.name()));
}
}
Unfortunately, I am not able to retrieve the same list of modules with dependencies from Spring boot application. I tried the following code, but myModule is UNNAMED, so its ModuleDescriptor is null. Why is my module UNNAMED? The module should be explicit since the project contains module-info.java file:
private void fillNodeAndEdges2(HashSet nodes, HashSet edges) {
Map beans = ApplicationContext.getBeansWithAnnotation(Service.class);
Object bean = beans.values().iterator().next();
Module myModule = bean.getClass().getModule();
System.out.println("bean.getClass():" + bean.getClass()); //IT NICELY RETURNS THE CORRECT BEAN-CLASS
System.out.println("myModule:"+ myModule);
ModuleDescriptor myModuleDescriptor = myModule.getDescriptor();
if (myModuleDescriptor == null) {
System.out.println("myModuleDescriptor == NULL");
return;
}
Set<Requires> requires = myModuleDescriptor.requires();
String moduleName = myModule.getName();
for (Requires require : requires) {
System.out.println("require:" + require);
nodes.add(new Node(moduleName));
edges.add(new Edge(moduleName, require.name()));
}
}
module-info.java looks like as follows:
module module.graph {
requires spring.beans;
requires spring.context;
requires spring.web;
requires spring.boot.autoconfigure;
requires spring.boot;
requires spring.boot.starter.web;
requires java.base;
}
Maven dependencies are as follows:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.kodedu.modulegraph</groupId>
<artifactId>module.graph</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>module.graph</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>10</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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>module.graph</finalName>
</configuration>
</plugin>
</plugins>
</build>
And complete project is here
I am not sure whether Spring boot is already capable to work with Java-9-modules.