Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<module>spring-modulith-bom</module>
<module>spring-modulith-core</module>
<module>spring-modulith-docs</module>
<module>spring-modulith-maven-plugin</module>
<module>spring-modulith-events</module>
<module>spring-modulith-junit</module>
<module>spring-modulith-moments</module>
Expand Down
120 changes: 120 additions & 0 deletions spring-modulith-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith</artifactId>
<version>2.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>spring-modulith-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<name>Spring Modulith - Maven Plugin</name>
<description>Maven plugin and site report support for Spring Modulith documentation.</description>

<properties>
<module.name>spring.modulith.maven.plugin</module.name>
<maven.api.version>3.8.6</maven.api.version>
<maven.reporting.version>3.1.0</maven.reporting.version>
<maven.plugin.tools.version>3.15.2</maven.plugin.tools.version>
<plantuml.version>1.2024.8</plantuml.version>
</properties>

<dependencies>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-modulith-docs</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>net.sourceforge.plantuml</groupId>
<artifactId>plantuml</artifactId>
<version>${plantuml.version}</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven.plugin.tools.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-api</artifactId>
<version>${maven.reporting.version}</version>
</dependency>

<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-impl</artifactId>
<version>${maven.reporting.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven.plugin.tools.version}</version>
<configuration>
<goalPrefix>modulith</goalPrefix>
</configuration>
<executions>
<execution>
<id>default-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>



Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.maven;

interface DiagramRenderer {

RenderedDiagram render(String source);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright 2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.maven;

import java.io.File;
import java.net.URLClassLoader;
import java.util.Locale;

import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.docs.Documenter.DiagramOptions.DiagramStyle;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

@Mojo(name = "report", defaultPhase = LifecyclePhase.SITE, threadSafe = true,
requiresDependencyResolution = ResolutionScope.RUNTIME)
@Execute(phase = LifecyclePhase.COMPILE)
public class ModulithReportMojo extends AbstractMavenReport {

@Component
private Renderer siteRenderer;

@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;

@Parameter(property = "spring.modulith.report.skip", defaultValue = "false")
private boolean skip;

@Parameter(property = "spring.modulith.report.applicationClassName")
private String applicationClassName;

@Parameter(property = "spring.modulith.report.basePackage")
private String basePackage;

@Parameter(property = "spring.modulith.report.name", defaultValue = "Spring Modulith Report")
private String siteReportName;

@Parameter(property = "spring.modulith.report.description", defaultValue = "Application module dependency diagrams generated from Spring Modulith metadata")
private String siteReportDescription;

@Parameter(property = "spring.modulith.report.outputdir", defaultValue = "modulith")
private String siteReportDirectory;

@Parameter(property = "spring.modulith.report.renderDiagrams", defaultValue = "true")
private boolean renderDiagrams;

@Parameter(property = "spring.modulith.report.diagramStyle", defaultValue = "C4")
private DiagramStyle diagramStyle;

private final ProjectClassLoaderFactory classLoaderFactory = new ProjectClassLoaderFactory();
private final ModulithSiteReportGenerator generator = new ModulithSiteReportGenerator(new PlantUmlSvgRenderer());

@Override
public String getOutputName() {
return siteReportDirectory + File.separator + "index";
}

@Override
public String getName(Locale locale) {
return siteReportName;
}

@Override
public String getDescription(Locale locale) {
return siteReportDescription;
}

@Override
protected Renderer getSiteRenderer() {
return siteRenderer;
}

@Override
protected String getOutputDirectory() {
return getReportOutputDirectory().getAbsolutePath();
}

@Override
protected MavenProject getProject() {
return project;
}

@Override
protected void executeReport(Locale locale) throws MavenReportException {

validateConfiguration();

try (URLClassLoader classLoader = classLoaderFactory.create(project)) {

var original = Thread.currentThread().getContextClassLoader();

try {
Thread.currentThread().setContextClassLoader(classLoader);

var modules = createApplicationModules(classLoader);
var outputDirectory = getReportOutputDirectory().toPath().resolve(siteReportDirectory);
generator.generate(modules, outputDirectory, diagramStyle, renderDiagrams);

} finally {
Thread.currentThread().setContextClassLoader(original);
}

} catch (Exception ex) {
throw new MavenReportException("Failed to generate Spring Modulith site report", ex);
}
}

@Override
public boolean canGenerateReport() {
return !skip;
}

@Override
public boolean isExternalReport() {
return true;
}

private ApplicationModules createApplicationModules(ClassLoader classLoader) throws ClassNotFoundException {

if (applicationClassName != null && !applicationClassName.isBlank()) {
Class<?> applicationType = ClassUtils.forName(applicationClassName, classLoader);
return ApplicationModules.of(applicationType);
}

Assert.hasText(basePackage, "Either applicationClassName or basePackage must be configured!");
return ApplicationModules.of(basePackage);
}

private void validateConfiguration() {

boolean hasApplicationClass = applicationClassName != null && !applicationClassName.isBlank();
boolean hasBasePackage = basePackage != null && !basePackage.isBlank();

if (hasApplicationClass == hasBasePackage) {
throw new IllegalArgumentException(
"Configure exactly one of spring.modulith.report.applicationClassName or spring.modulith.report.basePackage.");
}
}
}

Loading