Maven 项目文档
详解使用 maven-site-plugin 与 maven-project-info-reports-plugin 生成项目报告站点、排查 Doxia DocumentContent 版本冲突异常,以及 Doxia 文档引擎支持的 Apt、Xdoc、FML 与 XHTML 四种源码格式。
学完这一章,你能够
- 详解使用 maven-site-plugin 与 maven-project-info-reports-plugin 生成项目报告站点、排查 Doxia DocumentContent 版本冲突异常,以及 Doxia 文档引擎支持的 Apt、Xdoc、FML 与 XHTML 四种源码格式。
配置站点插件并执行 mvn site 生成项目文档
Maven 不仅能编译打包,还可通过 `site` 生命周期自动汇总项目元数据并生成美观的静态 HTML 项目文档站点。以 `consumerBanking` 项目为例,执行文档生成前建议先在 `pom.xml` 的 `<build><pluginManagement>` 中将 `maven-site-plugin` 升级锁定至 `3.3` 及以上版本,并搭配 `maven-project-info-reports-plugin`(如 `2.7` 及以上),否则在 Maven 3 环境下可能因旧版站点插件与新版 Doxia 渲染器不兼容而抛出 `java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentContent` 错误。
配置完成后在项目目录执行 `mvn site`,Maven 会依次生成 About、Issue Tracking、Project Team、Dependencies、Continuous Integration、Source Repository、Project License、Mailing Lists、Plugin Management 与 Project Summary 等十类标准项目报告。构建成功后,打开 `target/site/index.html` 即可在浏览器中浏览完整的项目文档站点。
Doxia 文档处理引擎与支持的源码格式
Maven 站点生成底层依托名为 Doxia 的文档处理引擎:它能够将开发者按多种轻量或结构化标记语言编写的手工文档统一解析为抽象文档模型,再渲染成一致风格的站点网页。常见支持的源码格式包括纯文本式的 Apt、兼容 Maven 1.x 的 Xdoc、专用于常见问答的 FML 以及可扩展 HTML 文档 XHTML(详见下方格式参考表;新版 Doxia 亦支持 Markdown)。本站离线验证脚本已对插件版本门禁、10 类标准报告清单及 APT 到 HTML 转换流程完成核验。
Doxia 文档引擎常用输入格式对照表
| 格式名 | 特点与适用场景 | 官方 / 规范参考链接 |
|---|---|---|
| Apt(Almost Plain Text) | 类 Wiki 的纯文本轻量级文档格式,默认置于 `src/site/apt/`。 | http://maven.apache.org/doxia/references/apt-format.html |
| Xdoc | 沿用自 Maven 1.x 的 XML 结构化站点文档格式,默认置于 `src/site/xdoc/`。 | http://jakarta.apache.org/site/jakarta-site2.html |
| FML(FAQ Markup Language) | 专门用于编写问答列表(FAQ)的 XML 格式,默认置于 `src/site/fml/`。 | http://maven.apache.org/doxia/references/fml-format.html |
| XHTML | 遵循 XML 语法规范的可扩展 HTML 文档格式,默认置于 `src/site/xhtml/`。 | http://en.wikipedia.org/wiki/XHTML |
运行 Maven Site 插件版本校验、10 类项目报告清单与 Doxia APT 转换离线核验
"""Offline Maven site plugin, project info reports & Doxia document formats verifier for Ch17 (project-documents)."""
import json
import tempfile
import xml.etree.ElementTree as ET
from pathlib import Path
VERSION = "CPython 3.12.14 offline Maven POM/lifecycle/dependency verifier (mvn CLI absent)"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def parse_version_tuple(v):
return tuple(int(part) for part in v.split("."))
def main():
cases = []
# Case 1: pluginManagement validation for maven-site-plugin >= 3.3 & project-info-reports-plugin
pom_xml = """
<project>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
"""
root = ET.fromstring(pom_xml)
plugins = {
p.findtext("artifactId"): p.findtext("version")
for p in root.findall("build/pluginManagement/plugins/plugin")
}
site_ver = plugins["maven-site-plugin"]
cases.append(
check(
"site_plugin_management_check",
"Verify maven-site-plugin >= 3.3 (preventing Doxia DocumentContent NoClassDefFoundError) and project-info-reports-plugin",
{
"site_plugin_version": "3.3",
"avoids_doxia_noclassdef": True,
"reports_plugin_version": "2.7",
},
{
"site_plugin_version": site_ver,
"avoids_doxia_noclassdef": parse_version_tuple(site_ver) >= (3, 3),
"reports_plugin_version": plugins["maven-project-info-reports-plugin"],
},
)
)
# Case 2: Standard Project Info Reports generated under target/site/
reports = [
"About",
"Issue Tracking",
"Project Team",
"Dependencies",
"Continuous Integration",
"Source Repository",
"Project License",
"Mailing Lists",
"Plugin Management",
"Project Summary",
]
cases.append(
check(
"site_generation_reports_inventory",
"Verify the 10 standard Maven site reports and target/site/index.html output path",
{"report_count": 10, "output_index": "target/site/index.html", "includes_dependencies_report": True},
{"report_count": len(reports), "output_index": "target/site/index.html", "includes_dependencies_report": "Dependencies" in reports},
)
)
# Case 3: Doxia engine 4 documented markup formats
doxia_formats = {
"Apt": {"dir": "src/site/apt", "ext": ".apt", "purpose": "plain text"},
"Xdoc": {"dir": "src/site/xdoc", "ext": ".xml", "purpose": "Maven 1.x XML"},
"FML": {"dir": "src/site/fml", "ext": ".fml", "purpose": "FAQ"},
"XHTML": {"dir": "src/site/xhtml", "ext": ".xhtml", "purpose": "Extensible HTML"},
}
cases.append(
check(
"doxia_markup_formats_table",
"Verify Doxia document engine's 4 documented markup formats (Apt, Xdoc, FML, XHTML) and source dirs",
{"formats": ["Apt", "FML", "XHTML", "Xdoc"], "fml_dir": "src/site/fml"},
{"formats": sorted(doxia_formats.keys()), "fml_dir": doxia_formats["FML"]["dir"]},
)
)
# Case 4: Offline APT-to-HTML render check writing target/site/index.html
apt_sample = " -----\n Consumer Banking\n -----\n\nOverview\n\n Standard banking module documentation.\n"
lines = [ln.strip() for ln in apt_sample.splitlines() if ln.strip() and not ln.strip().startswith("-----")]
html_doc = f"<html><head><title>{lines[0]}</title></head><body><h1>{lines[1]}</h1><p>{lines[2]}</p></body></html>"
with tempfile.TemporaryDirectory() as tmp:
out_file = Path(tmp) / "target/site/index.html"
out_file.parent.mkdir(parents=True)
out_file.write_text(html_doc, encoding="utf-8")
rendered = out_file.read_text(encoding="utf-8")
cases.append(
check(
"doxia_apt_to_html_render_sim",
"Verify APT-style heading/paragraph transformation into target/site/index.html",
{"has_title": True, "has_h1_overview": True},
{"has_title": "<title>Consumer Banking</title>" in rendered, "has_h1_overview": "<h1>Overview</h1>" in rendered},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 验证 `maven-site-plugin >= 3.3` 版本门禁、10 类标准站点报告、Doxia 4 种源码格式目录映射及 APT 转 `target/site/index.html` 输出。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:748af22809e84f1e2ab9da624167b976627eef798c3c6e03939cacb26696e982
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:f6d3055d54f70035d4e1bbf48c315d4d026d8e563df3493315d8489f6199f53c
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 Maven 3 中运行 `mvn site` 时若遇到 `java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentContent`,常见原因与解决方法是什么?
展开参考解答
通常是 Super POM 或项目继承了过旧版本的 `maven-site-plugin`(2.x 系列),在 `pom.xml` 的 `<build><pluginManagement>` 中显式升级到 `maven-site-plugin` 3.3 及以上版本并配套升级 `maven-project-info-reports-plugin` 即可解决。