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

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

我想在 VS Code 中配置一个 tasks.json 文件来运行 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
          }
        }
      }],
    
    
    }
    

    对于 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
          }
        }
      }],
    }
    

    最佳答案

    很简单,您只需合并 "tasks":[] 数组并唯一地命名您的任务。任务数组可以包含任意数量的任务对象,有些也可以相互依赖。 More info on VSCode Tasks
    在这里,当您使用 this 和 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"
    }]
    

    关于json - 我可以在 vs 代码中为一种以上的语言配置 task.json 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62566196/

    相关文章:

    visual-studio-code - 通过导航选项卡打开文件时,如何在 VSCode Explorer 面板中禁用文件自动对焦?

    python - 在 VS Code 中,我不想让 Black 格式化我的 settings.json

    json - Dart- 'String'类型不是 'Iterable<dynamic>'类型的子类型

    c# - 当 json 中的 id 属性充当对象时创建 POCO

    python - 对具有随机异常的字典列表进行排序(不排序)

    c++ - VSCode c++ task.json包含路径和库

    sass - 如何在 VS Code 中更改注释格式

    python - 可以从命名空间对象生成自动完成吗?

    json - 当我不知道字典的键时,如何从 JSON 解码字典?

    visual-studio-code - 如何使用 JavaScript/Nodejs 在 VSCode 中显示警告、错误、函数、变量