我可以在vs代码中为一种以上的语言配置task.json文件吗?

时间:2020-06-25 00:48:33

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

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

  • Ctrl + Shift + B

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

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

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

2 个答案:

答案 0 :(得分:3)

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

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

Tasks option

{
    "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"
}]

答案 1 :(得分: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"
  },