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

4

6

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
      }
    }
  }],
}
Tidewater answered 25/6, 2020 at 0:48 Comment(0)
A
3

Edit June 2022

I managed to implement this and it’s now available in 1.68 https://code.visualstudio.com/updates/v1_68#_tasks

Anglian answered 23/1, 2022 at 12:15 Comment(0)
Z
9

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"
  },
Zelig answered 7/10, 2020 at 17:1 Comment(0)
P
5

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"
}]
Plage answered 25/6, 2020 at 1:41 Comment(3)
Is there any way that it will automatically understand java or python file executing ? I mean if i do as you have said it will prompt for selecting java or python !Tidewater
Not as of now, check this issue , you can create a shortcut for the task though, I've added how to do it in the ansPlage
your task doc link is to the v0.1.0 version (VSC<=1.13), we now use task v2.0.0 filesTwitter
A
3

Edit June 2022

I managed to implement this and it’s now available in 1.68 https://code.visualstudio.com/updates/v1_68#_tasks

Anglian answered 23/1, 2022 at 12:15 Comment(0)
P
1

As JaseW pointed out, support for glob patterns for default tasks was added in 1.68 (credz to you, JaseW), however the glob pattern described in the linked update notes (**.ext) only works for files that are in the root of the workspace. To match all files with a given extension, you should use **/*.ext:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo txt",
            "type": "shell",
            "command": "echo TextFile",
            "group": {
                "kind": "build",
                "isDefault": "**/*.txt" // This is a glob pattern which will only match when the active file has a .txt extension.
            }
        },
        {
            "label": "echo js",
            "type": "shell",
            "command": "echo JavascriptFile",
            "group": {
                "kind": "build",
                "isDefault": "**/*.js" // This is a glob pattern which will only match when the active file has a .js extension.
            },
        }
    ]
}
Phillip answered 17/8, 2023 at 7:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.