tasks.json的使用
Intro
tasks.json 的作用是什么呢?官方的解释是这样的:
Lots of tools exist to automate tasks like linting, building, packaging, testing, or deploying software systems.
Tasks in VS Code can be configured to run scripts and start processes so that many of these existing tools can be used from within VS Code without having to enter a command line or write new code.
linting: Linting is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs > Integrate with External Tools via Tasks
简单来说,tasks.json 的作用就是可以在 VSCode 中编写一些脚本,用于执行一些任务,比如编译代码,运行代码,打包代码等等。
properties
The task’s properties have the following semantic:
- label: The task’s label used in the user interface.
- type: The task’s type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute.
- command: The actual command to execute.
- windows: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
- group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
- presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.
- options: Override the defaults for cwd (current working directory), env (environment variables), or shell (default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes.
- runOptions: Defines when and how a task is run.
示例
tasks.json
的功能十分丰富,这里只列举一些常用的功能,更多的功能可以参考官方文档。
compile c file(Windows)
和compile c file(Linux)
:用于编译 c 文件run exe file(Windows)
用于运行编译好的 exe 文件,代码没有改动时,可以直接运行 exe 文件,避免重新编译。compile and run c file(Windows)
用于编译并运行 c 文件。
ps: 为workbench.action.tasks.runTask
设置快捷键,可显示 tasks 列表
tasks.json的使用