【问题标题】:Can I configure a task.json file for more then one language in vs code?我可以为 vs code 中的一种以上语言配置 task.json 文件吗?
【发布时间】:2026-01-20 12:45:01
【问题描述】:

我想在 VS Code 中配置一个tasks.json 文件来运行 python 和 java 代码只需按下:

  • Ctrl + Shift + B

Python 和 Java 已配置,但需要两个不同的 tasks.json 文件。

但我只能在.vscode 文件夹中保留一个tasks.json 文件。

如何将两个配置文件合并到一个 tasks.json 文件中?

对于 Python:

{
  "version": "2.0.0",
  "tasks": [{
    "label": "Compile and run",
    "type": "shell",
    "command": "",
    "args": [
      "/usr/bin/time",
      "-v",
      "--output",
      "sys.txt",
      "timeout",
      "5",
      "python3",
      "${relativeFile}",
      "<",
      "input.txt",
      ">",
      "output.txt",
    ],
    "group": {
      "kind": "build",
      "isDefault": true
    },
    "problemMatcher": {
      "owner": "py",
      "fileLocation": [
        "relative",
        "${workspaceRoot}"
      ],
      "pattern": {
        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
        "file": 1,
        "line": 2,
        "column": 3,
        "severity": 4,
        "message": 5
      }
    }
  }],


}

对于 Java :

{
  "version": "2.0.0",
  "tasks": [{
    "label": "Compile and run",
    "type": "shell",
    "command": "",
    "args": [
      "/usr/bin/time",
      "-v",
      "--output",
      "sys.txt",
      "timeout",
      "5",
      "java",
      "${relativeFile}",
      "<",
      "input.txt",
      ">",
      "output.txt",
    ],
    "group": {
      "kind": "build",
      "isDefault": true
    },
    "problemMatcher": {
      "owner": "java",
      "fileLocation": [
        "relative",
        "${workspaceRoot}"
      ],
      "pattern": {
        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
        "file": 1,
        "line": 2,
        "column": 3,
        "severity": 4,
        "message": 5
      }
    }
  }],
}

【问题讨论】:

    标签: json visual-studio-code vscode-settings vscode-tasks


    【解决方案1】:

    如果您打开了 java 或 python 文件(并且按照@tHeSID 的建议“合并”了两个任务),您可以重载键绑定 ala:

      {
        "key": "ctrl+shift+B",
        "command": "workbench.action.tasks.runTask",
        "args": "Compile and run Python",
        "when": "editorLangId == python"
      },
      {
        "key": "ctrl+shift+B",
        "command": "workbench.action.tasks.runTask",
        "args": "Compile and run Java",
        "when": "editorLangId == java"
      },
    

    【讨论】:

      【解决方案2】:

      很简单,您只需合并"tasks":[] 数组并唯一地命名您的任务。任务数组可以包含任意数量的任务对象,有些也可以相互依赖。 More info on VSCode Tasks

      在这里,当您使用它和CTRL + SHIFT + B 时,它会显示选择任务的选项。

      {
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "Compile and run Python",
                  "type": "shell",
                  "command": "",
                  "args": [
                      "/usr/bin/time",
                      "-v",
                      "--output",
                      "sys.txt",
                      "timeout",
                      "5",
                      "python3",
                      "${relativeFile}",
                      "<",
                      "input.txt",
                      ">",
                      "output.txt"
                  ],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  },
                  "problemMatcher": {
                      "owner": "py",
                      "fileLocation": ["relative", "${workspaceRoot}"],
                      "pattern": {
                          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                          "file": 1,
                          "line": 2,
                          "column": 3,
                          "severity": 4,
                          "message": 5
                      }
                  }
              },
              {
                  "label": "Compile and run Java",
                  "type": "shell",
                  "command": "",
                  "args": [
                      "/usr/bin/time",
                      "-v",
                      "--output",
                      "sys.txt",
                      "timeout",
                      "5",
                      "java",
                      "${relativeFile}",
                      "<",
                      "input.txt",
                      ">",
                      "output.txt"
                  ],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  },
                  "problemMatcher": {
                      "owner": "java",
                      "fileLocation": ["relative", "${workspaceRoot}"],
                      "pattern": {
                          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                          "file": 1,
                          "line": 2,
                          "column": 3,
                          "severity": 4,
                          "message": 5
                      }
                  }
              }
          ]
      }
      

      由于无法根据文件扩展名 (See Issue here) 告诉 VSCode 运行哪个任务。

      您始终可以为构建任务创建键盘快捷键并执行它,而无需从弹出窗口中选择它。例如,对于下面的tasks.json,您可以通过将其添加到您的keybindings.json 文件来创建快捷方式。

      [{
        "key": "ctrl+alt+h",
        "command": "workbench.action.tasks.runTask",
        "args": "Compile and run Python" // this text should match exactly with task "label"
      }]
      

      【讨论】:

      • 有什么方法可以自动理解 java 或 python 文件的执行?我的意思是如果我按照你说的做,它会提示选择 java 或 python!
      • 不是现在,check this issue,你可以为任务创建一个快捷方式,我已经在答案中添加了如何做
      • 您的任务文档链接是 v0.1.0 版本 (VSC
      【解决方案3】:

      万一其他人降落在这里。

      https://github.com/microsoft/vscode/issues/88106 正在解决这个问题

      【讨论】:

        最近更新 更多