蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Eclipse m2eclipse 特性、.project/.classpath 元数据生成与 Maven package 执行流离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-eclipse.py
explore-eclipse.py123 行
"""Offline Maven Eclipse (m2eclipse) metadata, classpath & build log verifier for Ch23 (eclipse)."""
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: m2eclipse 9 core capabilities & workspace dependency resolution
    m2e_features = [
        "run_maven_goals",
        "integrated_console_output",
        "update_maven_dependencies",
        "build_maven_projects",
        "pom_driven_dependency_management",
        "workspace_resolution_without_local_install",
        "auto_download_dependencies_and_sources",
        "wizards_for_project_pom_and_enable_maven",
        "fast_remote_repo_artifact_search",
    ]
    workspace_projects = {"consumerBanking", "shared-utils"}
    can_resolve_in_workspace = "shared-utils" in workspace_projects
    cases.append(
        check(
            "m2eclipse_capabilities_and_workspace_resolution",
            "Verify m2eclipse 9 integration capabilities and same-workspace sibling resolution without mvn install",
            {"feature_count": 9, "workspace_resolution_ok": True, "preferences_menu": "Windows > Preferences"},
            {"feature_count": len(m2e_features), "workspace_resolution_ok": can_resolve_in_workspace, "preferences_menu": "Windows > Preferences"},
        )
    )

    # Case 2: Generate & verify Eclipse .project (maven2Nature) and .classpath (MAVEN2_CLASSPATH_CONTAINER)
    project_xml = """
    <projectDescription>
      <name>consumerBanking</name>
      <buildSpec>
        <buildCommand><name>org.eclipse.jdt.core.javabuilder</name></buildCommand>
        <buildCommand><name>org.eclipse.m2e.core.maven2Builder</name></buildCommand>
      </buildSpec>
      <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
      </natures>
    </projectDescription>
    """
    classpath_xml = """
    <classpath>
      <classpathentry kind="src" output="target/classes" path="src/main/java"/>
      <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
      <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
      <classpathentry kind="output" path="target/classes"/>
    </classpath>
    """
    pr = ET.fromstring(project_xml)
    cp = ET.fromstring(classpath_xml)
    natures = [el.text.strip() for el in pr.findall("natures/nature")]
    src_paths = [el.attrib["path"] for el in cp.findall("classpathentry[@kind='src']")]
    has_m2e_con = any(el.attrib.get("path") == "org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" for el in cp.findall("classpathentry[@kind='con']"))
    cases.append(
        check(
            "eclipse_import_and_classpath_generation",
            "Verify Eclipse .project maven2Nature and .classpath src/main/java, src/test/java, and MAVEN2_CLASSPATH_CONTAINER",
            {
                "project_name": "consumerBanking",
                "has_maven2_nature": True,
                "src_paths": ["src/main/java", "src/test/java"],
                "has_m2e_container": True,
            },
            {
                "project_name": pr.findtext("name"),
                "has_maven2_nature": "org.eclipse.m2e.core.maven2Nature" in natures,
                "src_paths": src_paths,
                "has_m2e_container": has_m2e_con,
            },
        )
    )

    # Case 3: Verify Maven package phase trace & Run As Java Application output
    package_steps = [
        "resources:resources",
        "compiler:compile",
        "resources:testResources",
        "compiler:testCompile",
        "surefire:test",
        "jar:jar",
    ]
    cases.append(
        check(
            "eclipse_maven_package_and_run_log",
            "Verify Eclipse Run As -> Maven package 6-goal execution trace and Run As Java Application stdout",
            {
                "artifact_id": "com.companyname.bank:consumerBanking:jar:1.0-SNAPSHOT",
                "package_goal_count": 6,
                "ends_with_jar": True,
                "app_stdout": "Hello World!",
            },
            {
                "artifact_id": "com.companyname.bank:consumerBanking:jar:1.0-SNAPSHOT",
                "package_goal_count": len(package_steps),
                "ends_with_jar": package_steps[-1] == "jar:jar",
                "app_stdout": "Hello World!",
            },
        )
    )

    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-eclipse-lab/explore-eclipse.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-eclipse.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Maven

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2175.zip

下载课程关联工程包:Maven

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2184.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习