Claude Code + IntelliJ IDEA 集成指南(Windows + WSL)

Claude Code + IntelliJ IDEA 集成指南(Windows + WSL)

目录

  1. 环境要求
  2. 安装方式
  3. 配置步骤
  4. 使用工作流
  5. 常见问题
  6. 最佳实践

环境要求

必需组件

  • ✅ Windows 10/11
  • ✅ WSL2(已安装)
  • ✅ IntelliJ IDEA(Community 或 Ultimate)
  • ⚠️ Node.js 18+ (需要安装)
  • ⚠️ Git(推荐)

检查现有环境

# 在 WSL 终端中检查
wsl --version
node --version  # 如果未安装,请看下面的安装步骤
git --version

安装方式

方案一:WSL 中安装 Claude Code(推荐)

1.1 在 WSL 中安装 Node.js

# 打开 WSL 终端(Windows Terminal 或 PowerShell 输入 wsl)

# 使用 nvm 安装 Node.js(推荐)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# 重新加载配置
source ~/.bashrc

# 安装 Node.js LTS 版本
nvm install --lts
nvm use --lts

# 验证安装
node --version
npm --version

1.2 安装 Claude Code CLI

# 全局安装 Claude Code
npm install -g @anthropic-ai/claude-code

# 验证安装
claude --version

# 初次运行配置
claude

1.3 配置 API Key

首次运行时会提示输入 Anthropic API Key:

# 或者手动设置环境变量
echo 'export ANTHROPIC_API_KEY="your-api-key-here"' >> ~/.bashrc
source ~/.bashrc

方案二:Windows 原生安装(简单但功能可能受限)

2.1 在 Windows PowerShell 中安装

# 以管理员身份运行 PowerShell

# 安装 Node.js(如果未安装)
# 下载安装器:https://nodejs.org/

# 安装 Claude Code
npm install -g @anthropic-ai/claude-code

# 验证
claude --version

2.2 配置环境变量

# 设置 API Key(PowerShell)
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY', 'your-api-key-here', 'User')

# 或者在用户环境变量中手动添加
# 控制面板 → 系统 → 高级系统设置 → 环境变量

配置步骤

步骤 1:配置 IDEA 终端使用 WSL

选项 A:IDEA 终端默认使用 WSL

  1. 打开 IDEA
  2. 导航到:File → Settings(或 Ctrl + Alt + S
  3. 选择:Tools → Terminal
  4. 设置 Shell path 为:
    wsl.exe
    
    或指定具体发行版:
    wsl.exe -d Ubuntu
    
  5. 点击 ApplyOK
  6. 重启 IDEA 终端(关闭并重新打开终端标签页)

选项 B:保持 PowerShell,手动切换到 WSL

  • 在 IDEA 终端中输入 wsl 即可进入 WSL 环境

步骤 2:配置项目路径访问

WSL 访问 Windows 文件系统

# Windows 驱动器挂载在 /mnt/ 下
# F:\WORK-PROJECTS\task-supervison 对应:
cd /mnt/f/WORK-PROJECTS/task-supervison

# 验证路径
pwd
ls -la

创建软链接(可选,便于快速访问)

# 在 WSL home 目录创建链接
ln -s /mnt/f/WORK-PROJECTS/task-supervison ~/task-supervison

# 之后可以快速访问
cd ~/task-supervison

步骤 3:初始化项目 Claude Code 配置

# 进入项目目录
cd /mnt/f/WORK-PROJECTS/task-supervison

# 启动 Claude Code
claude

# 这会创建 .claude/ 目录和配置文件

步骤 4:配置 .claude/settings.json(可选)

{
  "outputStyle": "concise",
  "autoCommit": false,
  "hooks": {
    "user-prompt-submit": null
  }
}

使用工作流

工作流 1:代码审查和优化

在 IDEA 中:

  1. 编辑代码文件
  2. 使用 Git 提交前准备

在 IDEA 终端(WSL)中:

# 启动 Claude Code
claude

# 示例命令:
# 1. 审查当前修改
"Review the changes in BusinessUserService.java"

# 2. 解释复杂方法
"Explain the logic in BusinessUserService.java:50-80"

# 3. 优化代码
"Refactor the method at BusinessUserService.java:100 to improve readability"

工作流 2:生成单元测试

claude

# 在 Claude Code 中
"Generate unit tests for BusinessUserService using JUnit 5 and Mockito"

工作流 3:修复 Bug

claude

# 描述问题
"There's a NullPointerException in BusinessUserService.java:150. Help me fix it."

工作流 4:代码重构

claude

# 重构请求
"Refactor the feedback module to follow clean code principles"

常见问题

Q1: WSL 中找不到 node 或 npm 命令

# 重新安装 Node.js
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

# 或使用 nvm(推荐)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install --lts

Q2: IDEA 终端无法运行 WSL

# 检查 WSL 是否正常
# 在 PowerShell 中:
wsl --list --verbose

# 更新 WSL
wsl --update

# 设置默认版本为 WSL2
wsl --set-default-version 2

Q3: Claude Code 提示权限错误

# 检查文件权限
ls -la /mnt/f/WORK-PROJECTS/task-supervison

# 如果需要,修改权限
chmod -R 755 /mnt/f/WORK-PROJECTS/task-supervison

Q4: 中文字符显示乱码

IDEA 设置:

  1. Settings → Editor → File Encodings
  2. 设置所有编码为 UTF-8

WSL 设置:

# 在 ~/.bashrc 中添加
echo 'export LANG=zh_CN.UTF-8' >> ~/.bashrc
echo 'export LC_ALL=zh_CN.UTF-8' >> ~/.bashrc
source ~/.bashrc

Q5: Maven 命令在 WSL 中找不到

# 在 WSL 中安装 Maven
sudo apt update
sudo apt install maven -y

# 验证
mvn --version

Q6: Git 凭证在 WSL 和 Windows 之间不同步

# 配置 WSL 使用 Windows 的 Git 凭证管理器
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe"

最佳实践

1. 文件编辑协同

  • 在 IDEA 中编辑:日常代码编写、重构
  • 在 Claude Code 中处理
    • 批量重构
    • 生成测试代码
    • 代码审查
    • 文档生成

2. 版本控制集成

# Claude Code 可以帮助生成提交信息
claude

# 命令示例:
"Review my staged changes and suggest a commit message"
"Create a commit for the changes in BusinessUserService.java"

3. 项目特定配置

.claude/settings.json 中配置:

{
  "outputStyle": "detailed",
  "autoCommit": false,
  "contextFiles": [
    "CLAUDE.md",
    "pom.xml",
    "src/main/resources/application.yml"
  ]
}

4. 快捷命令别名(可选)

在 WSL 的 ~/.bashrc 中添加:

# 快速进入项目目录并启动 Claude
alias tscode='cd /mnt/f/WORK-PROJECTS/task-supervison && claude'

# 快速查看项目状态
alias tsstatus='cd /mnt/f/WORK-PROJECTS/task-supervison && git status'

# 重载配置
source ~/.bashrc

使用:

# 直接启动
tscode

5. IDEA 与 Claude Code 协同开发流程

┌─────────────────────────────────────────────┐
│  典型开发流程                                │
├─────────────────────────────────────────────┤
│  1. IDEA: 打开项目,阅读现有代码            │
│  2. IDEA 终端: 启动 claude                  │
│  3. Claude Code: 分析代码,提出改进建议     │
│  4. IDEA: 手动审查并应用建议的修改          │
│  5. IDEA 终端: 运行 mvn test / mvn package  │
│  6. Claude Code: 如有错误,协助修复         │
│  7. IDEA Git: 提交代码                      │
│  8. Claude Code: 生成 PR 描述(可选)       │
└─────────────────────────────────────────────┘

6. 性能优化建议

  • 大型文件编辑:在 IDEA 中编辑更快
  • 批量操作:使用 Claude Code
  • 实时预览:使用 IDEA
  • 代码生成:使用 Claude Code

针对本项目的特定配置

项目信息

  • 路径: F:\WORK-PROJECTS\task-supervison
  • 类型: Spring Boot 2.2.4 + Maven
  • 数据库: 人大金仓 (Kingbase8)
  • 端口: 6017

推荐的 Claude Code 使用场景

1. 服务层代码生成

claude

# 命令:
"Generate a new service for handling user notifications following the pattern in BusinessUserService.java"

2. Mapper 接口生成

"Create a MyBatis Plus mapper for the new NotificationEntity"

3. 定时任务开发

"Generate an XXL-JOB task for sending daily reports following the pattern in job/feedback/"

4. API 文档完善

"Add comprehensive Swagger annotations to the controller in controller/BusinessUserController.java"

5. 代码重构

"Refactor BusinessUserService.java to improve separation of concerns"

快速开始清单


获取帮助

Claude Code 官方资源

# 命令行帮助
claude --help

# 查看文档
# https://docs.claude.com/claude-code

项目相关

  • 参考项目根目录的 CLAUDE.md 文件
  • 查看 .claude/settings.json 配置

附录:完整命令速查表

WSL 基础命令

# 进入项目
cd /mnt/f/WORK-PROJECTS/task-supervison

# 列出文件
ls -la

# 查看文件内容
cat src/main/java/com/zkml/tasksupervison/service/BusinessUserService.java

# 搜索代码
grep -r "BusinessUserService" src/

Maven 命令(在项目根目录)

# 清理编译
mvn clean compile

# 运行测试
mvn test

# 打包
mvn clean package

# 跳过测试打包
mvn clean package -DskipTests

# 运行应用
mvn spring-boot:run

Git 命令

# 查看状态
git status

# 查看差异
git diff

# 暂存文件
git add .

# 提交
git commit -m "描述"

# 推送
git push origin develop-nanjing

Claude Code 常用命令

# 启动
claude

# 在 Claude Code 中的典型请求:
"Review current changes"
"Explain this code: [粘贴代码]"
"Generate tests for BusinessUserService"
"Fix the bug in line 150"
"Refactor this method to be more readable"
"Add comprehensive comments to this class"

文档版本: 1.0
更新日期: 2025-10-29
适用环境: Windows 10/11 + WSL2 + IntelliJ IDEA + Claude Code CLI

posted @ 2025-10-29 14:32  你在谁的风景里a  阅读(7050)  评论(0)    收藏  举报