VS Code tasks
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:
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:
Terminal ⇨ Configure Tasks ⇨ scroll to Create tasks.json file from template ⇨ Others
This will create you a tasks.json
file within the .vscode
folder, with a sample echo
task:
Here’s how it will look when you execute it:
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.
You will then see the status bar update to show these tasks:
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
- Runs
run tests
:- Runs
./mvnw clean test
for Unix/Mac OS or.\mvnw.cmd clean test
for Windows
- Runs
run style tool
:- Runs
./mvnw clean compile exec:java@style
for Unix/Mac OS or.\mvnw.cmd clean compile exec:java@style
for Windows
- Runs
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:
- Open the Command Palette (Ctrl + Shift + P on Windows and GNU/Linux, ⇧ ⌘ P on macOS)
- Search for Tasks: Run Test Task:
- Double-click the Keybinding field:
- Type in your desired keyboard shortcut. For example:
- Windows and GNU/Linux: Ctrl + Shift + T
- macOS: ⇧ ⌘ T
Your keyboard shortcut is now set up:
Other Tasks
If you want to run any other task defined in your tasks.json
file, you can use the Run Task command:
Terminal ⇨ Run Task ⇨ run program (or any other task)
Alternatively, you can also stick to using keyboard shortcuts:
-
Windows and GNU/Linux: Ctrl + Shift + P ⇨ Tasks: Run Task ⇨ run program (or any other task)
-
macOS: ⇧ ⌘ P ⇨ Tasks: Run Task ⇨ run 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:
Then select the task you want to run:
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: