Can I configure a task.json file for more then one language in vs code?

Viewed 2030

I want to configure a tasks.json file in VS Code to run python and java code just pressing:

  • Ctrl + Shift + B

Python and Java are configured but it needs two different tasks.json files.

But I can just keep one tasks.json file in the .vscode folder.

How can I merge two config file in a tasks.json file ?

For 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
      }
    }
  }],


}

For 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
      }
    }
  }],
}
3 Answers

If you have the java or python file open (and the two tasks "merged" as @tHeSID suggested), you can just overload the keybindings 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"
  },

Its simple, you just merge the "tasks":[] array and name your tasks uniquely. The tasks array can contain any number of Tasks Objects, some can be dependent on each other as well. More info on VSCode Tasks

Here, when you use this and CTRL + SHIFT + B it will show you the option to select a task.

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

Since there's no way to tell VSCode which task to run base on file extension (See Issue here).

You can always create a keyboard shortcut for your build task and execute it without having to select it from the pop up. For example, for the below tasks.json you can create a shortcut by adding this to your keybindings.json file.

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