简介
本指南展示如何使用 Visual Studio Code Web 客户端在 GitHub Codespaces 中设置示例 Java 项目。它将一步步引导你打开代码空间、添加以及修改预定义的开发容器配置。
完成本教程后,你将能够使用 VS Code Web 客户端或 VS Code 桌面应用程序,将 dev 容器配置添加到自己的仓库中。
有关 dev 容器的更多信息,请参阅 dev 容器简介。
步骤 1:在代码空间中打开项目
-
如果尚未登录,请登录 GitHub.com。
-
点击 Use this template(使用此模板),然后点击 Open in a codespace(在代码空间中打开)。

创建代码空间时,你的项目会在专为你分配的远程虚拟机上创建。默认情况下,代码空间的容器已预装多种语言和运行时,包括 Java,并且还包含常用工具集,如 Gradle、Maven、git、wget、rsync、openssh 和 nano。
你可以通过调整 vCPU 与 RAM 的配额、添加 dotfiles 来个性化环境,或修改已安装的工具和脚本来自定义代码空间。更多信息请参见 自定义你的代码空间。
GitHub Codespaces 使用名为 devcontainer.json 的文件来配置在代码空间中工作的开发容器。每个仓库可以包含一个或多个 devcontainer.json 文件,以提供恰好满足代码在代码空间中工作所需的开发环境。
启动时,GitHub Codespaces 会读取 devcontainer.json 文件及其所有依赖文件,安装所需的工具和运行时,并执行项目所需的其他设置任务。更多信息请参见 dev 容器简介。
步骤 2:添加 dev 容器配置
GitHub Codespaces 的默认开发容器(即 “dev container”)可以让你顺利开展类似 vscode-remote-try-java 的 Java 项目。不过,我们建议你自行配置开发容器,加入项目所需的全部工具和脚本,以确保仓库中所有 GitHub Codespaces 用户都能获得完全可复现的环境。
要让仓库使用自定义 dev 容器,需要创建一个或多个 devcontainer.json 文件。你可以在 Visual Studio Code 中从预定义的配置模板添加,也可以自行编写。有关 dev 容器配置的更多信息,请参见 dev 容器简介。
-
打开 Visual Studio Code 命令面板(Shift+Command+P / Ctrl+Shift+P),开始输入 “add dev”。点击 Codespaces: Add Dev Container Configuration Files。

-
点击 创建新配置。
-
在本示例中,你创建代码空间时所使用的模板仓库已经包含了 dev 容器配置,因此会显示一条信息提示配置文件已存在。我们将覆盖现有配置文件,请点击 继续。
-
点击 显示所有定义。

-
输入
java并点击 Java 选项。如果项目使用特定工具,还可以选择其他选项,例如 Java & PostgreSQL。
-
选择项目要使用的 Java 版本。本例中请选择标记为 “(default)” 的版本。

-
勾选 Install Maven 选项并点击 OK。

-
会显示可安装的附加功能列表。这里我们将安装 Ant——用于构建应用的 Java 库和命令行工具。要安装此功能,输入
ant,选择Ant (via SDKMAN),随后点击 OK。
-
系统提示 dev 容器配置文件已存在。点击 覆盖。
已创建
devcontainer.json文件,并在编辑器中打开。
自定义 dev 容器配置的详细信息
在 Visual Studio Code 资源管理器中,你会看到项目根目录下新增了一个 .devcontainer 目录,里面包含 devcontainer.json 文件。这是从该仓库创建的代码空间的主要配置文件。
devcontainer.json
你刚添加的 devcontainer.json 将包含 name、image 与 features 等属性的值。文件中还包含了一些可能有用但已被注释掉的额外属性。
文件内容大致如下,具体取决于你选择的镜像。
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/java
{
"name": "Java",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.net.cn/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/java:0-17",
"features": {
"ghcr.io/devcontainers/features/java:1": {
"version": "none",
"installMaven": "true",
"installGradle": "false"
},
"ghcr.io/devcontainers-contrib/features/ant-sdkman:2": {}
}
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "java -version",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
- name: 你可以随意为 dev 容器命名,系统会提供一个默认值。
- image: 用于创建代码空间 dev 容器的容器镜像名称,来源于容器注册表(DockerHub、GitHub Container Registry 或 Azure Container Registry)。
- features: 一个或多个对象的列表,每个对象引用可用的 dev 容器功能之一。Features 是自包含、可共享的安装代码和容器配置单元,能够为你的开发容器轻松添加更多工具、运行时或库。你可以在 VS Code 中或在 GitHub 上的
devcontainer.json编辑器中添加功能。更多信息请查看 Visual Studio Code 或 Web browser 选项卡 中的 “在 devcontainer.json 文件中添加功能”。 - forwardPorts: 此处列出的所有端口都会自动转发。更多信息请参见 代码空间端口转发。
- postCreateCommand: 使用此属性在代码空间创建后运行命令。它可以是字符串(如上所示)、数组或对象。更多信息请参阅 Development Containers 网站上的 dev containers 规范。
- customizations: 此属性允许在代码空间中使用特定工具或服务时进行自定义,例如为 VS Code 配置特定设置和扩展。更多信息请参阅 Development Containers 网站上的 Supporting tools and services。
- remoteUser: 默认情况下,你以 vscode 用户身份运行;如有需要,也可以将其设置为 root。
完整的可用属性列表请参见 Development Containers 网站上的 dev containers 规范。
其它 dev 容器配置文件
如果你熟悉 Docker,除了 devcontainer.json,还可以使用 Dockerfile 或 Docker Compose 来配置代码空间环境,只需将 Dockerfile 或 compose.yaml 与 devcontainer.json 放在同一目录下即可。更多信息请参考 Development Containers 网站上的 使用镜像、Dockerfile 与 Docker Compose。
步骤 3:修改你的 devcontainer.json 文件
在添加了 dev 容器配置并基本了解各属性作用后,你现在可以进一步自定义环境。本示例将添加以下属性:
- 在 dev 容器创建完成后,运行一条命令来生成新文件。
- 在此代码空间中自动安装两个 VS Code 扩展。
-
在
devcontainer.json文件中,在features属性后添加一个逗号。JSON "features": { "ghcr.io/devcontainers/features/java:1": { "version": "none", "installMaven": "true", "installGradle": "false" }, "ghcr.io/devcontainers-contrib/features/ant-sdkman:2": {} },"features": { "ghcr.io/devcontainers/features/java:1": { "version": "none", "installMaven": "true", "installGradle": "false" }, "ghcr.io/devcontainers-contrib/features/ant-sdkman:2": {} }, -
取消注释
postCreateCommand属性,并将其值改为echo \"This file was added by the postCreateCommand.\" > TEMP.md。JSONC // Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "echo \"This file was added by the postCreateCommand.\" > TEMP.md",
// Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "echo \"This file was added by the postCreateCommand.\" > TEMP.md", -
取消注释
customizations属性,并按以下方式编辑,以安装 “Code Spell Checker” 扩展和 “Extension Pack for Java”。JSONC // Configure tool-specific properties. "customizations": { // Configure properties specific to VS Code. "vscode": { // Add the IDs of extensions you want installed when the container is created. "extensions": [ "streetsidesoftware.code-spell-checker", "vscjava.vscode-java-pack" ] } }// Configure tool-specific properties. "customizations": { // Configure properties specific to VS Code. "vscode": { // Add the IDs of extensions you want installed when the container is created. "extensions": [ "streetsidesoftware.code-spell-checker", "vscjava.vscode-java-pack" ] } }
此时 devcontainer.json 文件应类似如下,具体取决于你选择的镜像。
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/java
{
"name": "Java",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.net.cn/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/java:0-17",
"features": {
"ghcr.io/devcontainers/features/java:1": {
"version": "none",
"installMaven": "true",
"installGradle": "false"
},
"ghcr.io/devcontainers-contrib/features/ant-sdkman:2": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "echo \"This file was added by the postCreateCommand.\" > TEMP.md",
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"streetsidesoftware.code-spell-checker",
"vscjava.vscode-java-pack"
]
}
}
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
-
保存更改。
-
打开 VS Code 命令面板(Shift+Command+P / Ctrl+Shift+P),输入 “rebuild”。点击 Codespaces: Rebuild Container。

提示
有时你可能需要执行完整的重建,以清除缓存并使用全新镜像重新构建容器。更多信息请参见 代码空间中的容器重建。在代码空间内部进行重建可以确保更改在提交到仓库前如预期般工作。如果出现故障,系统会将你置于带有恢复容器的代码空间,你可以从中重新构建以继续调整容器。
重新构建 dev 容器并等待代码空间再次可用后,
postCreateCommand将被执行,生成TEMP.md文件,同时这两个扩展也会可供使用。
步骤 4:运行你的应用程序
-
按下
F5运行应用程序。 -
如果 VS Code 右下角弹出 “toast” 通知,询问是否切换到标准模式,请点击 Yes。

-
项目文件导入完成后,点击 Debug Console 选项卡查看程序输出。

步骤 5:提交你的更改
对代码空间进行更改后(无论是新代码还是配置),你都应提交这些更改。将配置提交到仓库可以确保以后任何人在此仓库创建代码空间时都能获得相同的配置。你所做的任何自定义(例如添加 VS Code 扩展)都将对所有用户生效。
本教程中,你是从模板仓库创建的代码空间,因此代码尚未存储在仓库中。你可以通过将当前分支发布到 GitHub 来创建仓库。
相关信息请参阅 在代码空间中使用源代码管理。
后续步骤
现在,你应该能够为自己的 Java 项目添加自定义的开发容器配置。
以下是一些面向更高级场景的补充资源。