"""Offline Maven 3 lifecycles, 23 default phases & antrun execution binding verifier for Ch06 (build-life-cycle)."""
import json
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Maven POM/lifecycle/dependency verifier (mvn CLI absent)"
CLEAN_PHASES = ["pre-clean", "clean", "post-clean"]
DEFAULT_PHASES = [
"validate",
"initialize",
"generate-sources",
"process-sources",
"generate-resources",
"process-resources",
"compile",
"process-classes",
"generate-test-sources",
"process-test-sources",
"generate-test-resources",
"process-test-resources",
"test-compile",
"process-test-classes",
"test",
"prepare-package",
"package",
"pre-integration-test",
"integration-test",
"post-integration-test",
"verify",
"install",
"deploy",
]
SITE_PHASES = ["pre-site", "site", "post-site", "site-deploy"]
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def phases_up_to(lifecycle_phases, target):
idx = lifecycle_phases.index(target)
return lifecycle_phases[: idx + 1]
def main():
cases = []
# Case 1: Clean lifecycle phases & cumulative prefix execution
clean_bindings = {"pre-clean": None, "clean": "maven-clean-plugin:clean", "post-clean": None}
cases.append(
check(
"clean_lifecycle_phases",
"Verify clean lifecycle 3 phases, default plugin binding, and prefix execution for clean vs post-clean",
{
"clean_run": ["pre-clean", "clean"],
"post_clean_run": ["pre-clean", "clean", "post-clean"],
"clean_default_goal": "maven-clean-plugin:clean",
},
{
"clean_run": phases_up_to(CLEAN_PHASES, "clean"),
"post_clean_run": phases_up_to(CLEAN_PHASES, "post-clean"),
"clean_default_goal": clean_bindings["clean"],
},
)
)
# Case 2: Default (build) lifecycle 23 phases & milestone ordering
milestones = ["validate", "compile", "test", "package", "verify", "install", "deploy"]
milestone_indices = [DEFAULT_PHASES.index(m) for m in milestones]
cases.append(
check(
"default_lifecycle_23_phases",
"Verify exact count (23) and strict ordering of default lifecycle phases and 7 key milestones",
{
"total_phases": 23,
"milestones_monotonic": True,
"compile_prefix_len": 7,
"package_includes_test": True,
"deploy_is_last": True,
},
{
"total_phases": len(DEFAULT_PHASES),
"milestones_monotonic": milestone_indices == sorted(milestone_indices),
"compile_prefix_len": len(phases_up_to(DEFAULT_PHASES, "compile")),
"package_includes_test": "test" in phases_up_to(DEFAULT_PHASES, "package"),
"deploy_is_last": DEFAULT_PHASES[-1] == "deploy",
},
)
)
# Case 3: Site lifecycle 4 phases & default plugin bindings
site_bindings = {
"pre-site": None,
"site": "maven-site-plugin:site",
"post-site": None,
"site-deploy": "maven-site-plugin:deploy",
}
cases.append(
check(
"site_lifecycle_phases",
"Verify site lifecycle 4 phases and default maven-site-plugin bindings",
{
"site_run": ["pre-site", "site"],
"site_deploy_run": ["pre-site", "site", "post-site", "site-deploy"],
"bound_goals": ["maven-site-plugin:site", "maven-site-plugin:deploy"],
},
{
"site_run": phases_up_to(SITE_PHASES, "site"),
"site_deploy_run": phases_up_to(SITE_PHASES, "site-deploy"),
"bound_goals": [site_bindings["site"], site_bindings["site-deploy"]],
},
)
)
# Case 4: Mixed CLI invocation (phases + standalone plugin goals)
def expand_cli(tokens):
plan = []
for tok in tokens:
if ":" in tok:
plan.append(f"goal:{tok}")
elif tok in CLEAN_PHASES:
plan.extend(f"clean:{p}" for p in phases_up_to(CLEAN_PHASES, tok))
elif tok in DEFAULT_PHASES:
plan.extend(f"default:{p}" for p in phases_up_to(DEFAULT_PHASES, tok))
elif tok in SITE_PHASES:
plan.extend(f"site:{p}" for p in phases_up_to(SITE_PHASES, tok))
return plan
mixed = expand_cli(["clean", "dependency:copy-dependencies", "package"])
clean_deploy = expand_cli(["clean", "deploy"])
cases.append(
check(
"mixed_phase_and_goal_dispatch",
"Verify ordered execution of 'mvn clean dependency:copy-dependencies package' and 'mvn clean deploy'",
{
"first_three": ["clean:pre-clean", "clean:clean", "goal:dependency:copy-dependencies"],
"last_step": "default:package",
"clean_deploy_steps": 25,
},
{
"first_three": mixed[:3],
"last_step": mixed[-1],
"clean_deploy_steps": len(clean_deploy),
},
)
)
# Case 5: Parse maven-antrun-plugin executions & verify triggered echoes for mvn compile / mvn site
pom_build_antrun = """
<project>
<build><plugins><plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution><id>id.validate</id><phase>validate</phase><goals><goal>run</goal></goals><configuration><tasks><echo>validate phase</echo></tasks></configuration></execution>
<execution><id>id.compile</id><phase>compile</phase><goals><goal>run</goal></goals><configuration><tasks><echo>compile phase</echo></tasks></configuration></execution>
<execution><id>id.test</id><phase>test</phase><goals><goal>run</goal></goals><configuration><tasks><echo>test phase</echo></tasks></configuration></execution>
<execution><id>id.package</id><phase>package</phase><goals><goal>run</goal></goals><configuration><tasks><echo>package phase</echo></tasks></configuration></execution>
<execution><id>id.deploy</id><phase>deploy</phase><goals><goal>run</goal></goals><configuration><tasks><echo>deploy phase</echo></tasks></configuration></execution>
</executions>
</plugin></plugins></build>
</project>
"""
root = ET.fromstring(pom_build_antrun)
by_phase = {
ex.findtext("phase"): ex.findtext("configuration/tasks/echo")
for ex in root.findall("build/plugins/plugin/executions/execution")
}
compile_echoes = [by_phase[p] for p in phases_up_to(DEFAULT_PHASES, "compile") if p in by_phase]
cases.append(
check(
"antrun_plugin_execution_bindings",
"Parse maven-antrun-plugin executions and verify 'mvn compile' triggers validate & compile only (not test/package/deploy)",
{
"total_declared_executions": 5,
"triggered_on_mvn_compile": ["validate phase", "compile phase"],
},
{
"total_declared_executions": len(by_phase),
"triggered_on_mvn_compile": compile_echoes,
},
)
)
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-life-cycle-lab/explore-build-life-cycle.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-build-life-cycle.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2177.zip
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2175.zip