"""Offline Maven build, test, JAR packaging & multi-class refactor verifier for Ch14 (build-test-project)."""
import json
import tempfile
import xml.etree.ElementTree as ET
import zipfile
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 consumerBanking pom.xml & JUnit 3.8.1 test dependency
pom_xml = """
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.bank</groupId>
<artifactId>consumerBanking</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>consumerBanking</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
ns = {"m": "http://maven.apache.org/POM/4.0.0"}
root = ET.fromstring(pom_xml)
dep = root.find("m:dependencies/m:dependency", ns)
cases.append(
check(
"consumer_banking_pom_and_junit",
"Parse consumerBanking pom.xml verifying JAR packaging, 1.0-SNAPSHOT version, and JUnit 3.8.1 test scope",
{
"gav": "com.companyname.bank:consumerBanking:1.0-SNAPSHOT",
"packaging": "jar",
"junit_dep": "junit:junit:3.8.1:test",
},
{
"gav": f"{root.findtext('m:groupId', namespaces=ns)}:{root.findtext('m:artifactId', namespaces=ns)}:{root.findtext('m:version', namespaces=ns)}",
"packaging": root.findtext("m:packaging", namespaces=ns),
"junit_dep": f"{dep.findtext('m:groupId', namespaces=ns)}:{dep.findtext('m:artifactId', namespaces=ns)}:{dep.findtext('m:version', namespaces=ns)}:{dep.findtext('m:scope', namespaces=ns)}",
},
)
)
# Case 2: Simulate mvn clean package (clean target -> compile -> test/surefire -> JAR archive)
with tempfile.TemporaryDirectory() as tmp:
proj = Path(tmp) / "consumerBanking"
target = proj / "target"
(target / "stale").mkdir(parents=True)
(target / "stale" / "old.txt").write_text("stale", encoding="utf-8")
# clean
for p in sorted(target.rglob("*"), reverse=True):
p.unlink() if p.is_file() else p.rmdir()
stale_removed = not (target / "stale").exists()
# compile & test-compile & surefire report
cls_dir = target / "classes/com/companyname/bank"
tcls_dir = target / "test-classes/com/companyname/bank"
rep_dir = target / "surefire-reports"
cls_dir.mkdir(parents=True)
tcls_dir.mkdir(parents=True)
rep_dir.mkdir(parents=True)
(cls_dir / "App.class").write_bytes(b"\xca\xfe\xba\xbe")
(tcls_dir / "AppTest.class").write_bytes(b"\xca\xfe\xba\xbe")
(rep_dir / "com.companyname.bank.AppTest.txt").write_text("Tests run: 1, Failures: 0\n", encoding="utf-8")
jar_file = target / "consumerBanking-1.0-SNAPSHOT.jar"
with zipfile.ZipFile(jar_file, "w") as zf:
zf.writestr("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n")
zf.write(cls_dir / "App.class", "com/companyname/bank/App.class")
with zipfile.ZipFile(jar_file, "r") as zf:
entries = sorted(zf.namelist())
report_exists = (rep_dir / "com.companyname.bank.AppTest.txt").is_file()
cases.append(
check(
"clean_package_pipeline_and_jar",
"Simulate mvn clean package clearing target/, generating surefire-reports, and packaging consumerBanking-1.0-SNAPSHOT.jar",
{
"stale_removed": True,
"report_exists": True,
"jar_name": "consumerBanking-1.0-SNAPSHOT.jar",
"jar_entries": ["META-INF/MANIFEST.MF", "com/companyname/bank/App.class"],
},
{
"stale_removed": stale_removed,
"report_exists": report_exists,
"jar_name": jar_file.name,
"jar_entries": entries,
},
)
)
# Case 3: Add Util.java and update App.java in same package com.companyname.bank
util_java = (
"package com.companyname.bank;\n"
"public class Util {\n"
" public static void printMessage(String message) { System.out.println(message); }\n"
"}\n"
)
app_v2 = (
"package com.companyname.bank;\n"
"public class App {\n"
' public static void main(String[] args) { Util.printMessage("Hello World!"); }\n'
"}\n"
)
cases.append(
check(
"add_util_class_and_refactor_app",
"Verify Util.printMessage and App.main share package com.companyname.bank without extra import",
{
"same_package": True,
"calls_util_printMessage": True,
"expected_stdout": "Hello World!",
},
{
"same_package": "package com.companyname.bank;" in util_java and "package com.companyname.bank;" in app_v2,
"calls_util_printMessage": 'Util.printMessage("Hello World!")' in app_v2,
"expected_stdout": "Hello World!",
},
)
)
# Case 4: Classpath execution from target/classes (java -cp . com.companyname.bank.App)
cases.append(
check(
"compile_and_java_cp_invocation",
"Verify mvn clean compile target directory and java -cp . FQN resolution",
{
"compile_cmd": "mvn clean compile",
"classes_root": "target/classes",
"fqn_main": "com.companyname.bank.App",
"class_rel_path": "com/companyname/bank/App.class",
},
{
"compile_cmd": "mvn clean compile",
"classes_root": "target/classes",
"fqn_main": "com.companyname.bank.App",
"class_rel_path": "com.companyname.bank.App".replace(".", "/") + ".class",
},
)
)
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-test-project-lab/explore-build-test-project.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-build-test-project.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2179.zip
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2175.zip