Skip to content

Commit

Permalink
feat: add feature to get maven module dependency graph (#151)
Browse files Browse the repository at this point in the history
* feat: add feature to get maven module dependency graph

* Add maven model dependency

* Please spotless

* Rectify IntelliJ warnings

* Make names clearer

* Detect if there are submodule as dependency

* Please spotless
  • Loading branch information
algomaster99 authored Feb 26, 2024
1 parent 5d90c92 commit e917911
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<artifactId>asm-util</artifactId>
<version>9.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.9.6</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
4 changes: 4 additions & 0 deletions terminator-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<artifactId>classgraph</artifactId>
<version>4.8.165</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.github.algomaster99.terminator.commons.maven;

import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;

public class MavenModule {
private final MavenModule parent;

private final Model self;

private final Path fileSystemPath;

private final List<MavenModule> submodules = new ArrayList<>();

MavenModule(Model self, Path fileSystemPath, MavenModule parent) {
this.self = self;
this.fileSystemPath = fileSystemPath;
this.parent = parent;
}

public void addSubmodule(MavenModule child) {
submodules.add(child);
}

public Model getSelf() {
return self;
}

public List<MavenModule> getSubmodules() {
return submodules;
}

public Path getFileSystemPath() {
return fileSystemPath;
}

public MavenModule findSubmodule(String artifactIdOfModule) {
Queue<MavenModule> queue = new ArrayDeque<>();
queue.add(topLevelParent());
while (!queue.isEmpty()) {
MavenModule module = queue.poll();
if (module.getSelf().getArtifactId().equals(artifactIdOfModule)) {
return module;
}
queue.addAll(module.getSubmodules());
}
return null;
}

public MavenModule topLevelParent() {
if (parent == null) {
return this;
}
return parent.topLevelParent();
}

public List<Model> getSubmodulesThatAreDependencies() {
List<Model> subModulesThatAreDependencies = new ArrayList<>();
List<Dependency> dependencies = self.getDependencies();
for (Dependency dependency : dependencies) {
String artifactId = dependency.getArtifactId();
MavenModule submodule = findSubmodule(artifactId);
if (submodule == null) {
continue;
}
subModulesThatAreDependencies.add(submodule.getSelf());
submodule.getSubmodulesThatAreDependencies();
}
return subModulesThatAreDependencies;
}

/**
* This is delegated to {@link Model#equals(Object)}.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MavenModule that = (MavenModule) obj;
return self.equals(that.self);
}

@Override
public int hashCode() {
return self.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.algomaster99.terminator.commons.maven;

import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

public class MavenModuleDependencyGraph {
// create dependency graph of maven modules

public static MavenModule createMavenModuleGraph(Path projectRoot) throws IOException, XmlPullParserException {
Path rootPom = projectRoot.resolve("pom.xml");
MavenXpp3Reader reader = new MavenXpp3Reader();
Model rootModel = reader.read(new FileReader(rootPom.toFile()));

MavenModule root = new MavenModule(rootModel, projectRoot.toAbsolutePath(), null);

List<String> submodules = rootModel.getModules();

for (String module : submodules) {
Path modulePath = projectRoot.resolve(module);
MavenXpp3Reader moduleReader = new MavenXpp3Reader();
Model moduleModel = moduleReader.read(
new FileReader(modulePath.resolve("pom.xml").toFile()));
MavenModule mavenModule = new MavenModule(moduleModel, modulePath, root);
if (moduleModel.getModules() != null) {
List<String> childModules = moduleModel.getModules();
List<MavenModule> children = childModules.stream()
.map(childModule -> {
try {
return createMavenModuleGraph(modulePath.resolve(childModule));
} catch (IOException | XmlPullParserException e) {
throw new RuntimeException(e);
}
})
.toList();
children.forEach(mavenModule::addSubmodule);
}
root.addSubmodule(mavenModule);
}
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package io.github.algomaster99.terminator.commons.maven;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.jupiter.api.Test;

public class MavenModuleDependencyGraphTest {
@Test
void createMavenModuleGraph_singleModule() throws XmlPullParserException, IOException {
// arrange
Path projectRoot =
Path.of("src/test/resources/maven-modules/single-module").toAbsolutePath();

// act
MavenModule root = MavenModuleDependencyGraph.createMavenModuleGraph(projectRoot);

// assert
assertThat(root).isNotNull();

assertThat(root.getSelf().getArtifactId()).isEqualTo("single-module");
assertThat(root.getFileSystemPath()).isEqualTo(projectRoot);
assertThat(root.getSubmodules()).hasSize(0);
}

@Test
void createMavenModuleGraph_multiModule_singleDepth() throws XmlPullParserException, IOException {
// arrange
Path projectRoot = Path.of("src/test/resources/maven-modules/multi-module-single-depth")
.toAbsolutePath();

// act
MavenModule root = MavenModuleDependencyGraph.createMavenModuleGraph(projectRoot);

// assert
assertThat(root).isNotNull();

assertThat(root.getSelf().getArtifactId()).isEqualTo("multi-module-single-depth");
assertThat(root.getFileSystemPath()).isEqualTo(projectRoot);
assertThat(root.getSubmodules()).hasSize(2);

MavenModule module1 = root.getSubmodules().get(0);
MavenModule module2 = root.getSubmodules().get(1);

assertThat(Set.of(module1.getSelf().getArtifactId(), module2.getSelf().getArtifactId()))
.contains("m1", "m2");
assertThat(Set.of(module1.getFileSystemPath(), module2.getFileSystemPath()))
.contains(projectRoot.resolve("m1"), projectRoot.resolve("m2"));

assertThat(module1.getSubmodules()).hasSize(0);
assertThat(module2.getSubmodules()).hasSize(0);
}

@Test
void createMavenModuleGraph_multiModule_multipleDepth() throws XmlPullParserException, IOException {
// arrange
Path projectRoot = Path.of("src/test/resources/maven-modules/multi-module-multiple-depth")
.toAbsolutePath();

// act
MavenModule root = MavenModuleDependencyGraph.createMavenModuleGraph(projectRoot);

// assert
assertThat(root).isNotNull();

assertThat(root.getSelf().getArtifactId()).isEqualTo("multi-module-multiple-depth");
assertThat(root.getFileSystemPath()).isEqualTo(projectRoot);
assertThat(root.getSubmodules()).hasSize(2);

MavenModule module1 = root.getSubmodules().get(0);
MavenModule module2 = root.getSubmodules().get(1);

assertThat(Set.of(module1.getSelf().getArtifactId(), module2.getSelf().getArtifactId()))
.contains("m1", "m2");
assertThat(Set.of(module1.getFileSystemPath(), module2.getFileSystemPath()))
.contains(projectRoot.resolve("m1"), projectRoot.resolve("m2"));

assertThat(module1.getSubmodules()).hasSize(0);
assertThat(module2.getSubmodules()).hasSize(1);

MavenModule module21 = module2.getSubmodules().get(0);

assertThat(module21.getSelf().getArtifactId()).isEqualTo("m21");
assertThat(module21.getFileSystemPath())
.isEqualTo(projectRoot.resolve("m2").resolve("m21"));
assertThat(module21.getSubmodules()).hasSize(0);
}

@Test
void createMavenModuleGraph_submoduleAsDependency() throws XmlPullParserException, IOException {
// arrange
Path projectRoot = Path.of("src/test/resources/maven-modules/submodule-as-dependency")
.toAbsolutePath();

// act
MavenModule root = MavenModuleDependencyGraph.createMavenModuleGraph(projectRoot);

// assert
assertThat(root).isNotNull();

assertThat(root.getSelf().getArtifactId()).isEqualTo("submodule-as-dependency");
assertThat(root.getFileSystemPath()).isEqualTo(projectRoot);
assertThat(root.getSubmodules()).hasSize(2);

MavenModule module1 = root.getSubmodules().get(0);
MavenModule module2 = root.getSubmodules().get(1);

assertThat(module1.getSubmodulesThatAreDependencies()).isEqualTo(List.of(module2.getSelf()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.example</groupId>
<artifactId>multi-module-multiple-depth</artifactId>
<version>10.4.2</version>
</parent>

<artifactId>m1</artifactId>
<packaging>jar</packaging>
<version>10.4.2</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.example</groupId>
<artifactId>m2</artifactId>
<version>10.4.2</version>
</parent>

<artifactId>m21</artifactId>
<packaging>jar</packaging>
<version>10.4.2</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.example</groupId>
<artifactId>multi-module-multiple-depth</artifactId>
<version>10.4.2</version>
</parent>

<artifactId>m2</artifactId>
<packaging>pom</packaging>
<version>10.4.2</version>

<modules>
<module>m21</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>multi-module-multiple-depth</artifactId>
<packaging>pom</packaging>
<version>10.4.2</version>

<modules>
<module>m1</module>
<module>m2</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.example</groupId>
<artifactId>multi-module-single-depth</artifactId>
<version>10.4.2</version>
</parent>

<artifactId>m1</artifactId>
<packaging>jar</packaging>
<version>10.4.2</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.example</groupId>
<artifactId>multi-module-single-depth</artifactId>
<version>10.4.2</version>
</parent>

<artifactId>m2</artifactId>
<packaging>jar</packaging>
<version>10.4.2</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>multi-module-single-depth</artifactId>
<packaging>pom</packaging>
<version>10.4.2</version>

<modules>
<module>m1</module>
<module>m2</module>
</modules>
</project>
Loading

0 comments on commit e917911

Please sign in to comment.