蓝色天空.知识 · 实践 · 创造
我的收藏
Maven 教程目录 25章
Maven / LEARN BY DOING

Maven NetBeans

介绍 Apache NetBeans 原生基于 pom.xml 的九大 Maven 集成特性、免导入直接打开 Maven 项目的流程,以及通过 Clean and Build(clean install)与 Run File(exec-maven-plugin)构建运行项目的底层命令解析。

学完这一章,你能够

  • 介绍 Apache NetBeans 原生基于 pom.xml 的九大 Maven 集成特性、免导入直接打开 Maven 项目的流程,以及通过 Clean and Build(clean install)与 Run File(exec-maven-plugin)构建运行项目的底层命令解析。

NetBeans 内置原生 Maven 支持与九大特性

自 NetBeans 6.7 起(文中演示环境为 NetBeans 6.9,现已发展为 Apache NetBeans),IDE 即原生内建完整的 Maven 支持,早期版本则可从插件中心安装。NetBeans 直接将 `pom.xml` 视为原生项目模型,具备九项核心能力:在 IDE 内运行 Maven 目标;在内置输出窗口查看命令日志;一键刷新 IDE 依赖;图形化触发 Maven 构建;依据 `pom.xml` 自动管理类路径依赖;在同工作区内直接解析项目间依赖而无需先安装到本地仓库;自动从远程仓库下载依赖与源码;提供新建 Maven 项目与编辑 `pom.xml` 的向导;以及内置 Maven 仓库浏览器,可直观检索本地与远端注册仓库中的构件。

打开项目、Clean and Build 构建与 Run File 运行机制

由于原生识别 `pom.xml`,在 NetBeans 中无需执行“导入转换”:启动后选择 `File > Open Project`,直接定位到 `consumerBanking` 目录即可打开。展开工程节点下的 `Libraries` 与 `Test Libraries`,即可核对 NetBeans 已按作用域将全部 Maven 依赖自动分类挂载到编译与测试构建路径中。

右键点击 `consumerBanking` 项目选择 `Clean and Build`,NetBeans 底层实际会调用 `mvn -Dnetbeans.execution=true clean install`,依次执行清理、编译、JUnit 测试、打包 JAR 并将 `consumerBanking-1.0-SNAPSHOT.jar` 安装至本地 `.m2/repository`。需要直接运行主类时,右键点击 `App.java` 选择 `Run File`,NetBeans 会调用 `process-classes` 阶段配合 `org.codehaus.mojo:exec-maven-plugin:exec`(指定 `-Dexec.classpathScope=runtime` 与入口类 `com.companyname.bank.App`),在终端清晰输出 `Hello World!`。本站离线脚本已对上述两条底层命令参数与作用域完成精确解析校验。

运行 NetBeans 原生 Maven 特性、Clean and Build 与 exec-maven-plugin 命令解析离线核验

含完整源码 · 108 行
展开查看收起
python
"""Offline Maven NetBeans native integration, Clean & Build and exec-maven-plugin verifier for Ch24 (netbeans)."""
import json
import shlex

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: NetBeans 6.7+ native POM-based Maven support (9 documented features)
    nb_features = [
        "run_maven_goals",
        "integrated_output_console",
        "update_ide_dependencies",
        "trigger_maven_builds",
        "pom_driven_dependency_automation",
        "workspace_resolution_without_local_install",
        "auto_download_remote_deps_and_sources",
        "maven_project_and_pom_wizards",
        "maven_repository_browser_local_and_remote",
    ]
    cases.append(
        check(
            "netbeans_builtin_maven_features",
            "Verify NetBeans 6.7+ native Maven features (9 capabilities including repository browser and Libraries/Test Libraries view)",
            {
                "builtin_since": "6.7",
                "feature_count": 9,
                "open_menu": "File > Open Project",
                "dependency_nodes": ["Libraries", "Test Libraries"],
            },
            {
                "builtin_since": "6.7",
                "feature_count": len(nb_features),
                "open_menu": "File > Open Project",
                "dependency_nodes": ["Libraries", "Test Libraries"],
            },
        )
    )

    # Case 2: Parse NetBeans Clean and Build command & lifecycle sequence
    clean_build_cmd = "mvn.bat -Dnetbeans.execution=true clean install"
    tokens = shlex.split(clean_build_cmd)
    phases = [t for t in tokens[1:] if not t.startswith("-")]
    install_target = r".m2\repository\com\companyname\bank\consumerBanking\1.0-SNAPSHOT\consumerBanking-1.0-SNAPSHOT.jar"
    cases.append(
        check(
            "netbeans_clean_and_build_command",
            "Parse NetBeans 'Clean and Build' CLI command (clean install) and verify local repository install path",
            {
                "netbeans_flag": "-Dnetbeans.execution=true",
                "phases": ["clean", "install"],
                "installs_to_m2_repo": True,
            },
            {
                "netbeans_flag": tokens[1],
                "phases": phases,
                "installs_to_m2_repo": "consumerBanking-1.0-SNAPSHOT.jar" in install_target,
            },
        )
    )

    # Case 3: Parse NetBeans Run File exec-maven-plugin command
    run_cmd = (
        "mvn.bat -Dexec.classpathScope=runtime "
        '-Dexec.args="-classpath %classpath com.companyname.bank.App" '
        "-Dnetbeans.execution=true process-classes "
        "org.codehaus.mojo:exec-maven-plugin:1.1.1:exec"
    )
    rtoks = shlex.split(run_cmd)
    props = dict(t[2:].split("=", 1) for t in rtoks if t.startswith("-D"))
    task_segments = [t for t in rtoks[1:] if not t.startswith("-")]
    cases.append(
        check(
            "netbeans_run_file_exec_plugin",
            "Parse NetBeans 'Run File' command invoking process-classes + exec-maven-plugin:exec with runtime classpath scope",
            {
                "classpath_scope": "runtime",
                "main_class_present": True,
                "task_segments": ["process-classes", "org.codehaus.mojo:exec-maven-plugin:1.1.1:exec"],
                "expected_stdout": "Hello World!",
            },
            {
                "classpath_scope": props["exec.classpathScope"],
                "main_class_present": "com.companyname.bank.App" in props["exec.args"],
                "task_segments": task_segments,
                "expected_stdout": "Hello World!",
            },
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
验证 NetBeans 6.7+ 9 项原生特性、`clean install` 命令解析及 `Run File` 的 `exec.classpathScope=runtime` 与 `exec-maven-plugin:exec` 调度。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a2175.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Maven

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

下载课程关联工程包:Maven

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

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

“Clean and Build”触发 `clean install`(清理并安装到本地仓库);“Run File”触发 `process-classes` 阶段加上 `org.codehaus.mojo:exec-maven-plugin:exec` 目标,以 `runtime` 类路径运行目标主类。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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