"""Offline Maven Java project archetype scaffold verifier for Ch13 (creating-project)."""
import json
import shlex
import tempfile
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 main():
cases = []
# Case 1: Parse archetype:generate CLI arguments & package directory conversion
cmd = (
'mvn archetype:generate "-DgroupId=com.companyname.bank" '
'"-DartifactId=consumerBanking" '
'"-DarchetypeArtifactId=maven-archetype-quickstart" '
'"-DinteractiveMode=false"'
)
tokens = shlex.split(cmd)
props = dict(tok[2:].split("=", 1) for tok in tokens if tok.startswith("-D"))
pkg_dir = props["groupId"].replace(".", "/")
cases.append(
check(
"archetype_cli_and_package_path",
"Parse quickstart archetype CLI arguments for consumerBanking and compute Java package directory",
{
"groupId": "com.companyname.bank",
"artifactId": "consumerBanking",
"archetype": "maven-archetype-quickstart",
"interactiveMode": "false",
"package_dir": "com/companyname/bank",
},
{
"groupId": props["groupId"],
"artifactId": props["artifactId"],
"archetype": props["archetypeArtifactId"],
"interactiveMode": props["interactiveMode"],
"package_dir": pkg_dir,
},
)
)
# Case 2: Generate & verify 4-entry directory structure (including manually added src/main/resources)
app_java = (
"package com.companyname.bank;\n"
"public class App {\n"
' public static void main(String[] args) { System.out.println("Hello World!"); }\n'
"}\n"
)
app_test_java = (
"package com.companyname.bank;\n"
"import junit.framework.Test;\n"
"import junit.framework.TestCase;\n"
"import junit.framework.TestSuite;\n"
"public class AppTest extends TestCase {\n"
" public AppTest(String testName) { super(testName); }\n"
" public static Test suite() { return new TestSuite(AppTest.class); }\n"
" public void testApp() { assertTrue(true); }\n"
"}\n"
)
with tempfile.TemporaryDirectory() as tmp:
proj = Path(tmp) / "consumerBanking"
main_pkg = proj / "src/main/java" / pkg_dir
test_pkg = proj / "src/test/java" / pkg_dir
res_dir = proj / "src/main/resources"
main_pkg.mkdir(parents=True)
test_pkg.mkdir(parents=True)
res_dir.mkdir(parents=True)
(proj / "pom.xml").write_text("<project><modelVersion>4.0.0</modelVersion></project>", encoding="utf-8")
(main_pkg / "App.java").write_text(app_java, encoding="utf-8")
(test_pkg / "AppTest.java").write_text(app_test_java, encoding="utf-8")
paths_ok = all(
p.exists()
for p in [
proj / "pom.xml",
main_pkg / "App.java",
test_pkg / "AppTest.java",
res_dir,
]
)
cases.append(
check(
"scaffold_directory_structure",
"Verify consumerBanking root pom.xml, src/main/java, src/test/java, and src/main/resources layout",
{"all_4_layout_entries_exist": True, "manual_resources_dir": "src/main/resources"},
{"all_4_layout_entries_exist": paths_ok, "manual_resources_dir": "src/main/resources"},
)
)
# Case 3: Validate App.java & JUnit 3 AppTest.java source contracts
cases.append(
check(
"app_and_junit3_test_source_check",
"Verify generated App.java main output and JUnit 3 AppTest extending TestCase with suite()/testApp()",
{
"app_package": "com.companyname.bank",
"app_prints_hello": True,
"junit3_extends_testcase": True,
"has_suite_and_testApp": True,
},
{
"app_package": "com.companyname.bank" if "package com.companyname.bank;" in app_java else "",
"app_prints_hello": '"Hello World!"' in app_java,
"junit3_extends_testcase": "extends TestCase" in app_test_java,
"has_suite_and_testApp": "public static Test suite()" in app_test_java and "public void testApp()" in app_test_java,
},
)
)
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-creating-project-lab/explore-creating-project.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-creating-project.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2181.zip
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2175.zip