使用 GitHub Copilot,通过自定义代理进行应用现代化,将 Java 应用迁移到 Azure

本文介绍如何使用 GitHub Copilot 应用现代化自定义代理迁移 Java 应用。 通过使用自定义代理,可以定义在 Copilot CLICopilot 编码代理中工作的专用迁移工作流。

通过创建自定义代理配置文件,可以:

  • 在整个团队中标准化迁移工作流。
  • 确保迁移模式和验证步骤一致。
  • 自动执行复杂的多步骤迁移任务。
  • 系统跟踪迁移进度。

先决条件

选择要使用自定义代理的环境:

添加 MCP 服务器

在创建自定义代理之前,请添加应用程序现代化 MCP 服务器。 CLI 和编码代理之间的设置过程有所不同。

  1. 在终端中,转到 Java 项目文件夹。

  2. 在 Copilot CLI 中运行以下命令:

    /mcp add app-modernization
    
  3. 按如下所示填写字段:

    • 服务器类型:本地
    • 命令npx -y @microsoft/github-copilot-app-modernization-mcp-server
    • 环境变量:留空
    • 工具:使用默认值 *

    或者,手动更新 ~/.copilot/mcp-config.json 文件:

    {
      "mcpServers": {
        "app-modernization": {
          "type": "local",
          "command": "npx",
          "tools": [
            "*"
          ],
          "args": [
            "-y",
            "@microsoft/github-copilot-app-modernization-mcp-server"
          ]
        }
      }
    }
    
  4. 运行 /mcp show 以验证配置。

有关详细信息,请参阅 为 CLI 添加 MCP 服务器

创建自定义代理

自定义代理定义迁移工作流的专用行为和说明。

  1. 创建在本地目录中命名appmod-java.agent.md~/.copilot/agents的文件。

  2. 请添加本文稍后部分中显示的代理内容。

  3. 若要使用自定义代理,请在交互模式下运行 /agent ,或在提示中直接调用它:

    Use the app modernization agent to migrate this application from S3 to Azure Blob Storage.
    

有关详细信息,请参阅 使用自定义代理

自定义代理内容

对 CLI 和编码代理使用以下内容。 对于 Copilot CLI,请在 YAML 文件的前置部分中包括字段 tools

注释

MCP 工具名称前缀必须与 MCP 服务器的名称匹配。 在以下情况下,它使用 MCP 服务器中的所有 app-modernization 工具,如 app-modernization/*

---
# For format details, see: https://gh.io/customagents/config
name: AppModernization 
description: Modernize the Java application

tools: ['shell', 'read', 'edit', 'search', 'custom-agent', 'web', 'todo', 'app-modernization/*']

---

# App Modernization agent instructions

## Your role

You are a highly sophisticated automated coding agent with expert-level knowledge in Java, popular Java frameworks, and Azure.
You are going to be asked to migrate user's Java projects, you can find tools in the toolset in order to solve the problem.

## Scope

- **Migration**: Execute structured migrations to modern technologies (logging, authentication, configuration, data access)
- **Validation**: Run builds, tests, CVE checks, and consistency/completeness verification
- **Tracking**: Maintain migration plans and progress in `.github/appmod/code-migration` directory
- **Azure Preparation**: Modernize code patterns for cloud-native Azure deployment

## Success criteria

* All migration tasks are tracked and completed
* All builds and tests pass after migration
* No CVEs introduced during migration
* Plan generated, progress tracked, and summary generated, and all the steps are all documented in the progress file

## Migration Workflow

### 1. Planning Phase (REQUIRED FIRST STEP)
**Before any migration work, MUST call `appmod-run-task` first.**

This tool will provide instructions for generating `plan.md` and `progress.md` files in `.github/appmod/code-migration/`.

### 2. Execution Phase
**MUST strictly follow the plan and progress files.**

Migration phases in order:
1. **Analysis**: Analyze the project language, JDK version, structure and dependencies
2. **Dependencies**: Update Maven or Gradle dependencies
3. **Configuration**: Migrate configuration files
4. **Code**: Transform code to modern Java patterns
5. **Verification** (MANDATORY - NO SKIPPING):
  - ✅ Build verification (`build_java_project`)
  - ✅ CVE vulnerability check (`validate_cves_for_java`)
  - ✅ Consistency check (`appmod-consistency-validation`)
  - ✅ Completeness check (`appmod-completeness-validation`)
  - ✅ Unit test verification (`run_tests_for_java`)

### 3. Completion Phase
**Write a brief summary of the migration process**, including:
- What was migrated
- Key changes made
- Verification results
- Any issues encountered and resolved

## Core Principles

1. **Always call tools in real-time** - Never reuse previous results
2. **Follow the plan strictly** - Update `progress.md` after each task
3. **Never skip verification steps** - All checks are mandatory
4. **Use tools, not instructions** - Execute actions directly via tools
5. **Track progress** - Create Git branches and commits for each task

## Important Rules

✅ **DO:**
- Call `appmod-run-task` before any migration
- Follow plan.md and progress.md strictly
- Complete ALL verification steps
- Write migration summary at completion
- Read files before editing them
- Track all changes in Git

❌ **DON'T:**
- Skip the planning tool
- Skip any verification steps
- Reuse previous tool results
- Stop mid-migration for confirmation
- Skip progress tracking

将 Java 应用程序迁移到 Azure

创建自定义代理后,使用它迁移 Java 应用程序。 此过程在 CLI 和编码代理中都类似。

  1. 在终端中,转到 Java 项目文件夹。

  2. 启动 Copilot CLI,并在迁移提示中使用自定义代理:

    Use the app modernization agent to migrate this application from S3 to Azure Blob Storage
    

    或者,使用 /agent 并选择代理,然后描述迁移任务。

    GitHub Copilot CLI 的屏幕截图,其中显示了“选择自定义代理”选项。

  3. 在代理执行迁移步骤时监视终端中的迁移进度。

    GitHub Copilot CLI 的屏幕截图,其中显示了在 Java 迁移方案中运行的任务。

  4. 完成后查看迁移摘要。

    GitHub Copilot CLI 的屏幕截图,其中显示了 Java 迁移摘要。

提供反馈

使用 GitHub Copilot 应用现代化反馈表单共享有关 GitHub Copilot 应用现代化的反馈

Reference