VS Code allows you to define your own tasks to run external tools so you don’t need to run them by typing into the Terminal.

For example, you could create custom tasks for your assignments to:

  • Run your tests,
  • Run your code,
  • Run the code style tool,
  • Or any other tool you want to run!

This will then enable you to run these commands with simple shortcuts, or even with a single click.


Install the “Tasks” extension

This isn’t essential for running Tasks, but it will allow you to run the Tasks with a single click.

Search for “Tasks” in the VS Code Extensions tab, and install the extension:

Edit


Setting up your project with a tasks.json file

Check if you have a .vscode/tasks.json file in your project.

If you don’t, you can download the one we have created for you (see the download button below).

Alternatively, you can create one from scratch if you want to define your own tasks:

TerminalConfigure Tasks ⇨ scroll to Create tasks.json file from templateOthers

This will create you a tasks.json file within the .vscode folder, with a sample echo task:

Edit

Here’s how it will look when you execute it:

Edit

Click the button in the status bar to execute it!


Defining tasks useful for the Assignments

We have created a sample tasks.json file for you to use in your projects.

Once you download it, place it inside your .vscode folder.

Download ‘tasks.json’ file


You will then see the status bar update to show these tasks:

Edit

The tasks are:

  • run program:
    • Runs ./mvnw clean compile exec:java@run for Unix/Mac OS or .\mvnw.cmd clean compile exec:java@run for Windows
  • run tests:
    • Runs ./mvnw clean test for Unix/Mac OS or .\mvnw.cmd clean test for Windows
  • run style tool:
    • Runs ./mvnw clean compile exec:java@style for Unix/Mac OS or .\mvnw.cmd clean compile exec:java@style for Windows

Done. Happy clicking!


Don’t want to use your mouse?

If you don’t want to use your mouse, you can also run these tasks with shortcuts.

The Build Task

The run program task is set up to be the default build task, and already has the following keyboard shortcut:

  • Windows and GNU/Linux: Ctrl + Shift + B
  • macOS: B

The Test Task

The run tests task is set up to be the default test task, but you need to set up a keyboard shortcut for it:

  1. Open the Command Palette (Ctrl + Shift + P on Windows and GNU/Linux, P on macOS)

Edit

  1. Search for Tasks: Run Test Task:

Edit

  1. Double-click the Keybinding field:

Edit

  1. Type in your desired keyboard shortcut. For example:
  • Windows and GNU/Linux: Ctrl + Shift + T
  • macOS: T

Edit

Your keyboard shortcut is now set up:

Edit

Other Tasks

If you want to run any other task defined in your tasks.json file, you can use the Run Task command:

TerminalRun Taskrun program (or any other task)

Alternatively, you can also stick to using keyboard shortcuts:

  • Windows and GNU/Linux: Ctrl + Shift + PTasks: Run Taskrun program (or any other task)

  • macOS: PTasks: Run Taskrun program (or any other task)

See the steps below:

You might need to type the first few letters of “tasks” to narrow down the list of commands:

Edit

Then select the task you want to run:

Edit


More information

The official VS Code documentation on Tasks has more information: https://code.visualstudio.com/docs/editor/tasks.

The following video also has a good overview of how to use Tasks:




More VS Code guides

Check out the other VS Code guide pages: