我可以在 VS Code 中为多种语言配置一个 task.json 文件吗?

Meh*_*fat 6 json visual-studio-code vscode-tasks

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

  • Ctrl + Shift + B

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

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

如何将两个配置文件合并到一个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
      }
    }
  }],


}
Run Code Online (Sandbox Code Playgroud)

对于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
      }
    }
  }],
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 9

如果您打开了 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"
  },
Run Code Online (Sandbox Code Playgroud)


tHe*_*SiD 5

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

在这里,当您使用它时,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
                }
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

由于无法根据文件扩展名告诉 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"
}]
Run Code Online (Sandbox Code Playgroud)


Jas*_*seW 3

编辑 2022 年 6 月

\n

我设法实现了这个,它\xe2\x80\x99s现在在1.68中可用https://code.visualstudio.com/updates/v1_68#_tasks

\n