"""Offline Maven build automation (maven-invoker-plugin & CI downstream triggers) verifier for Ch19 (build-automation)."""
import json
import xml.etree.ElementTree as ET
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 main():
cases = []
# Case 1: Parse upstream/downstream SNAPSHOT dependency graph
poms = {
"bus-core-api": """<project><artifactId>bus-core-api</artifactId><version>1.0-SNAPSHOT</version><dependencies/></project>""",
"app-web-ui": """<project><artifactId>app-web-ui</artifactId><version>1.0</version><dependencies><dependency><artifactId>bus-core-api</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>""",
"app-desktop-ui": """<project><artifactId>app-desktop-ui</artifactId><version>1.0</version><dependencies><dependency><artifactId>bus-core-api</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>""",
}
downstream = {"bus-core-api": []}
for name, xml_str in poms.items():
r = ET.fromstring(xml_str)
for dep in r.findall("dependencies/dependency"):
up = dep.findtext("artifactId")
if up in downstream:
downstream[up].append(name)
cases.append(
check(
"downstream_snapshot_dependency_graph",
"Parse bus-core-api, app-web-ui, and app-desktop-ui POMs to discover downstream SNAPSHOT consumers",
["app-desktop-ui", "app-web-ui"],
sorted(downstream["bus-core-api"]),
)
)
# Case 2: Parse maven-invoker-plugin <pomIncludes> sequential build chain
invoker_pom = """
<project>
<artifactId>bus-core-api</artifactId>
<build><plugins><plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.6</version>
<configuration>
<debug>true</debug>
<pomIncludes>
<pomInclude>app-web-ui/pom.xml</pomInclude>
<pomInclude>app-desktop-ui/pom.xml</pomInclude>
</pomIncludes>
</configuration>
<executions>
<execution><id>build</id><goals><goal>run</goal></goals></execution>
</executions>
</plugin></plugins></build>
</project>
"""
ir = ET.fromstring(invoker_pom)
includes = [el.text.strip() for el in ir.findall("build/plugins/plugin/configuration/pomIncludes/pomInclude")]
goal = ir.findtext("build/plugins/plugin/executions/execution/goals/goal")
exec_order = ["bus-core-api"] + [p.split("/")[0] for p in includes]
cases.append(
check(
"maven_invoker_plugin_post_build",
"Parse maven-invoker-plugin:1.6 pomIncludes and verify sequential build chain order",
{
"goal": "run",
"execution_order": ["bus-core-api", "app-web-ui", "app-desktop-ui"],
},
{"goal": goal, "execution_order": exec_order},
)
)
# Case 3: CI server (Hudson/Jenkins) automatic downstream job discovery when app-mobile-ui joins
ci_registry = dict(poms)
ci_registry["app-mobile-ui"] = """<project><artifactId>app-mobile-ui</artifactId><version>1.0</version><dependencies><dependency><artifactId>bus-core-api</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>"""
ci_triggered = sorted(
aid
for aid, xml_str in ci_registry.items()
if any(d.findtext("artifactId") == "bus-core-api" for d in ET.fromstring(xml_str).findall("dependencies/dependency"))
)
cases.append(
check(
"ci_server_auto_downstream_discovery",
"Simulate CI server auto-discovering new consumer app-mobile-ui without modifying bus-core-api/pom.xml",
["app-desktop-ui", "app-mobile-ui", "app-web-ui"],
ci_triggered,
)
)
# Case 4: Tradeoff comparison between invoker plugin & CI server
cases.append(
check(
"automation_tradeoff_comparison",
"Verify decoupling advantage of CI-driven dependency triggers vs static maven-invoker-plugin pomIncludes",
{"invoker_needs_upstream_pom_edit": True, "ci_needs_upstream_pom_edit": False},
{"invoker_needs_upstream_pom_edit": True, "ci_needs_upstream_pom_edit": False},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/maven-build-automation-lab/explore-build-automation.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-build-automation.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2175.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2176.zip