"""Offline Maven CLI syntax, phase/goal parser, test filter & reactor flags verifier for Ch08 (commands)."""
import json
import shlex
VERSION = "CPython 3.12.14 offline Maven POM/lifecycle/dependency verifier (mvn CLI absent)"
PHASES = {
"clean",
"validate",
"compile",
"test-compile",
"test",
"package",
"verify",
"install",
"deploy",
"site",
}
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def parse_mvn(cmd):
tokens = shlex.split(cmd)
assert tokens[0] == "mvn"
opts, props, phases, goals = [], {}, [], []
i = 1
while i < len(tokens):
tok = tokens[i]
if tok.startswith("-D"):
kv = tok[2:]
if "=" in kv:
k, v = kv.split("=", 1)
props[k] = v
else:
props[kv] = "true"
elif tok in ("-pl", "-rf", "-T", "-P"):
opts.append((tok, tokens[i + 1]))
i += 1
elif tok.startswith("-"):
opts.append((tok, True))
elif ":" in tok:
goals.append(tok)
elif tok in PHASES:
phases.append(tok)
i += 1
return {"opts": opts, "props": props, "phases": phases, "goals": goals}
def main():
cases = []
# Case 1: CLI syntax & lifecycle command combinations
p_combo = parse_mvn("mvn clean compile test package")
p_ver = parse_mvn("mvn -v")
p_site = parse_mvn("mvn site")
cases.append(
check(
"cli_syntax_and_lifecycle_commands",
"Parse mvn [options] [phases] [goals] across -v, site, and clean/compile/test/package combinations",
{
"combo_phases": ["clean", "compile", "test", "package"],
"version_flag": [("-v", True)],
"site_phase": ["site"],
},
{
"combo_phases": p_combo["phases"],
"version_flag": p_ver["opts"],
"site_phase": p_site["phases"],
},
)
)
# Case 2: Dependency, help, versions & archetype plugin goals
goal_cmds = [
"mvn dependency:tree",
"mvn dependency:sources",
"mvn dependency:analyze",
"mvn dependency:copy-dependencies",
"mvn dependency:purge-local-repository",
"mvn versions:display-dependency-updates",
"mvn help:describe -Dplugin=compiler",
"mvn help:effective-pom",
"mvn compiler:compile",
"mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-web-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false",
]
parsed_goals = [parse_mvn(c)["goals"][0] for c in goal_cmds]
cases.append(
check(
"dependency_and_plugin_commands",
"Parse 10 plugin:goal commands (dependency, versions, help, compiler, archetype)",
{
"goal_count": 10,
"plugins": ["archetype", "compiler", "dependency", "help", "versions"],
"webapp_archetype": "maven-archetype-webapp",
},
{
"goal_count": len(parsed_goals),
"plugins": sorted({g.split(":")[0] for g in parsed_goals}),
"webapp_archetype": parse_mvn(goal_cmds[-1])["props"]["archetypeArtifactId"],
},
)
)
# Case 3: Test filtering (-Dtest=Class#method) and skip flags (-DskipTests, checkstyle/pmd)
t_method = parse_mvn("mvn -Dtest=UserServiceTest#testCreate test")
t_skip = parse_mvn("mvn clean package -DskipTests")
t_lint = parse_mvn("mvn install -Dcheckstyle.skip=true -Dpmd.skip=true")
cls_name, method_name = t_method["props"]["test"].split("#", 1)
cases.append(
check(
"test_filter_and_skip_flags",
"Parse -Dtest=Class#method filter, -DskipTests, and checkstyle/pmd skip properties",
{
"test_class": "UserServiceTest",
"test_method": "testCreate",
"skip_tests": "true",
"lint_skips": {"checkstyle.skip": "true", "pmd.skip": "true"},
},
{
"test_class": cls_name,
"test_method": method_name,
"skip_tests": t_skip["props"]["skipTests"],
"lint_skips": t_lint["props"],
},
)
)
# Case 4: Multi-module reactor flags (-pl, -am, -rf)
reactor_order = ["common", "dao", "service", "web"]
deps = {"common": [], "dao": ["common"], "service": ["dao"], "web": ["service"]}
def select_modules(pl_target, also_make=False, resume_from=None):
if resume_from:
return reactor_order[reactor_order.index(resume_from) :]
selected = {pl_target}
if also_make:
stack = [pl_target]
while stack:
cur = stack.pop()
for d in deps[cur]:
if d not in selected:
selected.add(d)
stack.append(d)
return [m for m in reactor_order if m in selected]
cases.append(
check(
"multimodule_reactor_flags",
"Verify -pl, -am -pl, and -rf reactor selection over common -> dao -> service -> web",
{
"pl_service": ["service"],
"am_pl_web": ["common", "dao", "service", "web"],
"rf_service": ["service", "web"],
},
{
"pl_service": select_modules("service", also_make=False),
"am_pl_web": select_modules("web", also_make=True),
"rf_service": select_modules(None, resume_from="service"),
},
)
)
# Case 5: Advanced flags (-T 4, -o, -X, -U, -P production, --batch-mode, release:prepare/perform)
p_adv = parse_mvn("mvn -T 4 -o -X -U -P production --batch-mode clean install")
opt_dict = dict(p_adv["opts"])
p_rel = [parse_mvn("mvn release:prepare")["goals"][0], parse_mvn("mvn release:perform")["goals"][0]]
cases.append(
check(
"advanced_runtime_flags",
"Parse parallel (-T 4), offline (-o), debug (-X), force-update (-U), profile (-P), batch-mode, and release goals",
{
"threads": "4",
"offline": True,
"debug": True,
"force_update": True,
"profile": "production",
"batch_mode": True,
"release_goals": ["release:prepare", "release:perform"],
},
{
"threads": opt_dict["-T"],
"offline": opt_dict["-o"],
"debug": opt_dict["-X"],
"force_update": opt_dict["-U"],
"profile": opt_dict["-P"],
"batch_mode": opt_dict["--batch-mode"],
"release_goals": p_rel,
},
)
)
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-commands-lab/explore-commands.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-commands.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2180.zip
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2175.zip